所以我在显示 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();
//...
}
关于java - JDialog 出现时没有任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29651612/