Java:GUI-Swing(上)
学习资料
b站狂神说:https://www.bilibili.com/video/BV1DJ411B75F
第一个窗口
package com.zy7y.gui.swing;
import javax.swing.*;
import java.awt.*;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui.swing
* @Author: zy7y
* @Date: 2020/8/16 下午1:30
* @Description: 第一个swing JFrame窗口
*/
public class TestJFrame {
public void init(){
// 顶级窗口
JFrame jFrame = new JFrame();
jFrame.setTitle("swing第一个窗口");
// 设置可见
jFrame.setVisible(true);
//设置尺寸
jFrame.setBounds(100,100,100,100);
// 设置标签
JLabel jLabel = new JLabel("文本内容");
jFrame.add(jLabel);
// 设置标签居中到容器上
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
// 容器实例化
Container container = jFrame.getContentPane();
container.setBackground(Color.green);
container.setBounds(50,50,20,20);
// 设置关闭事件
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJFrame().init();
}
}
JDialog弹窗
package com.zy7y.gui.swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui.swing
* @Author: zy7y
* @Date: 2020/8/16 下午1:45
* @Description: JDialog弹窗
*/
public class TestDialog {
public static void main(String[] args) {
new MyJFrame().init();
}
}
class MyJFrame extends JFrame{
public MyJFrame(){
super("JDialog弹窗");
}
public void init(){
setBounds(500,600,500,500);
//容器
Container container = getContentPane();
// 绝对布局
container.setLayout(null);
// 按钮
JButton jButton = new JButton("按钮1");
jButton.setBounds(30,50,200,200);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 弹窗
new MyDialog();
}
});
// 将button添加到容器内
container.add(jButton);
// 设置可见
setVisible(true);
}
}
class MyDialog extends JDialog{
public MyDialog(){
setBounds(200,200,200,200);
setBackground(Color.red);
setTitle("弹出框");
Container container = getContentPane();
container.setLayout(null);
JLabel jLabel = new JLabel("JavaSE-Swing-JDialog");
jLabel.setSize(200,300);
container.add(label);
setVisible(true);
}
}
标签
package com.zy7y.gui.swing;
import com.sun.javafx.tools.ant.Info;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.net.URL;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui.swing
* @Author: zy7y
* @Date: 2020/8/16 下午2:02
* @Description: 标签
*/
public class TestLabel {
public static void main(String[] args) {
new MyLabel().init();
}
}
class MyLabel extends JFrame {
public void init(){
setTitle("标签、图标");
setBounds(300,300,300,300);
Container container = getContentPane();
JLabel jLabel = new JLabel("标签");
// 设置图片,拿到路径对象
URL url = MyLabel.class.getResource("test.png");
// 图标
ImageIcon icon = new ImageIcon(url);
jLabel.setIcon(icon);
container.add(jLabel);
// 设置可关闭窗口
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
}
面板
package com.zy7y.gui.swing;
import javax.swing.*;
import java.awt.*;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui.swing
* @Author: zy7y
* @Date: 2020/8/16 下午2:22
* @Description: 面板
*/
public class TestJPanel extends JFrame {
public TestJPanel(){
setTitle("Swing-面板-JPanel");
Container container = getContentPane();
// 容器布局
container.setLayout(new GridLayout(2,1,10,10)); // 2行1列 上下间距各为10
// 实例化面板
JPanel jPanel = new JPanel();
jPanel.setLayout(new GridLayout(1,3)) ;
JPanel jPane2 = new JPanel(new GridLayout(3,3));
JPanel jPane3 = new JPanel(new GridLayout(2,2));
JButton jButton = new JButton("按钮1");
JButton jButton1 = new JButton("按钮2");
JButton jButton2 = new JButton("按钮3");
JButton jButton3 = new JButton("按钮4");
// 将按钮添加到面板
jPanel.add(jButton);
jPane2.add(jButton1);
jPane3.add(jButton3);
jPane3.add(jButton2);
// 将面板添加到容器
container.add(jPanel);
container.add(jPane2);
container.add(jPane3);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJPanel();
}
}
JScroll
package com.zy7y.gui.swing;
import javax.swing.*;
import java.awt.*;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui.swing
* @Author: zy7y
* @Date: 2020/8/16 下午2:33
* @Description: 文本域与JScroll
*/
public class TestJScroll extends JFrame {
public TestJScroll(){
setTitle("文本域与JScroll");
Container container = getContentPane();
// 文本域
JTextArea jTextArea = new JTextArea(20,50); // 20行 50列
jTextArea.setText("来写作文");
//JScroll面板: 带滚动条的面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
container.add(jScrollPane);
setVisible(true);
setBounds(200,200,600,600);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJScroll();
}
}