复选框与单选按钮
2015-11-29 22:16
351 查看
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");
}
}
相关文章推荐
- 启动其他应用
- 浅谈三层架构
- 用户登录注册留言程序
- t4 template multi file output
- HashMap(JDK1.8)源码剖析
- 如何用命令在ubuntu(linux)下安装mysql数据库
- LeetCode -- 3Sum Closest
- hpuoj 1714: 感恩节KK专场——送给新生的礼物 (水题)
- [JavaWeb]Maven安装配置
- map的详细用法
- 非负矩阵分解中基于L1和L2范式的稀疏性约束
- VS2010创建和显式调用dll
- 读取Java文件到byte数组的三种方式
- for_each调用类成员函数,mem_fun, mem_fun_ref用法
- 善用php-fpm的慢执行日志slow log分析PHP执行效率和程序BUG
- Bootstrap 提示工具(Tooltip)插件方法的用法
- Contains Duplicate
- 初来乍到,请多关照
- MAC 下配置adb环境
- ECSHOP系统ajax更新购物车数量