【问题标题】:I cannot see Radio buttons in Java Swing我在 Java Swing 中看不到单选按钮
【发布时间】:2020-12-13 09:20:02
【问题描述】:

我有 2 个按钮和一个按钮组,但我在应用程序窗口中看不到它们。普通按钮对我有用,但不是收音机按钮。我正在使用普通按钮的教程,然后我检查了如何做单选按钮,但这种方法对我根本不起作用。

MyFrame.java

package com.company;

import javax.swing.*;
import java.awt.*;

public class MyFrame extends JFrame {

    public MyFrame() {
        super("DES");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setSize(800,600);
        setLocationRelativeTo(null);
        setLayout(new FlowLayout());

        //add(new JButton("Przycisk 1"));
        //add(new JButton("Przycisk 2"));
        //add(new JButton("Przycisk 3"));

        //JPanel buttonPanel = new ButtonPanel();
        //add(buttonPanel);

        JPanel radioPanel = new RadioPanel();
        add(radioPanel);

        setVisible(true);
    }
}

RadioPanel.java

package com.company;

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

import javax.swing.*;

public class RadioPanel extends JPanel implements ActionListener{

    private JRadioButton modeDES;
    private JRadioButton mode3DES;

    public RadioPanel() {
        modeDES = new JRadioButton("DES");
        modeDES.setSelected(true);

        mode3DES = new JRadioButton("3DES");

        ButtonGroup desMode = new ButtonGroup();
        desMode.add(modeDES);
        desMode.add(mode3DES);
        modeDES.addActionListener(this);
        mode3DES.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();

        if(source == modeDES)
            System.out.println("DES");

        else if(source == mode3DES)
            System.out.println("3DES");
    }
}

我的窗口是白色的,根本没有按钮。

【问题讨论】:

    标签: java swing user-interface button radio


    【解决方案1】:

    您还需要将单选按钮添加到面板中。仅将它们添加到按钮组是不够的。按钮组的唯一目的是确保按钮组中只能选择一个单选按钮。

    这是您更正后的代码。请注意,我只添加了两行。

    package com.company;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class RadioPanel extends JPanel implements ActionListener{
    
        private JRadioButton modeDES;
        private JRadioButton mode3DES;
    
        public RadioPanel() {
            modeDES = new JRadioButton("DES");
            modeDES.setSelected(true);
    
            mode3DES = new JRadioButton("3DES");
    
            ButtonGroup desMode = new ButtonGroup();
            desMode.add(modeDES);
            desMode.add(mode3DES);
            modeDES.addActionListener(this);
            mode3DES.addActionListener(this);
            add(modeDES); // added this line
            add(mode3DES); // added this line
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            Object source = e.getSource();
    
            if(source == modeDES)
                System.out.println("DES");
    
            else if(source == mode3DES)
                System.out.println("3DES");
        }
    }
    

    请注意,我没有更改类MyFrame 的代码。我只换了班级RadioPanel

    我推荐[在线]教程Creating a GUI With JFC/Swing

    【讨论】:

      猜你喜欢
      • 2011-01-25
      • 2011-08-31
      • 1970-01-01
      • 2015-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-10
      相关资源
      最近更新 更多