我有一个 JFrame
,里面有 2 个 JTextField
。现在我想让那个框架成为模态窗口,我该怎么做,请告诉我。
最佳答案
来自 Dialog 类的 javadoc
A dialog can be either modeless (the default) or modal. A modal dialog is one which blocks input to all other toplevel windows in the application, except for any windows created with the dialog as their owner.
public class AboutDialog extends JDialog implements ActionListener {
public AboutDialog(JFrame parent, String title, String message) {
super(parent, title, true);
if (parent != null) {
Dimension parentSize = parent.getSize();
Point p = parent.getLocation();
setLocation(p.x + parentSize.width / 4, p.y + parentSize.height / 4);
}
JPanel messagePane = new JPanel();
messagePane.add(new JLabel(message));
getContentPane().add(messagePane);
JPanel buttonPane = new JPanel();
JButton button = new JButton("OK");
buttonPane.add(button);
button.addActionListener(this);
getContentPane().add(buttonPane, BorderLayout.SOUTH);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
}
public static void main(String[] a) {
AboutDialog dlg = new AboutDialog(new JFrame(), "title", "message");
}
}
关于java - 制作框架模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1187271/