1 package zuye2; 2 import java.awt.*; 3 import java.awt.event.*; 4 import javax.swing.*; 5 6 public class MyWindow extends JFrame implements ActionListener { 7 8 JLabel interestLabel = new JLabel("兴趣:"); 9 JCheckBox badmintonCheck = new JCheckBox("羽毛球"); 10 JCheckBox tableTtennisCheck = new JCheckBox("乒乓球"); 11 JCheckBox singCheck = new JCheckBox("唱歌"); 12 13 JLabel genderLabel = new JLabel("性别:"); 14 JRadioButton maleRadioButton = new JRadioButton("男"); 15 JRadioButton femaleRadioButton = new JRadioButton("女"); 16 17 JTextArea textArea = new JTextArea(5,25); 18 19 MyWindow() 20 { 21 super("work"); 22 Container contentPane = getContentPane(); 23 24 JPanel northPanel = new JPanel(); 25 northPanel.setLayout(new GridLayout(2,1)); 26 27 Box box1 = Box.createHorizontalBox(); 28 Box box2 = Box.createHorizontalBox(); 29 30 box1.add(Box.createHorizontalStrut(3)); 31 box1.add(interestLabel ); 32 box1.add(badmintonCheck ); 33 box1.add(tableTtennisCheck ); 34 box1.add(singCheck); 35 36 ButtonGroup group = new ButtonGroup(); 37 group.add(maleRadioButton); 38 group.add(femaleRadioButton); 39 40 box2.add(Box.createHorizontalStrut(3)); 41 box2.add(genderLabel); 42 box2.add(maleRadioButton); 43 box2.add(femaleRadioButton); 44 45 northPanel.add(box1); 46 northPanel.add(box2); 47 contentPane.add(northPanel, BorderLayout.NORTH); 48 49 JScrollPane scrollPane = new JScrollPane(textArea); 50 contentPane.add(scrollPane, BorderLayout.CENTER); 51 52 badmintonCheck.addActionListener(this); 53 tableTtennisCheck.addActionListener(this); 54 singCheck.addActionListener(this); 55 maleRadioButton.addActionListener(this); 56 femaleRadioButton.addActionListener(this); 57 58 setVisible(true); 59 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 60 setSize(400, 300); 61 } 62 63 public void actionPerformed(ActionEvent e) 64 { 65 if(e.getSource() == badmintonCheck) 66 { 67 if(badmintonCheck.isSelected() == true) 68 { 69 textArea.append("羽毛球" + " "); 70 } 71 } 72 else if(e.getSource() == tableTtennisCheck) 73 { 74 if(tableTtennisCheck.isSelected() == true) 75 { 76 textArea.append("乒乓球" + " "); 77 } 78 } 79 else if(e.getSource() == singCheck) 80 { 81 if(singCheck.isSelected() == true) 82 { 83 textArea.append("唱歌" + " "); 84 } 85 } 86 else if(e.getSource() == maleRadioButton) 87 { 88 if(maleRadioButton .isSelected() == true) 89 { 90 textArea.append("男" + " "); 91 } 92 } 93 94 else if(e.getSource() == femaleRadioButton) 95 { 96 if(femaleRadioButton .isSelected() == true) 97 { 98 textArea.append("女" + " "); 99 } 100 } 101 else 102 { 103 return; 104 } 105 } 106 107 public static void main(String args[]) 108 { 109 new MyWindow(); 110 } 111 }