GUI2

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class GTest2 extends JFrame{

private JLabel label1,label2,label3;

private JTextField field;

private JRadioButton manButton,womenButton;

private ButtonGroup radioGroup;

private JCheckBox aBox,bBox,cBox;

private JButton button;

private JTextArea area;

public GTest2() {

super();

Container container = getContentPane();

FlowLayout layout = new FlowLayout();

layout.setAlignment(FlowLayout.LEFT);

container.setLayout(layout);

label1 = new JLabel("姓名");

label2 = new JLabel("性别");

label3 = new JLabel("爱好");

field = new JTextField(10);

manButton = new JRadioButton("男",true);

womenButton = new JRadioButton("女",false);

radioGroup = new ButtonGroup();

radioGroup.add(manButton);

radioGroup.add(womenButton);

//使得只能选择一个

aBox = new JCheckBox("体育");

bBox = new JCheckBox("音乐");

cBox = new JCheckBox("游戏");

button = new JButton("确认");

area = new JTextArea(10,10);

container.add(label1);

container.add(field);

container.add(label2);

container.add(manButton);

container.add(womenButton);

container.add(label3);

container.add(aBox);

container.add(bBox);

container.add(cBox);

container.add(button);

container.add(area);

setSize(400,400);

setVisible(true);

ButtonHandle BH = new ButtonHandle();

button.addActionListener(BH);

}

public static void main(String args[]) {

GTest2 a = new GTest2();

a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public class ButtonHandle extends JFrame implements ActionListener{

public void actionPerformed(ActionEvent e) {

  String str = "姓名:"+field.getText()+"\n性别:";

  if(manButton.isSelected()) {

  str+="男";

  }

  else {

  str+="女";

  }

  str+="\n爱好:";

  if(aBox.isSelected()) {

  str+="体育 ";

  }

  if(bBox.isSelected()) {

  str+="音乐 ";

  }

  if(cBox.isSelected()) {

  str+="游戏";

  }  

  area.setText(str);    

}

}

}


你可能感兴趣的:(GUI2)