Java GUI之(JRadioButton、JCheckBox、JTextArea)实例

GUI之(JRadioButton、JCheckBox、JTextArea)实例

本实例来源于《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); 
	}
}


 

结果

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值