Swing
用户界面开发工具包,比 AWT 更加高级一点,Swing 可以使用任何可插拔的外观风格 ,用很少的代码就可以创建优雅的用户界面,工具包中所有的包都是以swing作为名称
窗口
package JavaGUI;
import javax.swing.*;
import java.awt.*;
/**
* @Title: Test13JFrame
* @Package JavaGUI
* @Description:
* @author: maze
* @date 2020/10/20下午 13:57
*/
public class Test13JFrame {
public static void main(String[] args) {
new MyJFrame().init();
}
}
class MyJFrame extends JFrame{
public void init(){
// 获得一个容器
Container contentPane = this.getContentPane();
contentPane.setBackground(Color.red);
JLabel label = new JLabel("欢迎来到 Java !");
this.add(label);
// 水平居中标签
label.setHorizontalAlignment(SwingConstants.CENTER);
this.setVisible(true);
this.setBounds(1,1,200,200);
}
}
弹窗
JDialog 用来被弹出窗口
package JavaGUI;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @Title: Test15Dialog
* @Package JavaGUI
* @Description:
* @author: maze
* @date 2020/10/20下午 17:31
*/
public class Test15Dialog extends JFrame{
public Test15Dialog(){
this.setVisible(true);
this.setSize(700,500);
this.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 Test15Dialog();
}
}
// 弹窗的窗口
class MyDialogDemo extends JDialog{
public MyDialogDemo(){
this.setVisible(true);
this.setBounds(100,100,500,500);
Container container = this.getContentPane();
container.setLayout(null); //绝对定位
container.add(new Label("学 Java GUI 编程"));
}
}
标签
label
画了一个圆作为标签
public class Test16IconDemo1 extends JFrame implements Icon {
private int width;
private int height;
public Test16IconDemo1(){
}
public Test16IconDemo1(int width,int height){
this.width = width;
this.height = height;
}
public void init(){
Test16IconDemo1 iconDemo1 = new Test16IconDemo1(15,15);
// 图标放在标签上,也可以放在按钮上
JLabel jLabel = new JLabel("icontest",iconDemo1,SwingConstants.CENTER);
Container container = getContentPane()