不想学习只想摆烂系列之GUIjava项目

2023-12-17 20:46:05

知道GUI框架怎么写就行

1.定义jFRame

2.分开写测试类

3.给几个按钮

4.负责提供测试器

这样就把框架打好了

主程序-继承某个类

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RadioButtonApp {

    public static void main(String[] args) {
        JFrame frame = new JFrame("单选按钮");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        frame.setLayout(new FlowLayout());

        JRadioButton radioButton1 = new JRadioButton("选项1");
        JRadioButton radioButton2 = new JRadioButton("选项2");
        JRadioButton radioButton3 = new JRadioButton("选项3");

        // 创建一个组,确保只能选择一个按钮
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton1);
        group.add(radioButton2);
        group.add(radioButton3);


        radioButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "你选择了选项1");
                radioButton1.setVisible(false);
            }
        });
        radioButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "你选择了选项2");
                radioButton2.setVisible(false);
            }
        });
        radioButton3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "你选择了选项3");
                radioButton3.setVisible(false);
            }
        });


        frame.add(radioButton1);
        frame.add(radioButton2);
        frame.add(radioButton3);

        frame.setVisible(true);
    }
}

文章来源:https://blog.csdn.net/leke2003/article/details/135049357
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。