使用Java语言在JFrame窗口中创建一个按钮当按下按钮后创建一个继承于dialog的类的对象并传入的构造函数的参数为null的一个对话框的代码:
时间: 2024-03-12 20:43:17 浏览: 101
好的,以下是Java代码实现:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MainFrame extends JFrame {
private JButton button;
public MainFrame() {
super("Main Frame");
setLayout(new FlowLayout());
button = new JButton("Create Dialog");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MyDialog dialog = new MyDialog(null);
dialog.setVisible(true);
}
});
add(button);
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
MainFrame frame = new MainFrame();
frame.setVisible(true);
}
}
class MyDialog extends Dialog {
public MyDialog(Frame parent) {
super(parent, "My Dialog", true);
setSize(200, 100);
setLocationRelativeTo(null);
}
}
```
在这个例子中,我们首先创建了一个JFrame窗口,添加了一个按钮。当按钮被按下时,我们创建了一个继承于Dialog的类MyDialog的对象,并传入的构造函数的参数为null,即没有父窗口。最后,我们将对话框设为可见状态。
需要注意的是,我们在MyDialog类的构造函数中使用了super(parent, "My Dialog", true)来调用父类的构造函数,其中第一个参数为父窗口,这里传入的是null,表示没有父窗口,第二个参数为对话框的标题,第三个参数为是否为模态对话框,这里传入的是true,表示是模态对话框。
阅读全文