java - 制作框架模态

标签 java swing

我有一个 JFrame,里面有 2 个 JTextField。现在我想让那个框架成为模态窗口,我该怎么做,请告诉我。

最佳答案

Simple Modal Dialog

来自 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/

相关文章:

Java - 接口(interface)是否过多?

java - 对 Swing 组件的反射(reflection)

java - 不能使用java子类的public方法

java - 如何即时更改 Swing JFrame 大小

java - JButton 不执行任何操作

java - 读取文本字段数组

Java InputVerifier查找导致 "focus lost"的组件

java - 为什么我的sql代码在结果集中无法正常运行

java - 方法密集型 Java 对象的内存占用是多少?

java - 检查变量是否等于/(不等于)null 是否正确?取决于语境?