Swing组件基础-----窗口、面板、弹窗
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class DialogDemo extends JFrame{
public static void main(String[] args) {
new MyDialogDemo().init();
}
}
//主窗口
class MyDialogDemo extends JFrame{
public void init() {
this.setVisible(true);
this.setSize(280,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame 放东西,容器
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 MyDialogDemo1();
}
});
container.add(button);
}
}
//弹窗的窗口
class MyDialogDemo1 extends JDialog{
public MyDialogDemo1() {
this.setVisible(true);
this.setBounds(100,100,200,200);
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//设置文字 JLabel
JLabel label = new JLabel("Steven带你练Java");
this.add(label);
//让文本标签居中 设置水平对齐
label.setHorizontalAlignment(SwingConstants.CENTER);
//获得一个容器
Container container = this.getContentPane();
container.setBackground(Color.YELLOW);
}
}
效果:

问题:将主窗口做内部类处理,不报错但无运行结果,不明原因!

浙公网安备 33010602011771号