第十三周图形用户界面2

import java.awt.*;

import java.awt.event.*;
import javax.swing.*;
public class PersonalInformation extends JFrame {
         JPanel Panel ;
    JLabel lable1,lable2;
    JCheckBox checkBox1,checkBox2,checkBox3;
    ButtonGroup buttongroup;
    JRadioButton radiobutton1,radiobutton2;
    JScrollPane scrollpane;
JTextArea textarea;
public  PersonalInformation(){
super("PersonalInformation");
Listener1 listener = new Listener1();
Box boxH1 = Box.createHorizontalBox();//创建一个大盒子,里面有小的盒子
boxH1.add(Box.createHorizontalStrut(5));
boxH1.add(Box.createHorizontalStrut(50));
boxH1.add(Box.createHorizontalStrut(5));
boxH1.add(Box.createHorizontalStrut(5));
boxH1.add(lable1);
boxH1.add(checkBox1);
boxH1.add(checkBox2);
boxH1.add(checkBox3);
lable1=new JLabel("兴趣");
checkBox1 =new JCheckBox("羽毛球");
checkBox1.addItemListener(listener);
checkBox2 =new JCheckBox("兵乓球");
checkBox2.addItemListener(listener);
checkBox3 =new JCheckBox("唱歌");
checkBox3.addItemListener(listener);
Listener2 l2 = new Listener2();
buttongroup=new ButtonGroup();
Box boxH2 = Box.createHorizontalBox();
boxH2.add(Box.createHorizontalStrut(5));
boxH2.add(lable2);
boxH2.add(Box.createHorizontalStrut(50));
boxH2.add(radiobutton1);
boxH2.add(Box.createHorizontalStrut(5));
boxH2.add(radiobutton2);
radiobutton1.addActionListener(l2);
radiobutton2.addActionListener(l2);
buttongroup.add(radiobutton1);
buttongroup.add(radiobutton2);
lable2=new JLabel("性别");
radiobutton1=new JRadioButton("男");
radiobutton2=new JRadioButton("女");
JPanel panel=new JPanel ();
Panel.setLayout(new GridLayout(2,1));
Panel.add(boxH1);
Panel.add(boxH2);
textarea = new JTextArea(200, 300);
scrollpane= new JScrollPane(textarea);
Container contentPane = getContentPane();
contentPane.add(panel, BorderLayout.NORTH);
contentPane.add(scrollpane, BorderLayout.CENTER);
setSize(400,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new  PersonalInformation();
}
       //步骤2
class  Listener1 implements ItemListener{
public void itemStateChanged(ItemEvent e){
if(e.getSource()==checkBox1)
textarea.append(checkBox1.getText()+"\n");
else if(e.getSource()==checkBox2)
textarea.append(checkBox2.getText()+"\n");
else if(e.getSource()==checkBox3)
textarea.append(checkBox3.getText()+"\n");
}
}
class Listener2 implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==radiobutton1)
textarea.append(radiobutton1.getText()+"\n");
else if(e.getSource()==radiobutton2)
textarea.append(radiobutton2.getText()+"\n");

}
}

}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值