我正在使用 swing 库学习 java GUI。我知道如何制作 JFrame
并添加 JButton
、添加 ActionListener
等,但今天 JFrame
没有显示。我一切如常。请看一下我的代码并建议我哪里做错了..
import javax.swing.*;
import java.awt.*;
import java.util.concurrent.TimeUnit;
public class MyGroup extends JFrame {
private ButtonGroup myGroup = new ButtonGroup();
public MyGroup(){
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
JRadioButton b1 = new JRadioButton("check1");
JRadioButton b2 = new JRadioButton("check2");
JRadioButton b3 = new JRadioButton("check3");
JRadioButton b4 = new JRadioButton("check4");
add(b1);
add(b2);
add(b3);
add(b4);
myGroup.add(b1);
myGroup.add(b2);
myGroup.add(b3);
myGroup.add(b4);
setVisible(true);
}
public static void main(String[] args) throws Exception{
ButtonGroup m = new ButtonGroup();
}
}
最佳答案
您正在 main 方法中创建一个新的 ButtonGroup
,而不是新的 MyGroup
。我想,由于后者执行你的构造函数并使你的框架可见,这就是你的错误所在!
关于java - JFrame:无进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17807686/