package com.company;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventListener;
import java.awt.*;
import javax.swing.*;
public class Task{
public static void Operation(){
//定义一个窗口类JFrame类
JFrame f=new JFrame();
f.setSize(400,350);
f.setLocation(300,200);
f.setLayout(new BorderLayout());
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//窗口中放置一个JPanel面板。
JPanel p=new JPanel();
//JPanel面板从左到右依次放置如下组件
p.setLayout(new FlowLayout());
//Jable组件设置标题
JLabel label=new JLabel("兴趣:",JLabel.CENTER);
p.add(label);
//三个多选按钮JCheckBox
JCheckBox c1=new JCheckBox("羽毛球",false);
p.add(c1);
JCheckBox c2=new JCheckBox("乒乓球",false);
p.add(c2);
JCheckBox c3=new JCheckBox("唱歌",false);
p.add(c3);
//JLable标签进行性别选择
JLabel label2=new JLabel("性别:",JLabel.CENTER);
p.add(label2);
//两个JRadioButton按钮,文本分别为”男“”女“。
JRadioButton r1=new JRadioButton("男",false);
JRadioButton r2=new JRadioButton("女",false);
p.add(r1);
p.add(r2);
//窗口的CENTER区域放置一个JScrollPane容器,容器中放置一个JTextArea文本域。
JTextArea showArea=new JTextArea(2,5);
JScrollPane sp=new JScrollPane(showArea);
//为多选框定义监听器
ActionListener listener=new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(c1.isSelected())
showArea.setText(c1.getText());
if(c2.isSelected())
showArea.setText(c2.getText());
if(c3.isSelected())
showArea.setText(c3.getText());
}
};
//为单选框定义监听器
ActionListener listener2=new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if (r1.isSelected())
showArea.setText(r1.getText());
if (r2.isSelected())
showArea.setText(r2.getText());
}
};
c1.addActionListener(listener);
c2.addActionListener(listener);
c3.addActionListener(listener);
r1.addActionListener(listener2);
r2.addActionListener(listener2);
f.add(sp,BorderLayout.CENTER);
}
public static void main(String[] args){
SwingUtilities.invokeLater(Task::MyOperation);
}
}



