Swing
窗口 面板 Jframe
package com.swing.jframe;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo01 {
public static void main(String[] args) {
new MyJFrame2().init();
}
}
class MyJFrame2 extends JFrame{
public void init(){
this.setVisible(true);
this.setBounds(10,10,200,300);
JLabel label = new JLabel("welcome ");
this.add(label);
// center text
label.setHorizontalAlignment(SwingConstants.CENTER);
// get a container
Container container = this.getContentPane();
container.setBackground(Color.green);
}
}
需要使用 container,否则不会生效
弹窗 JDialog
用来被弹出,默认就有关闭事件
package com.swing.dialog;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DialogDemo extends JFrame {
public DialogDemo(){
this.setVisible(true);
this.setSize(700,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// dialog can be closed initially
// JFrame container
Container container = this.getContentPane();
// absolute layout
container.setLayout(null);
// buttom
JButton button = new JButton("pop up dialog");
button.setBounds(30,30,200,50);
// pop up dialog when clicked on the button
// add to container
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// pop-ups
new MyDialog();
}
});
container.