1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
public class TestFocusField extends JFrame {
private JComponent separator1;
private JLabel label1;
private JTextField textField1;
private JLabel label2;
private JTextField textField2;
private Color lightBlue = new Color(190,210,255);
public TestFocusField() {
initComponents();
}
private void initComponents() {
DefaultComponentFactory compFactory = DefaultComponentFactory.getInstance();
separator1 = compFactory.createSeparator("Test");
label1 = new JLabel();
textField1 = new JTextField();
label2 = new JLabel();
textField2 = new JTextField();
CellConstraints cc = new CellConstraints();
//======== this ========
JPanel contentPane = (JPanel) getContentPane();
//---- label1 ----
label1.setText("textfield1"); //$NON-NLS-1$
//---- label2 ----
label2.setText("textfield2"); //$NON-NLS-1$
//---- textField1 ----
textField1.setBackground(Color.LIGHT_GRAY);
textField1.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent arg0) {
textField1.setBackground(lightBlue);
}
public void focusLost(FocusEvent arg0) {
textField1.setBackground(Color.LIGHT_GRAY);
}});
//---- textField2 ----
textField2.setBackground(Color.LIGHT_GRAY);
textField2.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent arg0) {
textField2.setBackground(lightBlue);
}
public void focusLost(FocusEvent arg0) {
textField2.setBackground(Color.LIGHT_GRAY);
}});
PanelBuilder contentPaneBuilder = new PanelBuilder(new FormLayout(
new ColumnSpec[] {
FormFactory.DEFAULT_COLSPEC,
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW)
},
new RowSpec[] {
FormFactory.DEFAULT_ROWSPEC,
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC
}), contentPane);
contentPaneBuilder.add(separator1, cc.xywh(1, 1, 3, 1));
contentPaneBuilder.add(label1, cc.xy (1, 3));
contentPaneBuilder.add(textField1, cc.xy (3, 3));
contentPaneBuilder.add(label2, cc.xy (1, 5));
contentPaneBuilder.add(textField2, cc.xy (3, 5));
pack();
setLocationRelativeTo(getOwner());
}
public static void main(String[] args) {
TestFocusField f = new TestFocusField();
f.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
f.setVisible(true);
}
} |
Partager