package ToggleDemo;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class ToggleDemo extends JFrame {
private JTextArea area = new JTextArea(6,15);
private JScrollPane paneWithTextArea = new JScrollPane(area);
private JPanel panWithButtons = new JPanel();
private JLabel label1 = new JLabel("兴趣:");
private JLabel label2 = new JLabel("性别:");
private JCheckBox cb1 = new JCheckBox("唱歌");
private JCheckBox cb2 = new JCheckBox("游泳");
private JCheckBox cb3 = new JCheckBox("旅游");
private JRadioButton rb1 = new JRadioButton("男",true);
private JRadioButton rb2 = new JRadioButton("女");
private ButtonGroup group = new ButtonGroup();
private Set<String> hobbies = new HashSet<String>();
private String sex = "男";
private ActionListener listener1 = new ActionListener() {
public void actionPerformed(ActionEvent event) {
JCheckBox cb = (JCheckBox)event.getSource();
if(cb.isSelected()) {
hobbies.add(cb.getText());
} else {
hobbies.remove(cb.getText());
}
printStatus();
}
};
private ActionListener listener2 = new ActionListener() {
public void actionPerformed(ActionEvent event) {
JRadioButton cb = (JRadioButton)event.getSource();
sex = cb.getText();
printStatus();
}
};
public ToggleDemo(String title) {
super(title);
area.setEditable(false);
cb1.addActionListener(listener1);
cb2.addActionListener(listener1);
cb3.addActionListener(listener1);
rb1.addActionListener(listener2);
rb2.addActionListener(listener2);
group.add(rb1);
group.add(rb2);
panWithButtons.setLayout(new FlowLayout());
panWithButtons.add(label1);
panWithButtons.add(cb1);
panWithButtons.add(cb2);
panWithButtons.add(cb3);
panWithButtons.add(label2);
panWithButtons.add(rb1);
panWithButtons.add(rb2);
Container contentPane = getContentPane();
contentPane.add(panWithButtons, BorderLayout.NORTH);
contentPane.add(paneWithTextArea,BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
private void printStatus() {
area.append("您的兴趣爱好包括:");
Iterator<String> it = hobbies.iterator();
while(it.hasNext()) {
area.append(it.next()+" ");
}
area.append("您的性别为:"+sex+"\n");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("eeesdasfad");
new ToggleDemo("Hello");
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class ToggleDemo extends JFrame {
private JTextArea area = new JTextArea(6,15);
private JScrollPane paneWithTextArea = new JScrollPane(area);
private JPanel panWithButtons = new JPanel();
private JLabel label1 = new JLabel("兴趣:");
private JLabel label2 = new JLabel("性别:");
private JCheckBox cb1 = new JCheckBox("唱歌");
private JCheckBox cb2 = new JCheckBox("游泳");
private JCheckBox cb3 = new JCheckBox("旅游");
private JRadioButton rb1 = new JRadioButton("男",true);
private JRadioButton rb2 = new JRadioButton("女");
private ButtonGroup group = new ButtonGroup();
private Set<String> hobbies = new HashSet<String>();
private String sex = "男";
private ActionListener listener1 = new ActionListener() {
public void actionPerformed(ActionEvent event) {
JCheckBox cb = (JCheckBox)event.getSource();
if(cb.isSelected()) {
hobbies.add(cb.getText());
} else {
hobbies.remove(cb.getText());
}
printStatus();
}
};
private ActionListener listener2 = new ActionListener() {
public void actionPerformed(ActionEvent event) {
JRadioButton cb = (JRadioButton)event.getSource();
sex = cb.getText();
printStatus();
}
};
public ToggleDemo(String title) {
super(title);
area.setEditable(false);
cb1.addActionListener(listener1);
cb2.addActionListener(listener1);
cb3.addActionListener(listener1);
rb1.addActionListener(listener2);
rb2.addActionListener(listener2);
group.add(rb1);
group.add(rb2);
panWithButtons.setLayout(new FlowLayout());
panWithButtons.add(label1);
panWithButtons.add(cb1);
panWithButtons.add(cb2);
panWithButtons.add(cb3);
panWithButtons.add(label2);
panWithButtons.add(rb1);
panWithButtons.add(rb2);
Container contentPane = getContentPane();
contentPane.add(panWithButtons, BorderLayout.NORTH);
contentPane.add(paneWithTextArea,BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
private void printStatus() {
area.append("您的兴趣爱好包括:");
Iterator<String> it = hobbies.iterator();
while(it.hasNext()) {
area.append(it.next()+" ");
}
area.append("您的性别为:"+sex+"\n");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("eeesdasfad");
new ToggleDemo("Hello");
}
}