编写一个JFrame窗口,要求如下: 1. 在窗口的NORTH区放置一个JPanel面板。 2. JPanel面板放置如下组件: (1) JLable标签,标签文本为“兴趣”,右边接着是三个JChec
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyFirstWindow extends JFrame implements ActionListener{
JLabel label1 =new JLabel("兴趣:");
JCheckBox checkBox1=new JCheckBox("羽毛球");
JCheckBox checkBox2=new JCheckBox("乒乓球");
JCheckBox checkBox3=new JCheckBox("唱歌");
JLabel label2=new JLabel("性别:");
JRadioButton radioButtion1 =new JRadioButton("男");
JRadioButton radioButtion2 =new JRadioButton("女");
JTextArea textArea = new JTextArea(25,25);
MyFirstWindow(){
super("我的Java作业");
Container contentPane=getContentPane();
//盒子
Box box1=Box.createHorizontalBox();
Box box2=Box.createHorizontalBox();
box1.add(label1);
box1.add(checkBox1);
box1.add(checkBox2);
box1.add(checkBox3);
box2.add(label2);
box2.add(radioButtion1);
box2.add(radioButtion2);
//面板
JPanel panel1 =new JPanel();
panel1.add(box1);
panel1.add(box2);
GridLayout grid=new GridLayout (2,1);
panel1.setLayout(grid);
contentPane.add(panel1,BorderLayout.NORTH);
//滚动窗格
JScrollPane scroll =new JScrollPane(textArea);
JTextArea textArea =new JTextArea();
scroll.add(textArea);
contentPane.add(scroll,BorderLayout.CENTER);
//窗体自身作为监听器
checkBox1.addActionListener(this);
checkBox2.addActionListener(this);
checkBox3.addActionListener(this);
radioButtion1.addActionListener(this);
radioButtion2.addActionListener(this);
//设置窗口
setSize(300,400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==checkBox1) {
if(checkBox1.isSelected()==true)
{
textArea.append("羽毛球"+"\n");
}
}
else if(e.getSource()==checkBox2) {
if(checkBox2.isSelected()==true)
{
textArea.append("乒乓球"+"\n");
}
}
else if(e.getSource()==checkBox3)
{
if(checkBox3.isSelected()==true)
{
textArea.append("唱歌"+"\n");
}
}
else if(e.getSource()== radioButtion1)
{
if( radioButtion1.isSelected()==true)
{
textArea.append("男"+"\n");
}
}
else {
if( radioButtion2.isSelected()==true)
{
textArea.append("女"+"\n");
}
}
}
import java.awt.event.*;
import javax.swing.*;
public class MyFirstWindow extends JFrame implements ActionListener{
JLabel label1 =new JLabel("兴趣:");
JCheckBox checkBox1=new JCheckBox("羽毛球");
JCheckBox checkBox2=new JCheckBox("乒乓球");
JCheckBox checkBox3=new JCheckBox("唱歌");
JLabel label2=new JLabel("性别:");
JRadioButton radioButtion1 =new JRadioButton("男");
JRadioButton radioButtion2 =new JRadioButton("女");
JTextArea textArea = new JTextArea(25,25);
MyFirstWindow(){
super("我的Java作业");
Container contentPane=getContentPane();
//盒子
Box box1=Box.createHorizontalBox();
Box box2=Box.createHorizontalBox();
box1.add(label1);
box1.add(checkBox1);
box1.add(checkBox2);
box1.add(checkBox3);
box2.add(label2);
box2.add(radioButtion1);
box2.add(radioButtion2);
//面板
JPanel panel1 =new JPanel();
panel1.add(box1);
panel1.add(box2);
GridLayout grid=new GridLayout (2,1);
panel1.setLayout(grid);
contentPane.add(panel1,BorderLayout.NORTH);
//滚动窗格
JScrollPane scroll =new JScrollPane(textArea);
JTextArea textArea =new JTextArea();
scroll.add(textArea);
contentPane.add(scroll,BorderLayout.CENTER);
//窗体自身作为监听器
checkBox1.addActionListener(this);
checkBox2.addActionListener(this);
checkBox3.addActionListener(this);
radioButtion1.addActionListener(this);
radioButtion2.addActionListener(this);
//设置窗口
setSize(300,400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==checkBox1) {
if(checkBox1.isSelected()==true)
{
textArea.append("羽毛球"+"\n");
}
}
else if(e.getSource()==checkBox2) {
if(checkBox2.isSelected()==true)
{
textArea.append("乒乓球"+"\n");
}
}
else if(e.getSource()==checkBox3)
{
if(checkBox3.isSelected()==true)
{
textArea.append("唱歌"+"\n");
}
}
else if(e.getSource()== radioButtion1)
{
if( radioButtion1.isSelected()==true)
{
textArea.append("男"+"\n");
}
}
else {
if( radioButtion2.isSelected()==true)
{
textArea.append("女"+"\n");
}
}
}
}
public class TestMyFirstWindow {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyFirstWindow my =new MyFirstWindow();
}
}
结果: