zoukankan      html  css  js  c++  java
  • 在学习枯燥的Java中遇见美丽的Jframe,窗体中的单选按钮(JRadioButton)

    package com.company;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    
    public class Main  extends JFrame{
        public Main(){
            setBounds(100,100,180,110);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    
            Container c=getContentPane();
            c.setLayout(new FlowLayout());
    
            JRadioButton left=new JRadioButton("向右走");
            JRadioButton right=new JRadioButton("向左走");
            c.add(left);
            c.add(right);
            ButtonGroup group=new ButtonGroup();//按钮组
            group.add(left);//把按钮放在按钮组中
            group.add(right);
            left.setSelected(true);
            JButton bun=new JButton("打印");
            bun.addActionListener(new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println(left.getText()+"按钮选中状态:"+left.isSelected());
                    System.out.println(right.getText()+"按钮选中状态:"+right.isSelected());
                    group.clearSelection();//按钮组选项清空
                }
            });
            c.add(bun);
            setVisible(true);
        }
    
        public static void main(String[] args) {
            new Main();
        // write your code here
        }
    }
    package com.company;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    
    public class Main  extends JFrame{
        public Main(){
            setBounds(100,100,180,110);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    
            Container c=getContentPane();
            c.setLayout(new FlowLayout());
    
            JRadioButton left=new JRadioButton("向右走");
            JRadioButton right=new JRadioButton("向左走");
            c.add(left);
            c.add(right);
            ButtonGroup group=new ButtonGroup();//按钮组
            group.add(left);//把按钮放在按钮组中
            group.add(right);
            left.setSelected(true);
            JButton bun=new JButton("打印");
            bun.addActionListener(new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println(left.getText()+"按钮选中状态:"+left.isSelected());
                    System.out.println(right.getText()+"按钮选中状态:"+right.isSelected());
                    group.clearSelection();//按钮组选项清空
                }
            });
            c.add(bun);
            setVisible(true);
        }
    
        public static void main(String[] args) {
            new Main();
        // write your code here
        }
    }
    package com.company;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    
    public class Main  extends JFrame{
        public Main(){
            setBounds(100,100,180,110);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    
            Container c=getContentPane();
            c.setLayout(new FlowLayout());
    
            JRadioButton left=new JRadioButton("向右走");
            JRadioButton right=new JRadioButton("向左走");
            c.add(left);
            c.add(right);
            ButtonGroup group=new ButtonGroup();//按钮组
            group.add(left);//把按钮放在按钮组中
            group.add(right);
            left.setSelected(true);
            JButton bun=new JButton("打印");
            bun.addActionListener(new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println(left.getText()+"按钮选中状态:"+left.isSelected());
                    System.out.println(right.getText()+"按钮选中状态:"+right.isSelected());
                    group.clearSelection();//按钮组选项清空
                }
            });
            c.add(bun);
            setVisible(true);
        }
    
        public static void main(String[] args) {
            new Main();
        // write your code here
        }
    }

  • 相关阅读:
    nginx不支持pathinfo模式解决方法
    php只保留两位小数
    分享图文到QQ空间
    android 使用信鸽推送通知栏不显示推送的通知?
    Editetext获取焦点后让输入软键盘中出现搜索按键
    Android EditText的使用过程中遇到的问题
    在线根据现有apk生成指定id的推广apk (已过时)
    @Validated和@Valid区别
    idea快捷键使用总结
    大概
  • 原文地址:https://www.cnblogs.com/llhhcc/p/10128326.html
Copyright © 2011-2022 走看看