编写一个JFrame窗口,要求如下: 1. 在窗口的NORTH区放置一个JPanel面板。 2. JPanel面板放置如下组件: (1) JLable标签,标签文本为“兴趣”,右边接着是三个JChec
package 窗口;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Window extends JFrame implements ActionListener {
JLabel lb1,lb2;
JCheckBox cb1,cb2,cb3;
JRadioButton rb1,rb2;
JTextArea area=new JTextArea(12,12);
ButtonGroup group;
JScrollPane sp;
Box box1,box2;
Window(){
super("JFrame");
Container contentPane=getContentPane();
JFrame frame=new JFrame();
JPanel p1=new JPanel();
GridLayout grid=new GridLayout(2,1);
p1.setLayout(grid);
box1=Box.createHorizontalBox();
box2=Box.createHorizontalBox();
lb1=new JLabel("兴趣");
cb1=new JCheckBox("羽毛球");
cb2=new JCheckBox("乒乓球");
cb3=new JCheckBox("唱歌");
lb2=new JLabel("性别");
group=new ButtonGroup();
rb1=new JRadioButton("男");
rb2=new JRadioButton("女");
group.add(rb1);
group.add(rb2);
box1.add(lb1);
box1.add(cb1);
box1.add(cb2);
box1.add(cb3);
box2.add(lb2);
box2.add(rb1);
box2.add(rb2);
p1.add(box1);
p1.add(box2);
contentPane.add(p1,BorderLayout.NORTH);
sp=new JScrollPane(area);
JTextArea area=new JTextArea();
sp.add(area);
contentPane.add(sp,BorderLayout.CENTER);
validate();
setSize(300,400);
setVisible(true);
rb1.addActionListener(this);
rb2.addActionListener(this);
cb1.addActionListener(this);
cb2.addActionListener(this);
cb3.addActionListener(this);
}
public void actionPerformed (ActionEvent e){
if(e.getSource()==cb1){
area.append("羽毛球"+"\n");
}
if(e.getSource()==cb2){
area.append("乒乓球"+"\n");
}
if(e.getSource()==cb3){
area.append("唱歌"+"\n");
}
if(e.getSource()==rb1){
area.append("男"+"\n");
}
if(e.getSource()==rb2){
area.append("女"+"\n");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Window win=new Window();
win.setBounds(100,100,500,300);
win.setVisible(true);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Window extends JFrame implements ActionListener {
JLabel lb1,lb2;
JCheckBox cb1,cb2,cb3;
JRadioButton rb1,rb2;
JTextArea area=new JTextArea(12,12);
ButtonGroup group;
JScrollPane sp;
Box box1,box2;
Window(){
super("JFrame");
Container contentPane=getContentPane();
JFrame frame=new JFrame();
JPanel p1=new JPanel();
GridLayout grid=new GridLayout(2,1);
p1.setLayout(grid);
box1=Box.createHorizontalBox();
box2=Box.createHorizontalBox();
lb1=new JLabel("兴趣");
cb1=new JCheckBox("羽毛球");
cb2=new JCheckBox("乒乓球");
cb3=new JCheckBox("唱歌");
lb2=new JLabel("性别");
group=new ButtonGroup();
rb1=new JRadioButton("男");
rb2=new JRadioButton("女");
group.add(rb1);
group.add(rb2);
box1.add(lb1);
box1.add(cb1);
box1.add(cb2);
box1.add(cb3);
box2.add(lb2);
box2.add(rb1);
box2.add(rb2);
p1.add(box1);
p1.add(box2);
contentPane.add(p1,BorderLayout.NORTH);
sp=new JScrollPane(area);
JTextArea area=new JTextArea();
sp.add(area);
contentPane.add(sp,BorderLayout.CENTER);
validate();
setSize(300,400);
setVisible(true);
rb1.addActionListener(this);
rb2.addActionListener(this);
cb1.addActionListener(this);
cb2.addActionListener(this);
cb3.addActionListener(this);
}
public void actionPerformed (ActionEvent e){
if(e.getSource()==cb1){
area.append("羽毛球"+"\n");
}
if(e.getSource()==cb2){
area.append("乒乓球"+"\n");
}
if(e.getSource()==cb3){
area.append("唱歌"+"\n");
}
if(e.getSource()==rb1){
area.append("男"+"\n");
}
if(e.getSource()==rb2){
area.append("女"+"\n");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Window win=new Window();
win.setBounds(100,100,500,300);
win.setVisible(true);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}