📜  Java Swing-JDialog

📅  最后修改于: 2020-09-30 06:43:33             🧑  作者: Mango

Java J对话框

JDialog控件表示一个带有边框和标题的顶级窗口,用于从用户那里获取某种形式的输入。它继承了Dialog类。

与JFrame不同,它没有最大化和最小化按钮。

JDialog类声明

我们来看一下javax.swing.JDialog类的声明。

public class JDialog extends Dialog implements WindowConstants, Accessible, RootPaneContainer

常用的构造函数:

Constructor Description
JDialog() It is used to create a modeless dialog without a title and without a specified Frame owner.
JDialog(Frame owner) It is used to create a modeless dialog with specified Frame as its owner and an empty title.
JDialog(Frame owner, String title, boolean modal) It is used to create a dialog with the specified title, owner Frame and modality.

Java JDialog示例

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DialogExample {
private static JDialog d;
DialogExample() {
JFrame f= new JFrame();
d = new JDialog(f , "Dialog Example", true);
d.setLayout( new FlowLayout() );
JButton b = new JButton ("OK");
b.addActionListener ( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
DialogExample.d.setVisible(false);
}
});
d.add( new JLabel ("Click button to continue."));
d.add(b); 
d.setSize(300,300);  
d.setVisible(true);
}
public static void main(String args[])
{
new DialogExample();
}
}

输出: