java – 如何在另一个模态JDialog之上创建一个模态的JDialog
发布时间:2020-12-14 23:48:08 所属栏目:Java 来源:网络整理
导读:我有一个模式设置对话框是一个JDialog.在这个设置窗口中,我将一些组件(包括一个按钮)放置到另一个模式设置对话框,该对话框也是一个JDialog.我使他们成为JDialogs,因为这是我知道进行模态对话的唯一方法. 问题是这样的:当我创建主设置对话框时,我必须构建JDi
我有一个模式设置对话框是一个JDialog.在这个设置窗口中,我将一些组件(包括一个按钮)放置到另一个模式设置对话框,该对话框也是一个JDialog.我使他们成为JDialogs,因为这是我知道进行模态对话的唯一方法.
问题是这样的:当我创建主设置对话框时,我必须构建JDialog,无需父帧或父帧.由于我的主窗口是一个JFrame,我可以把它传递给主设置对话框的构造函数.但是当我想创建第二个模态设置对话框时,应该将主设置对话框作为父项,我找不到一个方法来获取JDialog的(J)框架.我确实希望通过这个主设置对话框作为父级,以便第二个设置对话框显示在它上面.让我们假设第二个设置对话框没有用于传递位置的构造函数,只是JDialog的构造函数. 有没有办法得到JDialog的(J)框架? 感谢您的帮助, 更新: public class CustomDialog extends JDialog { public CustomDialog(String title) { setModal(true); setResizable(false); setTitle(title); buildGUI(); } public Result showDialog(Window parent) { setLocationRelativeTo(parent); setVisible(true); return getResult(); } } 这也允许模态对话框中的模态对话框. 感谢你的帮助! 解决方法
不确定你有什么问题,但这里是一个例子,你可以有多个模态对话框:
import java.awt.BorderLayout; import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class TestDialog { protected static void initUI() { JPanel pane = newPane("Label in frame"); JFrame frame = new JFrame("Title"); frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); frame.add(pane); frame.pack(); frame.setVisible(true); } public static JPanel newPane(String labelText) { JPanel pane = new JPanel(new BorderLayout()); pane.add(newLabel(labelText)); pane.add(newButton("Open dialog"),BorderLayout.SOUTH); return pane; } private static JButton newButton(String label) { final JButton button = new JButton(label); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Window parentWindow = SwingUtilities.windowForComponent(button); JDialog dialog = new JDialog(parentWindow); dialog.setLocationRelativeTo(button); dialog.setModal(true); dialog.add(newPane("Label in dialog")); dialog.pack(); dialog.setVisible(true); } }); return button; } private static JLabel newLabel(String label) { JLabel l = new JLabel(label); l.setFont(l.getFont().deriveFont(24.0f)); return l; } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { initUI(); } }); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |