我必须在 JDialog 中显示 Jpanel,到目前为止我已经处理过它,但是我不知道如何关闭对话框并在处理它之前接收 Jpanel 中的数据。
newButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{
JDialog dialog = new JDialog(Main.getMainFrame(), true);
JPanel jPanel = new JPanel();
dialog.getContentPane().add(jPanel);
dialog.setMinimumSize(new Dimension(600, 800));
dialog.setVisible(true);
if (jPanel.close)
{
/*read some useful information from the jpanel*/
dialog.setVisible(false);
dialog.dispose();
}
}
});
到目前为止,我已经在 Jpanel 中放置了一个按钮,该按钮设置了 boolean 值 close = true,并且对话框会读取它并自行处理。但这不起作用:我怀疑在 JPanel 内单击关闭之前测试了 jPanel.close()
,而它应该等待实际关闭值更改。
最佳答案
您可以使用这样的代码来获取容器窗口实例(在我们的例子中为JDialog
)
JDialog parentDialog=(JDialog)SwingUtilities.getWindowAncestor(jPanel);
关于java - 关闭前从 JDialog 内的 JPanel 读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14579329/