两种选择
Swing(学习主要内容)
awt(与平台相关,终将被淘汰)
Swing中的控件
JButton JCheckBox JRadioButton JLabel JTextField JTextArea JComboBox JList JScrollBar JSlider等些控件
JButton的使用
JButton jbt = new JButton("test");
jbt.setSize(60, 20);
jbt.setLocation(10, 20);
jbt.setBackground(Color.getHSBColor(220, 200, 200));
jbt.setForeground(Color.PINK);
jbt.setToolTipText("点击可用");
//jbt.setBorderPainted(false);
jbt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "Test", "Haha", JOptionPane.INFORMATION_MESSAGE);
}
});
JCheckBox的使用
final JCheckBox jchk = new JCheckBox("Student" , true);
jchk.setSize(80, 20);
jchk.setLocation(90, 20);
jchk.setMnemonic('S');
jchk.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(jchk.isSelected())
JOptionPane.showMessageDialog(null, "Check!");
}
});
JRadioButton的使用
final JRadioButton jrb1 = new JRadioButton("A", true);
JRadioButton jrb2 = new JRadioButton("B");
JRadioButton jrb3 = new JRadioButton("C");
jrb1.setSize(40, 20);
jrb1.setLocation(20, 40);
jrb2.setSize(40, 20);
jrb2.setLocation(20, 60);
jrb3.setSize(40, 20);
jrb3.setLocation(20, 80);
ButtonGroup group = new ButtonGroup();
group.add(jrb1);
group.add(jrb2);
group.add(jrb3);
add(jrb1);
add(jrb2);
add(jrb3);
jrb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(jrb1.isSelected())
JOptionPane.showMessageDialog(null, "A is checked");
}
});