在学习枯燥的Java中遇见美丽的Jframe,窗体中的单选按钮(JRadioButton)

Posted 逐梦武威

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在学习枯燥的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
    }
}

 

以上是关于在学习枯燥的Java中遇见美丽的Jframe,窗体中的单选按钮(JRadioButton)的主要内容,如果未能解决你的问题,请参考以下文章

java,枯燥中遇见美丽JFrame,窗体(面板,JPanel)

java中怎样让一个JFrame窗体始终在最前端显示?

Java基础 | 简单使用Jframe创建一个窗体实例

代码笔记Java学习一阶段总结

JAVA的jframe怎么让文本框里面的内容出来啊?

Java学习笔记:窗体和画图