我无法在Java Swing中看到单选按钮。

huangapple go评论60阅读模式
英文:

I cannot see Radio buttons in Java Swing

问题

我有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");
        }
    }

我得到了一个空白的窗口根本没有按钮
英文:

I have 2 buttons and one button group for them, but I cannot see them in app window. Normal buttons work for me, but not the radio ones. I was using tutorial for normal buttons and then I have checked out how to do radio ones, but this method doesn't work for me at all.

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");
}
}

I have white window, no buttons at all.

答案1

得分: 4

以下是您要翻译的内容:

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

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

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); // 添加了这一行
        add(mode3DES); // 添加了这一行
    }

    @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类。

我推荐这个在线教程:使用 JFC/Swing 创建 GUI

英文:

You also need to add the radio buttons to the panel. Just adding them to the button group is not enough. The only purpose of the button group is to ensure that only one radio button in the button group can be selected.

Here is your corrected code. Note that I only added two lines.

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");
    }
}

Note that I did not change the code of class MyFrame. I only changed class RadioPanel.

I recommend the [online] tutorial Creating a GUI With JFC/Swing

huangapple
  • 本文由 发表于 2020年8月25日 01:38:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63566038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定