java 窗口点击事件

来源:互联网 发布:平安淘宝信用卡 编辑:程序博客网 时间:2024/06/11 01:08

题目:

编写一个JFrame窗口,要求如下:
1. 在窗口的NORTH区放置一个JPanel面板。
2. JPanel面板放置如下组件:
(1) JLable标签,标签文本为“兴趣”,右边接着是三个JCheckBox多选按钮,选项分别是“羽毛球”、“乒乓球”、“唱歌”。可以多选。
(2) JLabel标签,标签文本为“性别”,右边接着是两个JRadioButton按钮,选项分别是“男”、“女”。置成单选按钮,提示:使用ButtonGroup类 。
(3) 兴趣标签及按钮放在第一行,性别标签及按钮放在第二行,分别借助两个行型Box容器安排这两行组件的位置,而两个行型Box容器放入JPanel面板中,要两行组件对齐的话,可以把JPanel面板设置两行一列的GridLayout布局。
3. 窗口的CENTER区域放置一个JScrollPane容器,容器中放置一个JTextArea文本域。
4. 当点击JCheckBox多选按钮和JRadioButton按钮时,如果是选中操作,则把选中项的文本显示在JTextArea文本域,每行显示一个选项。可以重复点击,每次点击都显示选中项。

主要代码:

import java.awt.*;import java.awt.event.*;import javax.swing.*;public class ButtonFrame extends JFrame{JLabel label1,label2;JCheckBox checkbox1,checkbox2,checkbox3;JRadioButton radioButton1,radioButton2;Box boxV1, boxV2; ButtonGroup group;JTextArea textArea = new JTextArea(5,25);MyListener mListener;public static void main(String[] args) {new ButtonFrame();}ButtonFrame(){super("窗口点击事件");init();click();}public void init(){Container contentPane = getContentPane();JPanel panel=new JPanel();panel=new JPanel(new GridLayout(2,2));label1=new JLabel("兴趣");label2=new JLabel("性别");checkbox1=new JCheckBox("羽毛球");checkbox2=new JCheckBox("乒乓球");checkbox3=new JCheckBox("唱歌");radioButton1=new JRadioButton("男");radioButton2=new JRadioButton("女");group = new ButtonGroup();group.add(radioButton1);group.add(radioButton2);boxV1 = Box.createHorizontalBox();        boxV1.add(label1);        boxV1.add(Box.createHorizontalStrut(3));        boxV1.add(checkbox1);        boxV1.add(Box.createHorizontalStrut(3));        boxV1.add(checkbox2);        boxV1.add(Box.createHorizontalStrut(3));        boxV1.add(checkbox3);        boxV1.add(Box.createHorizontalStrut(3));        boxV2 = Box.createHorizontalBox();        boxV2.add(label2);        boxV2.add(Box.createHorizontalStrut(3));        boxV2.add(radioButton1);        boxV2.add(Box.createHorizontalStrut(3));        boxV2.add(radioButton2);        panel.add(boxV1);        panel.add(boxV2);        contentPane.add(panel,BorderLayout.NORTH);        JScrollPane scrollPane = new JScrollPane(textArea);        contentPane.add(scrollPane, BorderLayout.CENTER);        setVisible(true);        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        setSize(400, 300);        mListener=new MyListener();}private void click() {checkbox1.addActionListener(mListener);                   //为每个按钮设定监视器checkbox2.addActionListener(mListener);checkbox3.addActionListener(mListener);radioButton1.addActionListener(mListener);radioButton2.addActionListener(mListener);}private class MyListener implements ActionListener {@Overridepublic void actionPerformed(ActionEvent e) {if(e.getSource() == checkbox1)          {              if(checkbox1.isSelected() == true)              {                   textArea.append(checkbox1.getText() + "\n");               }          }            else if(e.getSource() == checkbox2)          {              if(checkbox2.isSelected() == true)              {                   textArea.append(checkbox2.getText() + "\n");               }                    }           else if(e.getSource() == checkbox3)          {              if(checkbox3.isSelected() == true)              {                   textArea.append(checkbox3.getText() + "\n");               }           }          else if(e.getSource() == radioButton1)          {             if(radioButton1 .isSelected() == true)             {                   textArea.append(radioButton1.getText() + "\n");              }           }          else if(e.getSource() == radioButton2)          {             if(radioButton2 .isSelected() == true)             {                   textArea.append(radioButton2.getText()+ "\n");              }           }}}}

结果图:


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 考的大学不理想怎么办 只考上二本大学怎么办 w7电脑中病毒了怎么办 电脑中病毒了该怎么办 泰迪呼吸急促怎么办啊 狗狗呼吸急促是怎么办 狗狗着凉了呕吐怎么办 狗鼻子流黄鼻涕怎么办 刚出生婴儿睡觉不踏实怎么办 有人溺水后你该怎么办 借钱不还怎么办没欠条 私人欠货款不还怎么办 公司欠货款不还怎么办 两个人离婚一方不同意怎么办 比亚迪l3油耗高怎么办 u盘密码忘记了怎么办 主板没有m.2接口怎么办 点痣留下了疤怎么办 危险三角区长痘痘怎么办 挤了危险三角区怎么办 三角区长痘挤了怎么办 三角区发红长痘怎么办 激光祛斑碰水了怎么办 激光打痣留下坑怎么办 点痣之后留下坑怎么办 去痣留下的红印怎么办 激光点痦子留疤怎么办 激光点痣的疤痕怎么办 做完眉毛碰水了怎么办 脸上疤掉了有坑怎么办 结痂不小心抠掉怎么办 脸上肉松弛怎么办19岁 点痣留下来的疤怎么办 激光祛斑的红印怎么办 脸上疤掉了红印怎么办 痘痘发炎了红肿怎么办 脸上的斑越来越多了怎么办 点痣留下的疤痕怎么办 额头又高又大怎么办 脸太长额头太高怎么办 动车因台风停运怎么办