Swing
Jframe窗体
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//init() 初始化
public void init(){
JFrame frame = new JFrame("这是一个JFrame窗口");
frame.setVisible(true);
frame.setBounds(100, 100, 200, 200);
//frame.setBackground(Color.pink);
//设置一个容器
frame.getContentPane().setBackground(Color.pink);
//设置文字
JLabel jLabel = new JLabel("阿涛来了");
frame.add(jLabel);
//让文本标签水平居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
//关闭事件
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo().init();
}
}
JDialog弹窗
弹窗默认有关闭事件!
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class DialogDemo extends JFrame {
public static void main(String[] args) {
new DialogDemo();
}
public DialogDemo() {
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(600, 600);
//放东西,容器
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("点击弹出一个对话框");
button.setBounds(30, 30, 200, 60);
//监听器
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialog();
}
});
container.add(button);
}
}
//弹窗的窗口
class MyDialog extends JDialog{
public MyDialog() {
this.setVisible(true);
this.setBounds(100,100,500,500);
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
container.add(new Label("阿涛学JAVA"));
}
}
标签
Icon标签:
import javax.swing.*;
import java.awt.*;
public class IconDemo extends JFrame implements Icon