JDialog有哪些构造方法

时间: 2023-08-11 18:04:22 浏览: 57
JDialog 类具有以下几种构造方法: 1. `JDialog()`: 创建一个默认的JDialog,无标题、无父组件。 2. `JDialog(Dialog owner)`: 创建一个无标题的JDialog,指定父对话框。 3. `JDialog(Dialog owner, boolean modal)`: 创建一个无标题的JDialog,指定父对话框和模态性。 4. `JDialog(Dialog owner, String title)`: 创建一个具有指定标题的JDialog,指定父对话框。 5. `JDialog(Dialog owner, String title, boolean modal)`: 创建一个具有指定标题的JDialog,指定父对话框和模态性。 6. `JDialog(Frame owner)`: 创建一个无标题的JDialog,指定父窗口。 7. `JDialog(Frame owner, boolean modal)`: 创建一个无标题的JDialog,指定父窗口和模态性。 8. `JDialog(Frame owner, String title)`: 创建一个具有指定标题的JDialog,指定父窗口。 9. `JDialog(Frame owner, String title, boolean modal)`: 创建一个具有指定标题的JDialog,指定父窗口和模态性。 这些构造方法可以根据需要选择适合的构造方法来创建 JDialog 对象,并设置父组件、标题和模态性等属性。
相关问题

对JDialog的关闭键监听

要在JDialog中对关闭键(即窗口的关闭按钮)进行监听,也可以使用WindowListener接口来实现。以下是一个简单的示例代码: ```java public class MyDialog extends JDialog implements WindowListener { public MyDialog(Frame owner, String title, boolean modal) { super(owner, title, modal); // 在构造方法中添加监听器 addWindowListener(this); } // 实现WindowListener接口中的方法 @Override public void windowClosing(WindowEvent e) { // 在窗口关闭时执行的操作 System.out.println("窗口关闭了!"); // 可以在这里添加关闭窗口的代码 } @Override public void windowOpened(WindowEvent e) {} @Override public void windowClosed(WindowEvent e) {} @Override public void windowIconified(WindowEvent e) {} @Override public void windowDeiconified(WindowEvent e) {} @Override public void windowActivated(WindowEvent e) {} @Override public void windowDeactivated(WindowEvent e) {} } ``` 在上面的示例代码中,MyDialog类继承自JDialog类,并实现了WindowListener接口。在构造方法中添加了对自身的监听。当窗口关闭时,会执行windowClosing方法中的代码。在此方法中,可以添加关闭窗口的操作。同时,需要实现接口中的其他方法,但这些方法可以空实现,不必添加任何代码。

使用JDialog,参照Windows记事本编写一个“字体”对话框

