GUI编程--03--Swing

Posted 高高for 循环

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GUI编程--03--Swing相关的知识,希望对你有一定的参考价值。

Swing 窗口

import javax.swing.*;
import java.awt.*;

public class TestJFrame {

    public static void main(String[] args) {
        //建立一个窗口
        new TestJFrame().init();
    }


    //init();初始化
    public void init(){

        //顶级窗口
        JFrame jf = new JFrame("这是一个JFram窗口");
        jf.setVisible(true);
        jf.setBounds(100,100,200,200);


        //设置文字Jlabel
        JLabel label = new JLabel("加油!!!");
        jf.add(label);

        //居中
        label.setHorizontalAlignment(SwingConstants.CENTER);


        //容器实例化
        Container contentPane = jf.getContentPane();
        contentPane.setBackground(Color.green);

        //关闭事件
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

在这里插入图片描述

容器实例化

//容器实例化

Container contentPane = jf.getContentPane();

弹窗 JDialog

package swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class TestDialog {
    public static void main(String[] args) {
        new Dialog1();
    }
}


//主窗口
class Dialog1 extends JFrame{
    public Dialog1(){

        this.setVisible(true);
        this.setBounds(100,100,300,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //Jframe放东西,容器
        Container container = this.getContentPane();
        //绝对布局
        container.setLayout(null);
        //按钮
        JButton jButton = new JButton("弹出对话框");//创建
        jButton.setBounds(30,30,200,50);
        container.add(jButton);
        //点击这个按钮的时候,弹出一个弹窗(监听事件)
        jButton.addActionListener(new AbstractAction() {  //监听器
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new Dialog2();
            }
        });
        container.add(jButton);
    }
}
//弹窗的窗口
class Dialog2 extends JDialog{
    public Dialog2(){
        this.setVisible(true);
        this.setBounds(100,100,500,500);
        //弹窗中默认有此事件
        //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container container = this.getContentPane();
        container.setLayout(null);
        container.setBackground(Color.PINK);


        Label label = new Label("fighting !!!");
        label.setBounds(100,100,100,100);
        container.add(label);
    }
}

在这里插入图片描述

标签/图片

label

new Lavel(“xxx”);

new JLavel(“xxx”);

Icon 图标

import javax.swing.*;
import java.awt.*;

public class TestIcon {
    public static void main(String[] args) {
        new Icon1(100,100).init();
    }
}


class Icon1 extends JFrame implements Icon{
    //属性
    private int width;
    private int high;

    public Icon1(){}//无参构造

    public Icon1(int width,int high){
        this.width = width;
        this.high = high;
    }
    //方法
    public void init(){
        setBounds(100,100,300,300);
        Icon1 icon1 = new Icon1(15,15);
        //图标放在标签上,也可以放在按钮上
        JLabel jLabel = new JLabel("gaogao",icon1,SwingConstants.CENTER);
        
        Container container = getContentPane();
        container.add(jLabel);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.fillOval(x,y,width,high);
    }
    @Override
    public int getIconWidth() {
        return this.width;
    }
    @Override
    public int getIconHeight() {
        return this.high;
    }
}

在这里插入图片描述

ImageIcon 图片

package swing;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class TestImageIcon {

    public static void main(String[] args) {
        new TestImageIcon();
    }


    public TestImageIcon()  {
        JFrame jFrame = new JFrame();
        //获取图片地址     url是一个具体的地址
        JLabel jLabel = new JLabel("ImageIcon");
        URL url = TestImageIcon.class.getResource("gougou.jpg");//获取当前类下的资源
        ImageIcon imageIcon = new ImageIcon(url);//命名避免冲突
        jLabel.setIcon(imageIcon);//图片饭在jLabel上
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        Container container =jFrame.getContentPane();
        container.add(jLabel);
        jFrame.setVisible(true);
        jFrame. setBounds(100,100,300,300);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }


}

在这里插入图片描述

面板 JPanel

import javax.swing.*;
import java.awt.*;

public class TestJPane extends JFrame{
    public TestJPane()  {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(2,1,10,10));  //间距
        JPanel jPanel = new JPanel(new GridLayout(1,3));
        JPanel jPanel1 = new JPanel(new GridLayout(1,3));
        JPanel jPanel2 = new JPanel(new GridLayout(1,3));
        JPanel jPanel3 = new JPanel(new GridLayout(1,3));
        jPanel.add(new JButton("1"));
        jPanel.add(new JButton("2"));
        jPanel.add(new JButton("3"));
        jPanel1.add(new JButton("1"));
        jPanel1.add(new JButton("2"));
        jPanel1.add(new JButton("3"));
        jPanel2.add(new JButton("1"));
        jPanel2.add(new JButton("2"));
        jPanel2.add(new JButton("3"));
        jPanel3.add(new JButton("1"));
        jPanel3.add(new JButton("2"));
        jPanel3.add(new JButton("3"));
        container.add(jPanel);
        container.add(jPanel1);
        container.add(jPanel2);
        container.add(jPanel3);
        setVisible(true);
        setBounds(100,100,300,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestJPane();
    }
}

在这里插入图片描述

JScrollPonel 带有滚动条的 卷轴面板

import javax.swing.*;
import java.awt.*;

public class JScrollDemo extends JFrame {
    public JScrollDemo(){
        Container container = this.getContentPane();
        //文本域
        JTextArea textArea = new JTextArea(200 ,400);
        textArea.setText("待到秋来九月八 我花开后百花杀!!!");
        //Scroll面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);
        this.setBounds(100,100,400,400);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JScrollDemo();
    }
}

在这里插入图片描述

按钮

普通按钮 JButton

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class TestJButton extends JFrame {
    public TestJButton() {
        Container container = this.getContentPane();
        //将图片变为一个图标
        URL url = TestJButton.class.getResource("gougou.jpg");
        ImageIcon icon = new ImageIcon(url);
        //把图片放在按钮上
        JButton jButton = new JButton();
        jButton.setIcon(icon);
        jButton.setSize(100,100);
        jButton.setToolTipText("图片按钮");

        //add
        container.add(jButton);
        setVisible(true);
        setBounds(100,100,300,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestJButton();
    }
}

在这里插入图片描述

单选 JRadioButton

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class TestJButton extends JFrame {
    public TestJButton() {
        Container container = this.getContentPane();

        //单选框
        JRadioButton jRadioButton1 = new JRadioButton("1");
        JRadioButton jRadioButton2 = new JRadioButton("2");
        JRadioButton jRadioButton3 = new JRadioButton("3");
        //由于单选框只能选一个所以我们要分组,一个组中只能选一个
        ButtonGroup group = new ButtonGroup();
        group.add(jRadioButton1);
        group.add(jRadioButton2);
        group.add(jRadioButton3);
        
        container.add(jRadioButton1,BorderLayout.CENTER);
        container.add(jRadioButton2,BorderLayout.NORTH);
        container.add(jRadioButton3,BorderLayout.SOUTH);


        setVisible(true);
        setBounds(100,100,300,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestJButton();
    }
}

在这里插入图片描述

复选 JCheckBox

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class TestJButton extends JFrame {
    public TestJButton() {
        Container container = this.getContentPane();

        JCheckBox jCheckBox = new JCheckBox("1");
        JCheckBox jCheckBox1 = new JCheckBox("2");

        container.VSCode自定义代码片段——JS中的面向对象编程

VSCode自定义代码片段9——JS中的面向对象编程

QT 实用代码片段

使用 Pygments 检测代码片段的编程语言

面向面试编程代码片段之GC

如何在 Django Summernote 中显示编程片段的代码块?