package javafr; import java.awt.Dimension; import javax.swing.JTextField; /** * exemple de fabrique de composants swing * * @author Jonathan Guéhenneux */ public class ComponentFactory { public static final ComponentFactory INSTANCE = new ComponentFactory(); private static final Dimension DEFAULT_TEXT_FIELD_DIMENSION = new Dimension(0, 20); /** * constructeur privé pour implémenter la pattern singleton et éviter le gaspillage d'instances */ private ComponentFactory() { } /** * @return */ public JTextField createTextField() { JTextField textField = new JTextField(); textField.setPreferredSize(DEFAULT_TEXT_FIELD_DIMENSION); return textField; } }
import java.awt.Rectangle; import javax.swing.JFrame; import javax.swing.JTextField; public class Swing2 { public static void main(String argv[]) { JFrame f = new JFrame("ma fenetre"); f.setSize(600, 400); JTextField testField1 = ComponentFactory.INSTANCE.createTextField(); testField1.setBounds(new Rectangle(5,5, 90, 17)); JTextField testField6 = ComponentFactory.INSTANCE.createTextField(); testField6.setBounds(new Rectangle(90,5, 50, 33)); JTextField testField = ComponentFactory.INSTANCE.createTextField(); testField.setBounds(new Rectangle(5,65,80,20)); JTextField testField3 = ComponentFactory.INSTANCE.createTextField(); testField3.setBounds(new Rectangle(5,100,70,30)); f.getContentPane().add(testField1); f.getContentPane().add(testField); f.getContentPane().add(testField3); f.getContentPane().add(testField6); f.setVisible(true); } }
package javafr; import java.awt.Rectangle; import javax.swing.JTextField; /** * @author Jonathan Guéhenneux */ public class MyTextField extends JTextField { private static final int HEIGHT = 20; @Override public void setBounds(int x, int y, int width, int height) { super.setBounds(x, y, width, HEIGHT); } @Override public void setBounds(Rectangle bounds) { bounds.height = HEIGHT; super.setBounds(bounds); } }
package javafr; import javax.swing.JTextField; /** * exemple de fabrique de composants swing * * @author Jonathan Guéhenneux */ public class ComponentFactory { public static final ComponentFactory INSTANCE = new ComponentFactory(); /** * constructeur privé pour implémenter la pattern singleton et éviter le gaspillage d'instances */ private ComponentFactory() { } /** * @return */ public JTextField createTextField() { return new MyTextField(); } }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question