JDialog 出现时没有任何内容
JDialog appearing without any content
所以我一直遇到让我的 JDialog
出现的问题。我知道这不是一个新问题,但我仍然不能完全理解 EDT 的概念,Swing
中的并发。我希望有人能以一种简单的方式解释这一点(或向我指出一些解释得很好的资源)这是如何工作的以及我将如何在我的代码中包含它以使 JDialog
工作。
这是我正在阅读的有关并发的资源 http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html
这是我的代码:
public class TestView {
JFrame frame = new JFrame("Testing Radio Dialogs");
JPanel mainPanel = new JPanel();
JButton button = new JButton("Click me");
String[] folderNames = { "Java", "Ruby", "C++", "HTML" };
JRadioButton r11 = new JRadioButton(folderNames[0]);
JRadioButton r22 = new JRadioButton(folderNames[1]);
JRadioButton r33 = new JRadioButton(folderNames[2]);
JRadioButton r44 = new JRadioButton(folderNames[3]);
public TestView() {
frame.add(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(400, 400);
mainPanel.setLayout(new GridLayout(0, 1));
mainPanel.add(button);
button.addActionListener(new ButtonListener());
mainPanel.add(r11);
mainPanel.add(r22);
mainPanel.add(r33);
mainPanel.add(r44);
}
class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ev) {
if (ev.getSource() == button) {
createPopup();
System.out.println("Button clicked!");
}
}
}
public void createPopup() {
JDialog dialog = new JDialog();
JPanel dialogPanel = new JPanel(new GridLayout(0,1));
dialogPanel.setSize(150, 150);
JRadioButton r1 = new JRadioButton("Ruby");
JRadioButton r2 = new JRadioButton("Java");
JRadioButton r3 = new JRadioButton("C++");
JRadioButton r4 = new JRadioButton("HTML");
ButtonGroup group = new ButtonGroup();
group.add(r1);
group.add(r2);
group.add(r3);
group.add(r4);
dialogPanel.add(r1);
dialogPanel.add(r2);
dialogPanel.add(r3);
dialogPanel.add(r4);
dialog.setVisible(true);
System.out.println("Popup created!");
}
}
我在不同的论坛上浏览过类似的问题,但我仍然不太了解这个概念。对于此事和我的代码,我将不胜感激。
并发或分派线程没有问题,您只是忘记将 dialogPanel
添加到 dialog
。
public void createPopup() {
//...
dialog.add(dialogPanel);
dialog.pack();
dialog.setVisible();
//...
}
请注意,当您创建框架时,您是在框架可见后向 mainPanel 添加组件。默认情况下,所有组件的大小均为零,因此无需绘制任何内容。如果您的代码有效,我会感到很惊讶。
您的代码顺序应为:
panel.add(...);
panel.add(...);
frame.add(panel);
frame.pack();
frame.setVisible();
在使框架可见之前,应将所有组件添加到面板中。 pack() 将调用负责确定面板上组件的 size/location 的布局管理器。
setVisible(true) 应该是最后一条语句。
所以我一直遇到让我的 JDialog
出现的问题。我知道这不是一个新问题,但我仍然不能完全理解 EDT 的概念,Swing
中的并发。我希望有人能以一种简单的方式解释这一点(或向我指出一些解释得很好的资源)这是如何工作的以及我将如何在我的代码中包含它以使 JDialog
工作。
这是我正在阅读的有关并发的资源 http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html
这是我的代码:
public class TestView {
JFrame frame = new JFrame("Testing Radio Dialogs");
JPanel mainPanel = new JPanel();
JButton button = new JButton("Click me");
String[] folderNames = { "Java", "Ruby", "C++", "HTML" };
JRadioButton r11 = new JRadioButton(folderNames[0]);
JRadioButton r22 = new JRadioButton(folderNames[1]);
JRadioButton r33 = new JRadioButton(folderNames[2]);
JRadioButton r44 = new JRadioButton(folderNames[3]);
public TestView() {
frame.add(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(400, 400);
mainPanel.setLayout(new GridLayout(0, 1));
mainPanel.add(button);
button.addActionListener(new ButtonListener());
mainPanel.add(r11);
mainPanel.add(r22);
mainPanel.add(r33);
mainPanel.add(r44);
}
class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ev) {
if (ev.getSource() == button) {
createPopup();
System.out.println("Button clicked!");
}
}
}
public void createPopup() {
JDialog dialog = new JDialog();
JPanel dialogPanel = new JPanel(new GridLayout(0,1));
dialogPanel.setSize(150, 150);
JRadioButton r1 = new JRadioButton("Ruby");
JRadioButton r2 = new JRadioButton("Java");
JRadioButton r3 = new JRadioButton("C++");
JRadioButton r4 = new JRadioButton("HTML");
ButtonGroup group = new ButtonGroup();
group.add(r1);
group.add(r2);
group.add(r3);
group.add(r4);
dialogPanel.add(r1);
dialogPanel.add(r2);
dialogPanel.add(r3);
dialogPanel.add(r4);
dialog.setVisible(true);
System.out.println("Popup created!");
}
}
我在不同的论坛上浏览过类似的问题,但我仍然不太了解这个概念。对于此事和我的代码,我将不胜感激。
并发或分派线程没有问题,您只是忘记将 dialogPanel
添加到 dialog
。
public void createPopup() {
//...
dialog.add(dialogPanel);
dialog.pack();
dialog.setVisible();
//...
}
请注意,当您创建框架时,您是在框架可见后向 mainPanel 添加组件。默认情况下,所有组件的大小均为零,因此无需绘制任何内容。如果您的代码有效,我会感到很惊讶。
您的代码顺序应为:
panel.add(...);
panel.add(...);
frame.add(panel);
frame.pack();
frame.setVisible();
在使框架可见之前,应将所有组件添加到面板中。 pack() 将调用负责确定面板上组件的 size/location 的布局管理器。
setVisible(true) 应该是最后一条语句。