好的,我会尽可能详细地回答你的问题。首先,JDialog是Java中的一个类,用于创建一个对话框窗口,它可以包含用户输入的表单、文本框、按钮等组件。在这个字体对话框的例子中,我们会使用JDialog来打造一个带有各种字体、字号、字形等选项的自定义字体面板。下面是代码实现: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class FontDialog extends JDialog implements ActionListener { private static final long serialVersionUID = 1L; private JList<String> fontList; private JList<String> sizeList; private JCheckBox boldCheck; private JCheckBox italicCheck; private JTextField previewText; private JButton okButton; private JButton cancelButton; private String selectedFontName; private int selectedFontSize; private boolean isBold; private boolean isItalic; public FontDialog(JFrame parentFrame) { super(parentFrame, "Select Font", true); setSize(400, 400); setResizable(false); setLocationRelativeTo(parentFrame); selectedFontName = ""; selectedFontSize = 0; isBold = false; isItalic = false; JPanel mainPanel = new JPanel(new BorderLayout(10, 10)); mainPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); JPanel fontPanel = new JPanel(new BorderLayout(10, 10)); fontPanel.setPreferredSize(new Dimension(200, 0)); fontList = new JList<>(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames()); fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); JScrollPane fontScroll = new JScrollPane(fontList); fontPanel.add(fontScroll, BorderLayout.CENTER); JPanel sizePanel = new JPanel(new BorderLayout(10, 10)); sizePanel.setPreferredSize(new Dimension(70, 0)); String[] sizes = {"8", "10", "12", "14", "16", "18", "20", "24", "28", "32", "36", "48", "72"}; sizeList = new JList<>(sizes); sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); JScrollPane sizeScroll = new JScrollPane(sizeList); sizePanel.add(sizeScroll, BorderLayout.CENTER); JPanel stylePanel = new JPanel(new GridLayout(2, 1, 5, 5)); boldCheck = new JCheckBox("Bold"); italicCheck = new JCheckBox("Italic"); stylePanel.add(boldCheck); stylePanel.add(italicCheck); JPanel settingsPanel = new JPanel(new BorderLayout(10, 10)); settingsPanel.setBorder(new TitledBorder("Settings")); settingsPanel.add(fontPanel, BorderLayout.CENTER); settingsPanel.add(sizePanel, BorderLayout.EAST); settingsPanel.add(stylePanel, BorderLayout.SOUTH); previewText = new JTextField("The quick brown fox jumps over the lazy dog."); previewText.setEditable(false); previewText.setBorder(new CompoundBorder(BorderFactory.createLineBorder(Color.BLACK), new EmptyBorder(5, 5, 5, 5))); previewText.setBackground(Color.WHITE); JPanel buttonsPanel = new JPanel(new GridLayout(1, 2, 10, 10)); okButton = new JButton("OK"); okButton.addActionListener(this); cancelButton = new JButton("Cancel"); cancelButton.addActionListener(this); buttonsPanel.add(okButton); buttonsPanel.add(cancelButton); mainPanel.add(settingsPanel, BorderLayout.CENTER); mainPanel.add(previewText, BorderLayout.SOUTH); mainPanel.add(buttonsPanel, BorderLayout.SOUTH); setContentPane(mainPanel); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == cancelButton) { dispose(); } else if (e.getSource() == okButton) { selectedFontName = fontList.getSelectedValue(); selectedFontSize = Integer.parseInt(sizeList.getSelectedValue()); isBold = boldCheck.isSelected(); isItalic = italicCheck.isSelected(); dispose(); } } public String getSelectedFontName() { return selectedFontName; } public int getSelectedFontSize() { return selectedFontSize; } public boolean isBold() { return isBold; } public boolean isItalic() { return isItalic; } } ``` 这个类继承了JDialog类,并实现了ActionListener接口,用于处理窗口中的按钮点击事件。在构造函数中,我们先设置一些初始值,然后创建了一个JPanel作为主面板,并设置了其布局和边框。接着创建了三个子面板分别放置字体、字号、字形等选项,并通过JScrollPane将它们包装成可滚动的列表。然后我们创建了一个JTextField作为字体预览区域,并设置了它的边框和背景色。 最后,我们创建了两个按钮,分别为OK和Cancel,并通过GridLayout布局将它们放置在一个JPanel中。我们将OK按钮的ActionListener设置为当前类,用于处理用户点击OK按钮的事件。当用户点击OK按钮时,我们通过getter函数获取用户所选择的字体、字号、字形等选项,并保存到成员变量中。然后调用dispose()方法将窗口关闭。当用户点击Cancel按钮时,我们只需要将窗口关闭即可,因此在ActionListener中我们只需要判断事件来源即可。 你可以在你的应用程序中实例化FontDialog类,并在需要的时候弹出字体对话框。比如,你可以在菜单中添加一个“选择字体”选项,点击后弹出字体对话框,并将用户选择的字体信息保存下来。另外,你还可以将用户选择的字体信息应用到你的其他组件上,比如文本框、标签等。

相关推荐

package project; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.SQLException; import javax.swing.*; public class AddBookDialog extends JDialog{ final int WIDTH=400; final int HEIGHT=300; public AddBookDialog(JFrame jf,String title,boolean isModel) { super(jf,title,isModel); this.setBounds(HEIGHT, HEIGHT, WIDTH, HEIGHT); Box vBox=Box.createVerticalBox(); Box numBox=Box.createHorizontalBox(); JLabel numLabel=new JLabel("学号:"); JTextField numField=new JTextField(15); numBox.add(numLabel); numBox.add(Box.createHorizontalStrut(20)); numBox.add(numField); Box nameBox=Box.createHorizontalBox(); JLabel nameLabel=new JLabel("姓名:"); JTextField nameField=new JTextField(15); nameBox.add(nameLabel); nameBox.add(Box.createHorizontalStrut(20)); nameBox.add(nameField); Box sexBox=Box.createHorizontalBox(); JLabel sexLabel=new JLabel("性别:"); JTextField sexField=new JTextField(15); sexBox.add(sexLabel); sexBox.add(Box.createHorizontalStrut(20)); sexBox.add(sexField); Box ageBox=Box.createHorizontalBox(); JLabel ageLabel=new JLabel("年龄:"); JTextField ageField=new JTextField(15); ageBox.add(ageLabel); ageBox.add(Box.createHorizontalStrut(20)); ageBox.add(ageField); Box teBox=Box.createHorizontalBox(); JLabel teLabel=new JLabel("电话:"); JTextField teField=new JTextField(15); teBox.add(teLabel); teBox.add(Box.createHorizontalStrut(20)); teBox.add(teField); Box idBox=Box.createHorizontalBox(); JLabel idLabel=new JLabel("房间号:"); JTextField idField=new JTextField(15); idBox.add(idLabel); idBox.add(Box.createHorizontalStrut(20)); idBox.add(idField); Box btnBox=的问题

最新推荐

recommend-type

java经典面试2010集锦100题(不看你后悔)

C) 子类继承父类,如果父类中只有一个构造方法并且有参数,那么在子类的构造方法中必须先用super调用父类构造方法并传参。 D) 以上说法都不正确。 题目11:d 程序如下: class Test { private int day; public int...
recommend-type

H11111111111111111111111111111111111111111111111111111

sadfasfdgsdgsdfgsdgsdg
recommend-type

16路舵机机械臂.zip16路舵机机械臂_.zip

16路舵机机械臂.zip16路舵机机械臂_
recommend-type

Python_来自微软的熟悉的、友好的、现代的表情符号集合.zip

Python_来自微软的熟悉的、友好的、现代的表情符号集合
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MySQL数据库性能提升秘籍:揭秘性能下降幕后真凶及解决策略

![MySQL数据库性能提升秘籍:揭秘性能下降幕后真凶及解决策略](http://xiaoyuge.work/explain-sql/index/2.png) # 1. MySQL数据库性能优化概述** MySQL数据库性能优化是一项至关重要的任务,可以显著提高应用程序的响应时间和整体用户体验。优化涉及识别和解决影响数据库性能的因素,包括硬件资源瓶颈、软件配置不当和数据库设计缺陷。通过采取适当的优化策略,可以显著提升数据库性能,满足业务需求并提高用户满意度。 # 2. MySQL数据库性能下降的幕后真凶 ### 2.1 硬件资源瓶颈 #### 2.1.1 CPU利用率过高 **症状:
recommend-type

如何在unity创建按钮

在 Unity 中创建按钮的步骤如下: 1. 在 Unity 中创建一个 UI Canvas,选择 GameObject -> UI -> Canvas。 2. 在 Canvas 中创建一个按钮,选择 GameObject -> UI -> Button。 3. 在场景视图中调整按钮的位置和大小。 4. 在 Inspector 中设置按钮的文本、颜色、字体等属性。 5. 添加按钮的响应事件,选择按钮,在 Inspector 的 On Click () 中添加相应的方法。 这样就可以创建一个按钮了,你可以在游戏中使用它来触发相应的操作。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依