50,523
社区成员
发帖
与我相关
我的任务
分享
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyJDialog extends JDialog { // 创建新类继承JDialog类
public MyJDialog() {
// 实例化一个JDialog类对象,指定对话框的父窗体、窗体标题和类型
super(new MyJFrame(), true);
Container cp = getContentPane(); // 创建一个容器
cp.add(new JLabel("对话框")); // 在容器中添加标签
setSize(100, 100);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.show();//////////////////////////////////////////////////////////////////////
}
public static void main(String[] args) {
new MyJDialog();
}
}
class MyJFrame extends JFrame {
public MyJFrame() {
Container cp = getContentPane();
JLabel jl = new JLabel("这是个JFrame窗体");
cp.add(jl);
JButton jb = new JButton("Press me");
cp.add(jb);
jb.addActionListener(new ActionListener() { // 为按钮添加鼠标单击事件.
public void actionPerformed(ActionEvent arg0) {
new MyJDialog().setVisible(true);
}
});
this.setVisible(true);////////////////////////////////////////////////////////////////
}
}