GUI学习之Swing

GUI编程之Swing

1.窗口、面板(JFrame)

package day03;

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

public class JFrameTest {
    
    
    public static void main(String[] args) {
    
    
        //创建一个jFrame
        JFrame jFrame_test = new JFrame("JFrame test");

        //设置窗口大小
        jFrame_test.setBounds(100, 100, 500, 500);

        //new一个label
        Label hello_world = new Label("hello world");
        hello_world.setAlignment(Label.CENTER);

        //把窗口变换为一个容器之后,加上背景颜色
        Container contentPane = jFrame_test.getContentPane();
        contentPane.setBackground(Color.BLUE);

        //将label嵌入到窗口中去
        jFrame_test.add(hello_world);

        //设置可见性
        jFrame_test.setVisible(true);

        //关闭窗口
        jFrame_test.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

2.弹窗(dialog)

package day03;

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


public class JDialogTest {
    
    
    public static void main(String[] args) {
    
    
        JFrame jf = new JFrame("弹窗测试");

        //设置窗口的大小和位置
        jf.setBounds(100, 100, 500, 500);
        //设置可见性
        jf.setVisible(true);
        Container contentPane1 = jf.getContentPane();

        JButton button = new JButton("点击弹出一个弹框");
        button.setBounds(10, 10, 200, 50);
        button.addActionListener(new ActionListener() {
    
    
            @Override
            public void actionPerformed(ActionEvent e) {
    
    
                new MyDialog2();
            }
        });
        contentPane1.add(button);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

class MyDialog2 extends JDialog {
    
    
    public MyDialog2() {
    
    
        setVisible(true);
        setBounds(100, 100, 200, 200);
        Container contentPane = getContentPane();
        contentPane.setLayout(null);
    }
}

3.标签

package day03;

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


//图标,需要实现类,JFrame继承
public class IconTest extends JFrame implements Icon {
    
    
    private int width;
    private int height;

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

    public IconTest(int width, int height) {
    
    
        this.width = width;
        this.height = height;
    }

    public void init() {
    
    
        IconTest iconTest = new IconTest(30, 30);
        //图标放在标签上,也可以放在按钮上
        JLabel icon_test = new JLabel("icon_test", iconTest, SwingConstants.CENTER);


        Container contentPane = getContentPane();
        contentPane.add(icon_test);

        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,height);
    }

    @Override
    public int getIconWidth() {
    
    
        return this.width;
    }

    @Override
    public int getIconHeight() {
    
    
        return this.height;
    }

    public static void main(String[] args) {
    
    
        new IconTest().init();
    }
}

4.面板

package day03;

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

public class JPanelTest extends JFrame {
    
    
    public JPanelTest() {
    
    
        Container contentPane = getContentPane();
        contentPane.setLayout(new GridLayout(1, 1));

        JPanel jPanel = new JPanel(new GridLayout(1, 3));

        jPanel.add(new JButton("1"));
        jPanel.add(new JButton("2"));
        jPanel.add(new JButton("3"));

        contentPane.add(jPanel);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setBounds(100, 100, 500, 500);
    }

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

JScroll面板

package day03;

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

public class JScrollTest extends JFrame {
    
    
    public JScrollTest() {
    
    
        //创建一个容器
        Container contentPane = getContentPane();

        //创建一个文本域
        JTextArea jTextArea = new JTextArea(20, 50);
        jTextArea.setText("hello world");

        //创建一个Scroll面板
        JScrollPane jScrollPane = new JScrollPane(jTextArea);
        contentPane.add(jScrollPane);

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setBounds(100,100,500,500);
    }

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

5.按钮

  • 单选按钮:只能选其中一个按钮点击
  • 复选按钮:可以点击多个
package day03;

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

public class JButtonTest extends JFrame {
    
    
    public JButtonTest() {
    
    
        Container contentPane = this.getContentPane();

        //将图片变为图标
        URL resource = JButtonTest.class.getResource("lianbiao.jpg");
        ImageIcon imageIcon = new ImageIcon(resource);

        //把图标放在按钮上
        JButton jButton = new JButton();
        jButton.setIcon(imageIcon);
        jButton.setToolTipText("图片按钮");

        //添加
        contentPane.add(jButton);

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setBounds(100, 100, 500, 500);
    }

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

单选按钮代码:

package day03;

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

public class JRadioButtonTest extends JFrame {
    
    
    public JRadioButtonTest() {
    
    
        Container contentPane = this.getContentPane();

        //单选框
        JRadioButton button1 = new JRadioButton("button1");
        JRadioButton button2 = new JRadioButton("button2");
        JRadioButton button3 = new JRadioButton("button3");

        //将三个按钮放到一个组当中,一个组中只能选一个按钮
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(button1);
        buttonGroup.add(button2);
        buttonGroup.add(button3);

        contentPane.add(button1,BorderLayout.NORTH);
        contentPane.add(button2,BorderLayout.CENTER);
        contentPane.add(button3,BorderLayout.SOUTH);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100, 100, 600, 600);
    }

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

多选框代码

package day03;

import com.sun.jdi.PathSearchingVirtualMachine;

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

public class JCheckBoxTest extends JFrame {
    
    
    public JCheckBoxTest() {
    
    
        Container contentPane = this.getContentPane();

        JCheckBox button1 = new JCheckBox("button1");
        JCheckBox button2 = new JCheckBox("button2");
        JCheckBox button3 = new JCheckBox("button3");

        contentPane.add(button1, BorderLayout.NORTH);
        contentPane.add(button2, BorderLayout.CENTER);
        contentPane.add(button3, BorderLayout.SOUTH);

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setBounds(100, 100, 600, 600);
    }

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

6.列表

下拉框代码:

package day03;

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

public class ComboboxTest extends JFrame {
    
    
    public ComboboxTest() {
    
    
        Container contentPane = this.getContentPane();

        JComboBox<Object> comboBox = new JComboBox<>();

        comboBox.addItem(null);
        comboBox.addItem("等待发货");
        comboBox.addItem("已发货");
        comboBox.addItem("等待接收");

        contentPane.add(comboBox);

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setBounds(100, 100, 600, 100);
    }

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

列表框代码:

package day03;

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

public class JListTest extends JFrame {
    
    
    public JListTest() {
    
    
        Container contentPane = this.getContentPane();

        //列表内容
        String[] content = {
    
    "1", "2", "3"};
        JList<Object> List = new JList<>(content);

        contentPane.add(List);

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setBounds(100, 100, 600, 600);
    }

    public static void main(String[] args) {
    
    
        new JListTest();
    }
}
  • 选择性去看信息,用下拉框
  • 引来展示信息,用列表框,一般是动态扩容

7.文本框

文本框代码

package day03;

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

public class JTextFieldTest extends JFrame {
    
    
    public JTextFieldTest() {
    
    
        Container contentPane = this.getContentPane();

        JTextField hello = new JTextField("hello",10);
        JTextField world = new JTextField("world", 10);

        contentPane.add(hello, BorderLayout.NORTH);
        contentPane.add(world, BorderLayout.SOUTH);

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100, 100, 600, 600);
        this.setVisible(true);
    }

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

密码框代码

package day03;

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

public class JPasswordTest extends JFrame {
    
    
    public JPasswordTest() {
    
    
        Container contentPane = super.getContentPane();

        JPasswordField jPasswordField = new JPasswordField();

        contentPane.add(jPasswordField);

        super.setDefaultCloseOperation(EXIT_ON_CLOSE);
        super.setBounds(100, 100, 600, 600);
        super.setVisible(true);
    }

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

文本域代码

package day03;

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


public class JScrollTest extends JFrame {
    
    
    public JScrollTest() {
    
    
        //创建一个容器
        Container contentPane = getContentPane();

        //创建一个文本域
        JTextArea jTextArea = new JTextArea(20, 50);
        jTextArea.setText("hello world");

        //创建一个Scroll面板
        JScrollPane jScrollPane = new JScrollPane(jTextArea);
        contentPane.add(jScrollPane);

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setBounds(100,100,500,500);
    }

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

参考b站GUI学习视频

猜你喜欢

转载自blog.csdn.net/wqs12345612/article/details/111827522