GUI学习之Swing

1. 窗口JFrame

JFrame是Frame的子类,JFrame在javax.swing包内,Frame在包java.awt中。

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

public class testJF {
    public static void main(String[] args) {
        new MyJF().init();
    }
}
//JFrame是一个顶级窗口,里面的东西需要放在容器中
class MyJF extends JFrame{
    //init()方法用于初始化
    public void init(){
        JLabel jLabel = new JLabel("这是JLabel");
        this.add(jLabel);
        //设置JLabel居中
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        //获得一个容器,用于添加背景色
        Container container = this.getContentPane();
//        this.setBackground(Color.blue);不会生效,
        container.setBackground(Color.blue);
        this.setSize(200,200);
        this.setVisible(true);
        //关闭事件
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

2. 弹窗JDialog

JDialog是被弹出的窗口,默认有关闭此窗口事件

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

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

class MyJD extends JFrame{
    public void init(){
        setSize(600,500);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container container = getContentPane();
        container.setLayout(null);//绝对布局,可以设置其中元素的坐标位置和大小
        JButton jButton = new JButton("点击弹出");
        //设置按钮在绝对布局中的位置大小
        jButton.setBounds(30,30,200,50);
        //监听器,点击按钮弹出事件
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyJD2();
            }
        });
        container.add(jButton);
    }
}

class MyJD2 extends JDialog{
    public MyJD2(){
        setVisible(true);
        this.setBounds(300,300,300,400);
//        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container container = getContentPane();
        container.setLayout(null);
        JLabel jLabel = new JLabel("这是JLabel");
        jLabel.setSize(130,30);//设置JLabel大小后才会在弹窗中显示
        container.add(jLabel);
    }
}

3. 图标

3.1. Icon普通图标

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

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

class MyIcon extends JFrame implements Icon{
    private int width;
    private int height;
    public MyIcon(){}
    public MyIcon(int width,int height) {
        this.width=width;
        this.height=height;
    }
    public void init(){
        MyIcon myIcon = new MyIcon(15,15);
        //图标要放在标签或按钮上
        JLabel Jlabel = new JLabel("testicon",myIcon,SwingConstants.CENTER);
        Container contentPane = getContentPane();
        contentPane.add(Jlabel);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(300,400);
        setVisible(true);
    }
//用画笔画出图标样式
    @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;
    }
}

3.2 ImageIcon图片图标

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

public class testImgIcon {
    public static void main(String[] args) {
        new MyII();
    }
}

