Java GUI之(JRadioButton、JCheckBox、JTextArea)实例
2016-05-24 20:08
567 查看
GUI之(JRadioButton、JCheckBox、JTextArea)实例
本实例来源于《Java基础入门》课后习题
结果
本实例来源于《Java基础入门》课后习题
package BookProblem; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import javax.swing.JCheckBox; import javax.swing.JRadioButton; import javax.swing.ButtonGroup; import javax.swing.JScrollPane; import javax.swing.JTextArea; import java.awt.event.ActionListener; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import java.awt.event.ActionEvent; public class P381Problem6B extends JFrame { private JPanel contentPane; private JTextArea textArea; private Set Hobbies = new HashSet();//兴趣爱好 private String Gender ="";//性别 private final ButtonGroup buttonGroup = new ButtonGroup(); /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { P381Problem6B frame = new P381Problem6B(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public P381Problem6B() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 440); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JPanel panel = new JPanel(); panel.setBounds(10, 10, 414, 74); contentPane.add(panel); panel.setLayout(null); JLabel Peference = new JLabel("兴趣"); Peference.setBounds(10, 10, 54, 15); panel.add(Peference); JCheckBox Badminton = new JCheckBox("羽毛球"); Badminton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (Badminton.isSelected()) { Hobbies.add(Badminton.getText());//选中的复选框把文本添加到 Set 集合中 } else { Hobbies.remove(Badminton.getText());//反之从集合中移除 } PrintJTextArea(); } }); Badminton.setBounds(70, 6, 66, 23); panel.add(Badminton); JCheckBox TableTennis = new JCheckBox("乒乓球"); TableTennis.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(TableTennis.isSelected()){ Hobbies.add(TableTennis.getText());//选中的复选框把文本添加到 Set 集合中 }else{ Hobbies.remove(TableTennis.getText());//反之从集合中移除 } PrintJTextArea(); } }); TableTennis.setBounds(138, 6, 66, 23); panel.add(TableTennis); JCheckBox Sing = new JCheckBox("唱歌"); Sing.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(Sing.isSelected()){ Hobbies.add(Sing.getText());//选中的复选框把文本添加到 Set 集合中 }else{ Hobbies.remove(Sing.getText());//反之从集合中移除 } PrintJTextArea(); } }); Sing.setBounds(200, 6, 66, 23); panel.add(Sing); JLabel Sex = new JLabel("性别"); Sex.setBounds(10, 49, 54, 15); panel.add(Sex); JRadioButton Man = new JRadioButton("男"); Man.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Gender =Man.getText(); PrintJTextArea(); } }); buttonGroup.add(Man); Man.setBounds(70, 45, 66, 23); panel.add(Man); JRadioButton Fale = new JRadioButton("女"); Fale.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Gender =Fale.getText(); PrintJTextArea(); } }); buttonGroup.add(Fale); Fale.setBounds(138, 45, 66, 23); panel.add(Fale); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(10, 89, 414, 303); contentPane.add(scrollPane); textArea = new JTextArea(); scrollPane.setViewportView(textArea); } protected void PrintJTextArea() { // TODO Auto-generated method stub textArea.setText(""); if (Hobbies.size() > 0)//如果 Set 集合中有元素,打印兴趣 textArea.append("你的兴趣爱好有: "); Iterator it = Hobbies.iterator(); while (it.hasNext()) { textArea.append(it.next() + " "); } if (!"".equals(Gender))//如果 Gender 不为空字符串,打印性别 textArea.append("\n你的性别为: " + Gender); } }
结果
相关文章推荐
- SAP Web IDE - sapui5开发工具
- Android酷炫实用的开源框架(UI框架)
- 工具类系列-DataFilterQueue
- UE中使用正则表达式
- QuickHull 快速凸包
- HDU Common Subsequence(最长公共子序列)
- java之十一 Java GUI
- java之十一 Java GUI
- java之十一 Java GUI
- PAT (Advanced Level) 1041. Be Unique (20)
- UIImagePickerController(图片选择器)
- Max Sum of Max-K-sub-sequence(单调队列)
- 下载SAPUI5 SDK
- 设计模式笔记-builder 模式
- AndroidUI——InputFilter的使用(3)
- LA_3263_That_Nice_Euler_Circuits_(欧拉定理+计算几何基础)
- AndroidUI——InputFilter的使用(2)
- NDK编译问题: Program "ndk-build" is not found in PATH 和 Unable to launch cygpath. Is Cygwin on the path?
- AndroidUI——InputFilter的使用(1)
- 判断UIImageView中图片是否相同