/* 正确代码 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//加不加this都行
public class DialogDemo extends JFrame {
public DialogDemo(){
setVisible(true);
setSize(700,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
JButton button = new JButton("点击弹出一个对话框");
button.setBounds(30,30,200,50);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new MyDialogdemo();
}
});
container.add(button);
}
public static void main(String[] args) {
new DialogDemo();
}
}
class MyDialogdemo extends JDialog{
public MyDialogdemo(){
setVisible(true);
//删除了this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);,因为主类里面已经有这行代码了,这行代码需要省略
setBounds(100,100,500,500);
Container container=this.getContentPane();
container.setLayout(new FlowLayout());//将container.setLayout(null);改成了container.setLayout(new FlowLayout());,没有布局就不会显示组件
container.add(new JLabel("我只是试一下啊"));
}
}
|