class MyII extends JFrame{
    public MyII() throws HeadlessException {
        JLabel jLabel = new JLabel("ImageIcon");
        //获取图片地址
        URL resource = MyII.class.getResource("top0.png");
        ImageIcon imageIcon = new ImageIcon(resource);
        jLabel.setIcon(imageIcon);
        jLabel.setSize(30,40);
        Container contentPane = getContentPane();
        contentPane.add(jLabel);
        setVisible(true);
        setSize(300,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

4. 面板JPanel、JScrollPane

JPanel与Panel类似

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

public class testJP {
    public static void main(String[] args) {
        new MyJP();
    }
}

class MyJP extends JFrame{
    public MyJP() {
        Container contentPane = this.getContentPane();
        //设置表格布局,两行一列水平、垂直间距为10
        contentPane.setLayout(new GridLayout(2,1,10,10));
        JPanel jPanel1 = new JPanel(new GridLayout(1,3));
        JPanel jPanel2 = new JPanel(new GridLayout(2,1));
        jPanel1.add(new JButton("1"));
        jPanel1.add(new JButton("1"));
        jPanel1.add(new JButton("1"));
        jPanel2.add(new JButton("2"));
        jPanel2.add(new JButton("2"));
        contentPane.add(jPanel1);
        contentPane.add(jPanel2);
        setSize(500,500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);

    }
}


JScrollPane是带有滚动条的面板。

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

public class testJS {
    public static void main(String[] args) {
        new MyJS();
    }
}

class MyJS extends JFrame{
    public MyJS(){
        Container contentPane = getContentPane();
        //创建文本域
        JTextArea jTextArea = new JTextArea(20,50);
        jTextArea.setText("文本内容");
        //创建滚动面板
        JScrollPane jScrollPane = new JScrollPane(jTextArea);
        contentPane.add(jScrollPane);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(200,200);
        setVisible(true);
    }
}

会出现滚动条

5. 按钮JButton

5.1. 图片按钮

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

public class testJB {
    public static void main(String[] args) {
        new MyJB();
    }
}

class MyJB extends JFrame{
    public MyJB() throws HeadlessException {
        Container contentPane = this.getContentPane();
        URL resource = MyJB.class.getResource("top0.png");
        //先将图片变为图标
        ImageIcon imageIcon = new ImageIcon(resource);
        JButton jButton = new JButton();
        //把图标放在图片上
        jButton.setIcon(imageIcon);
        //设置提示
        jButton.setToolTipText("图片按钮");
        contentPane.add(jButton);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(300,300);
        setVisible(true);
    }
}

5.2 单选框JRadioButton

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

public class testSelect {
    public static void main(String[] args) {
        new MyS();
    }
}

class MyS extends JFrame{
    public MyS() {
        Container contentPane = getContentPane();
        //单选框
        JRadioButton jRadioButton1 = new JRadioButton("1");
        JRadioButton jRadioButton2 = new JRadioButton("2");
        JRadioButton jRadioButton3 = new JRadioButton("3");

        //将三个按钮分到一个组里
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(jRadioButton1);
        buttonGroup.add(jRadioButton2);
        buttonGroup.add(jRadioButton3);

        contentPane.add(jRadioButton1);
        contentPane.add(jRadioButton2);
        contentPane.add(jRadioButton3);
        //设置为流式布局
        setLayout(new FlowLayout(FlowLayout.LEFT));
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(300,300);
        setVisible(true);
    }
}

5.3 多选框

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

public class testS2 {
    public static void main(String[] args) {
        new MyS2();
    }
}

class MyS2 extends JFrame{
    public MyS2() {
        Container contentPane = getContentPane();
        //多选框
        JCheckBox jCheckBox1 = new JCheckBox("1");
        JCheckBox jCheckBox2 = new JCheckBox("2");
        JCheckBox jCheckBox3 = new JCheckBox("3");
        
        contentPane.add(jCheckBox1);
        contentPane.add(jCheckBox2);
        contentPane.add(jCheckBox3);
        //设置为流式布局
        setLayout(new FlowLayout(FlowLayout.LEFT));
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(300,300);
        setVisible(true);
    }
}

6. 列表

6.1 下拉框

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class testCB {
    public static void main(String[] args) {
        new MyCB();
    }
}

class MyCB extends JFrame{
    public MyCB(){
        Container contentPane = getContentPane();
        //创建下拉框对象
        JComboBox<String> jComboBox = new JComboBox<String>();
        //设置条目
        jComboBox.addItem(null);
        jComboBox.addItem("1");
        jComboBox.addItem("2");
        jComboBox.addItem("3");
        //添加监听事件获取选中的值
        jComboBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                System.out.println(e.getItem());
            }
        });
        //下一行是使用匿名方法代替的
//        jComboBox.addItemListener(e -> System.out.println(e.getItem()));
        contentPane.add(jComboBox);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(300,300);
        setVisible(true);
    }
}

6.2. 列表框

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


public class testCB2 {
    public static void main(String[] args) {
        new MyCB2();
    }
}

class MyCB2 extends JFrame{
    public MyCB2(){
        Container contentPane = getContentPane();
        //创建下拉框对象
        String[] s = {"1","2","3"};
        JList<String> stringJList = new JList<>(s);

        contentPane.add(stringJList);

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(300,300);
        setVisible(true);
    }
}

7. 文本框、密码框

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

public class testTest {
    public static void main(String[] args) {
        new MyT();
    }
}

class MyT extends JFrame{
    public MyT() {
        Container contentPane = getContentPane();
        //创建文本框
        JTextField text = new JTextField("text");
        contentPane.add(text);
        //创建密码框
        JPasswordField jPasswordField = new JPasswordField("password");
        //设置显示的字符
        jPasswordField.setEchoChar('*');
        contentPane.add(jPasswordField);
        setLayout(new FlowLayout(FlowLayout.LEADING));
        setSize(300,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);

    }
}

两个框都可以输入文本,但第二个框是密码形式的

posted @ 2021-08-25 14:56  ellll  阅读(119)  评论(0)    收藏  举报