JAVA图形界面GUI

目录

 一、窗口、窗格、按钮、标签

设置一个窗口JFrame

设置一个窗格JPanel和按钮JButton

设置一个标签JLabel

标准写法

 二、监听器ActionListener

用内部类实现

用匿名内部类实现

用LAMADA表达式实现

 三、文本域、复选框、下拉列表

JTextField单行文本域

JCheckBox复选框

JComboBox下拉列表

 四、设置标签内格式

设置标签内文本格式、标签大小、背景色

小测试

 五、布局器

FlowLayout流式布局器

BoardLayout边界布局器


 一、窗口、窗格、按钮、标签

设置一个窗口JFrame

import javax.swing.*;

/**
 * @author Ethan Xu
 * @date 2023/6/13 19:01
 * Talk is cheap,show me code.
 */
//设置一个窗口
public class Mydemo1 {
    public static void main(String[] args){

        //JFrame指一个窗口,构造方法的参数为窗口标题
        JFrame frame=new JFrame("Swing Example");
        //或者frame.setTitle("Swing Example");也能设置标题

        //当关闭窗口时,退出整个程序
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        //设置窗口的大小
        frame.setSize(400,300);//第一个参数是宽度,第二个参数是高

        //显示窗口
        frame.setVisible(true);
    }
}

 

设置一个窗格JPanel和按钮JButton

import javax.swing.*;

/**
 * @author Ethan Xu
 * @date 2023/6/13 19:21
 * Talk is cheap,show me code.
 */

//设置一个窗口,窗格和按钮
public class Mydemo2 {
    public static void main(String[] args){

        //JFrame指一个窗口,构造方法的参数为窗口标题
        JFrame frame=new JFrame("Swing Example");
        //或者frame.setTitle("Swing Example");也能设置标题

        //当关闭窗口时,退出整个程序
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //设置一个窗格
        JPanel panel=new JPanel();
        frame.setContentPane(panel);

        //设置一个按钮,把按钮加在窗格里
        JButton button=new JButton("测试");
        panel.add(button);

        //设置窗口的大小
        frame.setSize(400,300);//第一个参数是宽度,第二个参数是高

        //显示窗口
        frame.setVisible(true);
    }
}

设置一个标签JLabel

import javax.swing.*;

/**
 * @author Ethan Xu
 * @date 2023/6/13 19:21
 * Talk is cheap,show me code.
 */

//设置一个窗口,窗格,按钮,标签
public class mydemo3 {
    public static void main(String[] args){

        //JFrame指一个窗口,构造方法的参数为窗口标题
        JFrame frame=new JFrame("Swing Example");
        //或者frame.setTitle("Swing Example");也能设置标题

        //当关闭窗口时,退出整个程序
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //设置一个窗格
        JPanel panel=new JPanel();
        frame.setContentPane(panel);

        //设置一个按钮,把按钮加在窗格里
        JButton button=new JButton("测试");
        panel.add(button);

        //设置一个标签
        JLabel label=new JLabel("你好");
        panel.add(label);
        //panel.add(new JLabel("hello"));也可以这么写

        //设置窗口的大小
        frame.setSize(400,300);//第一个参数是宽度,第二个参数是高

        //显示窗口
        frame.setVisible(true);
    }
}

标准写法

定义一个MyFrame类继承JFrame,封装起来

import javax.swing.*;

/**
 * @author Ethan Xu
 * @date 2023/6/13 19:31
 * Talk is cheap,show me code.
 */

//把窗格,按钮,标签封装一下,简化
public class MyFrame1 extends JFrame {
    //封装在MyFrame类中

    public MyFrame1(String title){
        super(title);
        //设置一个窗格
        JPanel panel=new JPanel();
        this.setContentPane(panel);

        //设置一个按钮,把按钮加在窗格里
        JButton button=new JButton("测试");
        panel.add(button);

        //设置一个标签
        JLabel label=new JLabel("你好");
        panel.add(label);
        //panel.add(new JLabel("hello"));也可以这么写
    }
}

再定义一个Use_MyFrame类,使用MyFrame

import javax.swing.*;

/**
 * @author Ethan Xu
 * @date 2023/6/13 19:37
 * Talk is cheap,show me code.
 */

//使用封装后的MyFrame
public class Use_MyFrame1 {
    public static void main(String[] args){


        //MyFrame frame=new MyFrame("标题");
        //一般都写成向上转型
        JFrame frame=new MyFrame1("title");

        //当关闭窗格时,推出整个程序
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //设置窗格大小
        frame.setSize(400,300);

        //显示窗格
        frame.setVisible(true);
    }


}

 二、监听器ActionListener

用内部类实现

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Ethan Xu
 * @date 2023/6/13 19:54
 * Talk is cheap,show me code.
 */

//监听器的三种写法
//1、内部类
//2、匿名内部类
//3、LAMADA表达式

//设置窗口,窗格,按钮,标签,监听器(用内部类写)
//封装起来
public class MyFrame2 extends JFrame {
    //封装在MyFrame类中

    //设置一个标签,用来存放当前时间
    //因为这个东西,要让内部类访问到它,所以把它作为外部类的属性,不放在构造方法中,如果放在构造方法中,则内部类访问不到
    JLabel timeLabel=new JLabel("00:00:00");



    public MyFrame2(String title){
        super(title);
        //设置一个窗格(容器)
        JPanel panel=new JPanel();
        this.setContentPane(panel);

        //设置一个按钮,把按钮加在窗格里
        JButton button=new JButton("测试");
        panel.add(button);


        //在窗格中添加timeLabel
        panel.add(timeLabel);


        //设置一个监听器
        // 点击按钮,就会执行actionPerformed方法
        //button.addActionListener();这东西是一个接口,不能直接实现,要弄一个内部类
        //内部类定义好了,现在要实例化一个对象
        //MyActionListener listener=new MyActionListener();
        //一般都向上转型
        ActionListener listener=new MyActionListener();
        button.addActionListener(listener);
        //也可以将这两句合并
        //button.addActionListener(new MyActionListener());



    }

    //定义了一个内部类实现了addActionListener接口
    private class MyActionListener implements ActionListener {

        public void actionPerformed(ActionEvent e){
            System.out.println("**按钮被点击了");
            //点击按钮之后,显示当前系统时间
            //取得当前时间
            SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
            String timeStr=sdf.format(new Date());
            System.out.println(timeStr);

            timeLabel.setText(timeStr);
        }
    }
}
import javax.swing.*;

/**
 * @author Ethan Xu
 * @date 2023/6/13 20:05
 * Talk is cheap,show me code.
 */

//使用封装后的MyFrame2
public class Use_MyFrame2 {
    public static void main(String[] args){


        //MyFrame frame=new MyFrame("标题");
        //一般都写成向上转型
        JFrame frame=new MyFrame2("title");

        //当关闭窗格时,推出整个程序
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //设置窗格大小
        frame.setSize(400,300);

        //显示窗格
        frame.setVisible(true);
    }


}

用匿名内部类实现

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Ethan Xu
 * @date 2023/6/13 20:38
 * Talk is cheap,show me code.
 */
//设置窗口,窗格,按钮,标签,监听器(用匿名内部类写)
//封装起来
public class MyFrame3 extends JFrame {
    //封装在MyFrame3类中

    //设置一个标签,用来存放当前时间
    //因为这个东西,要让内部类访问到它,所以把它作为外部类的属性,不放在构造方法中,如果放在构造方法中,则内部类访问不到
    JLabel timeLabel=new JLabel("00:00:00");



    public MyFrame3(String title){
        super(title);
        //设置一个窗格(容器)
        JPanel panel=new JPanel();
        this.setContentPane(panel);

        //设置一个按钮,把按钮加在窗格里
        JButton button=new JButton("测试");
        panel.add(button);


        //在窗格中添加timeLabel
        panel.add(timeLabel);


        //设置一个监听器
        // 点击按钮,就会执行actionPerformed方法
        //button.addActionListener();这东西是一个接口,不能直接实现,弄一个匿名内部类
        button.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                System.out.println("**按钮被点击了");
                //点击按钮之后,显示当前系统时间
                showTime();

            }
        });

    }

    public void showTime(){
        //取得当前时间
        SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
        String timeStr=sdf.format(new Date());
        System.out.println(timeStr);

        //显示时间
        timeLabel.setText(timeStr);
    }


}
import javax.swing.*;

/**
 * @author Ethan Xu
 * @date 2023/6/13 20:38
 * Talk is cheap,show me code.
 */
//使用封装后的MyFrame3
public class Use_MyFrame3 {
    public static void main(String[] args){


        //MyFrame frame=new MyFrame("标题");
        //一般都写成向上转型
        JFrame frame=new MyFrame3("title");

        //当关闭窗格时,推出整个程序
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //设置窗格大小
        frame.setSize(400,300);

        //显示窗格
        frame.setVisible(true);
    }


}

用LAMADA表达式实现

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Ethan Xu
 * @date 2023/6/13 20:53
 * Talk is cheap,show me code.
 */
//设置窗口,窗格,按钮,标签,监听器(用Lamada表达式写)
//封装起来
public class MyFrame4 extends JFrame {
    //封装在MyFrame4类中

    //设置一个标签,用来存放当前时间
    //因为这个东西,要让内部类访问到它,所以把它作为外部类的属性,不放在构造方法中,如果放在构造方法中,则内部类访问不到
    JLabel timeLabel=new JLabel("00:00:00");



    public MyFrame4(String title){
        super(title);
        //设置一个窗格(容器)
        JPanel panel=new JPanel();
        this.setContentPane(panel);

        //设置一个按钮,把按钮加在窗格里
        JButton button=new JButton("测试");
        panel.add(button);


        //在窗格中添加timeLabel
        panel.add(timeLabel);


        //设置一个监听器
        // 点击按钮,就会执行actionPerformed方法
        //button.addActionListener();这东西是一个接口,不能直接实现,用lamada表达式实现
        button.addActionListener((e)->{

                System.out.println("**按钮被点击了");
                //点击按钮之后,显示当前系统时间
                showTime();

        });

    }

    public void showTime(){
        //取得当前时间
        SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
        String timeStr=sdf.format(new Date());
        System.out.println(timeStr);

        //显示时间
        timeLabel.setText(timeStr);
    }


}
import javax.swing.*;

/**
 * @author Ethan Xu
 * @date 2023/6/13 20:53
 * Talk is cheap,show me code.
 */
//使用封装后的MyFrame4
public class Use_MyFrame4 {
    public static void main(String[] args){


        //MyFrame frame=new MyFrame("标题");
        //一般都写成向上转型
        JFrame frame=new MyFrame4("title");

        //当关闭窗格时,推出整个程序
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //设置窗格大小
        frame.setSize(400,300);

        //显示窗格
        frame.setVisible(true);
    }


}

 点击测试按钮之后,自动获取当前系统时间

 三、文本域、复选框、下拉列表

JTextField单行文本域

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

/**
 * @author Ethan Xu
 * @date 2023/6/14 18:30
 * Talk is cheap,show me code.
 */

//JTextField 单行文本域
public class MyFrame5 extends JFrame {

    //放在外面,写成属性,是为了让监听器内部类访问到
    //设置一个单行文本域,宽度为20个英文字母
    JTextField textField=new JTextField(20);

    public MyFrame5(String title){
        super(title);

        //设置一个窗格(容器)
        JPanel root=new JPanel();
        this.setContentPane(root);

//        //设置一个单行文本域,宽度为20个英文字母
//        JTextField textField=new JTextField(20);//被放在外面了
        root.add(textField);
        textField.setText("你好");//在文本域中赋一个初值


        //设置一个按钮,一个监听器,当鼠标点击ok按钮时,自动获取文字域里的内容
        JButton button=new JButton("OK");
        root.add(button);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                test();
            }
        });


    }
    public void test(){
        String str=textField.getText();
        System.out.println(str);
    }

}
import javax.swing.*;

/**
 * @author Ethan Xu
 * @date 2023/6/14 18:37
 * Talk is cheap,show me code.
 */
public class Use_MyFrame5 {
    public static void main(String[] args){


        //MyFrame frame=new MyFrame("标题");
        //一般都写成向上转型
        JFrame frame=new MyFrame5("title");

        //当关闭窗格时,退出整个程序
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //设置窗格大小
        frame.setSize(400,300);

        //显示窗格
        frame.setVisible(true);
    }


}

JCheckBox复选框

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

/**
 * @author Ethan Xu
 * @date 2023/6/14 18:57
 * Talk is cheap,show me code.
 */

//JCheckBox复选框

public class MyFrame6 extends JFrame {

    //内部类访问不到,所以作为属性放在外面

    //设置一个复选框
    JCheckBox agreeField=new JCheckBox("同意用户协议");

    //设置一个下一步按钮
    JButton nextButton=new JButton("下一步");

    public MyFrame6(String title){
        super(title);

        JPanel root=new JPanel();
        this.setContentPane(root);

        //设置一个复选框
        //JCheckBox agreeField=new JCheckBox("同意用户协议");放外面了
        root.add(agreeField);
        //agreeField.setSelected(true);//给复选框设置一个初值,默认为勾选,如果参数是false,则默认为不勾选

        //设置一个下一步按钮
        //JButton nextButton=new JButton("下一步");放外面了
        root.add(nextButton);

        //界面初始化
        agreeField.setSelected(false);//将复选框设置为未勾选状态
        nextButton.setEnabled(false);//此时无法选择下一步按钮,被禁用了

        //给复选框添加事件处理
        agreeField.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                if(agreeField.isSelected()){//如果被勾选,则启用下一步按钮,反之则继续禁用下一步按钮
                    nextButton.setEnabled(true);
                }else{
                    nextButton.setEnabled(false);
                }
            }
        });

    }
}
import javax.swing.*;

/**
 * @author Ethan Xu
 * @date 2023/6/14 18:57
 * Talk is cheap,show me code.
 */
public class Use_MyFrame6 {
    public static void main(String[] args){


        //MyFrame frame=new MyFrame("标题");
        //一般都写成向上转型
        JFrame frame=new MyFrame6("title");

        //当关闭窗格时,退出整个程序
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //设置窗格大小
        frame.setSize(400,300);

        //显示窗格
        frame.setVisible(true);
    }


}

 未勾选时,下一步按钮呗禁用

 勾选之后,下一步按钮启用

JComboBox下拉列表

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

/**
 * @author Ethan Xu
 * @date 2023/6/14 19:18
 * Talk is cheap,show me code.
 */

//JComboBox下拉列表

public class MyFrame7 extends JFrame {
    //设置一个下拉列表
    JComboBox<String> colorField=new JComboBox<>();
    //添加一个测试按钮
    JButton testButton=new JButton("测试");

    public MyFrame7(String title){
        super(title);

        JPanel root=new JPanel();
        this.setContentPane(root);

        //设置一个下拉列表
        //JComboBox<String> colorField=new JComboBox<>();放外面了
        root.add(colorField);
        //在下拉列表中添加数据项
        colorField.addItem("红色");
        colorField.addItem("黄色");
        colorField.addItem("蓝色");

        colorField.setSelectedIndex(2);//默认最开始就选中第二项,蓝色


        //添加一个测试按钮
        //JButton testButton=new JButton("测试");放外面了
        root.add(testButton);
        testButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                test();
            }
        });

    }
    public void test(){
        int count=colorField.getItemCount();//获取数据项的个数
        String value=colorField.getItemAt(0);//获取第0项的值
        System.out.println(count+value);
    }

}
import javax.swing.*;

/**
 * @author Ethan Xu
 * @date 2023/6/14 19:26
 * Talk is cheap,show me code.
 */
public class Use_MyFrame7 {
    public static void main(String[] args){


        //MyFrame frame=new MyFrame("标题");
        //一般都写成向上转型
        JFrame frame=new MyFrame7("title");

        //当关闭窗格时,退出整个程序
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //设置窗格大小
        frame.setSize(400,300);

        //显示窗格
        frame.setVisible(true);
    }


}

 

 四、设置标签内格式

设置标签内文本格式、标签大小、背景色

//设置标签内文本的字体格式,颜色,对齐方式,以及标签的大小和背景色

public class MyFrame8 extends JFrame {

    public MyFrame8(String title){
        super(title);

        JPanel root=new JPanel();
        this.setContentPane(root);

        JLabel label=new JLabel();
        root.add(label);

        label.setText("你好");

        //设置标签文字内容的字体
        label.setFont(new Font("楷体",Font.PLAIN,14));//第一个参数是字体,第二个是是否改为粗体(Font.PLAIN不是粗体,Font.BOLD是粗体),第三个是字号

        //设置标签文字内容的颜色
        label.setForeground(new Color(255,0,0));//RGB三个值

        //设置标签的背景色
        label.setOpaque(true);//先打开这个,才能设置背景色,如果不打开,则默认是透明的
        label.setBackground(new Color(0,0,255));//这两句一起用,才能修改背景色

        //设置标签的大小
        label.setPreferredSize(new Dimension(80,30));//宽度和高度

        //标签内文字的对齐方式
        label.setHorizontalAlignment(SwingConstants.CENTER);//SwingConstants.LEFT是左对齐,SwingConstants.CENTER是居中,SwingConstants.RIGHT是右对齐

    }

}
import javax.swing.*;

/**
 * @author Ethan Xu
 * @date 2023/6/14 19:44
 * Talk is cheap,show me code.
 */
public class Use_MyFrame8 {
    public static void main(String[] args){


        //MyFrame frame=new MyFrame("标题");
        //一般都写成向上转型
        JFrame frame=new MyFrame8("title");

        //当关闭窗格时,退出整个程序
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //设置窗格大小
        frame.setSize(400,300);

        //显示窗格
        frame.setVisible(true);
    }


}

小测试

设置四个标签,分别为1,2,3,4,每个颜色都不同
import javax.swing.*;
import java.awt.*;

/**
 * @author Ethan Xu
 * @date 2023/6/14 20:04
 * Talk is cheap,show me code.
 */

//一个小测试,设置四个标签,分别为1,2,3,4,每个颜色都不同

public class MyFrame9 extends JFrame {

    public MyFrame9(String title){
        super(title);

        JPanel root=new JPanel();
        this.setContentPane(root);

//        JLabel a1=new JLabel("1");
//        a1.setOpaque(true);
//        a1.setBackground(Color.YELLOW);
//        a1.setPreferredSize(new Dimension(60,30));
//        a1.setHorizontalAlignment(SwingConstants.CENTER);
//
//        JLabel a2=new JLabel("2");
//        a2.setOpaque(true);
//        a2.setBackground(Color.GREEN);
//        a2.setPreferredSize(new Dimension(60,30));
//        a2.setHorizontalAlignment(SwingConstants.CENTER);
//
//        JLabel a3=new JLabel("3");
//        a3.setOpaque(true);
//        a3.setBackground(Color.LIGHT_GRAY);
//        a3.setPreferredSize(new Dimension(60,30));
//        a3.setHorizontalAlignment(SwingConstants.CENTER);
//
//        JLabel a4=new JLabel("4");
//        a4.setOpaque(true);
//        a4.setBackground(Color.CYAN);
//        a4.setPreferredSize(new Dimension(60,30));
//        a4.setHorizontalAlignment(SwingConstants.CENTER);
//
//        root.add(a1);
//        root.add(a2);
//        root.add(a3);
//        root.add(a4);
        //这种重复性太高,一看就没水平,换一种方式去写

        ColorLabel a1=new ColorLabel("1",Color.YELLOW);
        ColorLabel a2=new ColorLabel("2",Color.GREEN);
        ColorLabel a3=new ColorLabel("3",Color.LIGHT_GRAY);
        ColorLabel a4=new ColorLabel("4",Color.CYAN);

        root.add(a1);
        root.add(a2);
        root.add(a3);
        root.add(a4);


    }

    private static class ColorLabel extends JLabel{
        public ColorLabel(String text,Color bgColor){
            this.setText(text);

            this.setOpaque(true);
            this.setBackground(bgColor);
            this.setPreferredSize(new Dimension(60,30));
            this.setHorizontalAlignment(SwingConstants.CENTER);

        }
    }
}
import javax.swing.*;

/**
 * @author Ethan Xu
 * @date 2023/6/14 20:05
 * Talk is cheap,show me code.
 */
public class Use_MyFrame9 {
    public static void main(String[] args){


        //MyFrame frame=new MyFrame("标题");
        //一般都写成向上转型
        JFrame frame=new MyFrame9("title");

        //当关闭窗格时,退出整个程序
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //设置窗格大小
        frame.setSize(400,300);

        //显示窗格
        frame.setVisible(true);
    }


}

 五、布局器

FlowLayout流式布局器

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

/**
 * @author Ethan Xu
 * @date 2023/6/14 20:27
 * Talk is cheap,show me code.
 */
//布局器,默认是FlowLayout流式布局器
    //从左往右,依次往后排,如果排满了,则排在下一行

public class MyFrame10 extends JFrame {

    public MyFrame10(String title){
        super(title);

        JPanel root=new JPanel();
        this.setContentPane(root);

        //不写的话也是默认的流式布局器,写上也行
        LayoutManager layout=new FlowLayout();
        root.setLayout(layout);


        MyFrame10.ColorLabel a1=new MyFrame10.ColorLabel("1", Color.YELLOW);
        MyFrame10.ColorLabel a2=new MyFrame10.ColorLabel("2",Color.GREEN);
        MyFrame10.ColorLabel a3=new MyFrame10.ColorLabel("3",Color.LIGHT_GRAY);
        MyFrame10.ColorLabel a4=new MyFrame10.ColorLabel("4",Color.CYAN);

        root.add(a1);
        root.add(a2);
        root.add(a3);
        root.add(a4);

        a3.setPreferredSize(new Dimension(250,60));


    }

    private static class ColorLabel extends JLabel{
        public ColorLabel(String text,Color bgColor){
            this.setText(text);

            this.setOpaque(true);
            this.setBackground(bgColor);
            this.setPreferredSize(new Dimension(60,30));
            this.setHorizontalAlignment(SwingConstants.CENTER);

        }
    }
}
import javax.swing.*;

/**
 * @author Ethan Xu
 * @date 2023/6/14 20:27
 * Talk is cheap,show me code.
 */
public class Use_MyFrame10{
    public static void main(String[] args){


        //MyFrame frame=new MyFrame("标题");
        //一般都写成向上转型
        JFrame frame=new MyFrame10("title");

        //当关闭窗格时,退出整个程序
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //设置窗格大小
        frame.setSize(400,300);

        //显示窗格
        frame.setVisible(true);
    }


}

BoardLayout边界布局器

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

/**
 * @author Ethan Xu
 * @date 2023/6/14 20:38
 * Talk is cheap,show me code.
 */

//BoardLayout 边界布局器
    //将容器分为东,南,西,北,中,五个区域
public class MyFrame11 extends JFrame {

    public MyFrame11(String title){
        super(title);

        JPanel root=new JPanel();
        this.setContentPane(root);

        //设置成边界布局器
       root.setLayout(new BorderLayout());


        MyFrame11.ColorLabel a1=new MyFrame11.ColorLabel("1", Color.YELLOW);
        MyFrame11.ColorLabel a2=new MyFrame11.ColorLabel("2",Color.GREEN);
        MyFrame11.ColorLabel a3=new MyFrame11.ColorLabel("3",Color.LIGHT_GRAY);
        MyFrame11.ColorLabel a4=new MyFrame11.ColorLabel("4",Color.CYAN);
        MyFrame11.ColorLabel a5=new MyFrame11.ColorLabel("5",Color.RED);

        root.add(a1,BorderLayout.NORTH);//显示在北边,宽度不能变,但是高度可以变
        root.add(a2,BorderLayout.SOUTH);
        root.add(a3,BorderLayout.WEST);//高度不能调,但是可以调宽度
        root.add(a4,BorderLayout.EAST);
        root.add(a5,BorderLayout.CENTER);//中间这个区域不能调整,一定是占满整个中央

        //把a1调高点
        a1.setPreferredSize(new Dimension(0,100));//宽度怎么设都不会变

        //把a3调高点
        a3.setPreferredSize(new Dimension(100,0));//高度怎么设都不会变



    }

    private static class ColorLabel extends JLabel{
        public ColorLabel(String text,Color bgColor){
            this.setText(text);

            this.setOpaque(true);
            this.setBackground(bgColor);
            this.setPreferredSize(new Dimension(60,30));
            this.setHorizontalAlignment(SwingConstants.CENTER);

        }
    }
}
import javax.swing.*;

/**
 * @author Ethan Xu
 * @date 2023/6/14 20:39
 * Talk is cheap,show me code.
 */
public class Use_MyFrame11{
    public static void main(String[] args){


        //MyFrame frame=new MyFrame("标题");
        //一般都写成向上转型
        JFrame frame=new MyFrame11("title");

        //当关闭窗格时,退出整个程序
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //设置窗格大小
        frame.setSize(400,300);

        //显示窗格
        frame.setVisible(true);
    }


}

  • 1
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java GUI图形界面编程是指使用Java编程语言来创建图形用户界面(Graphical User Interface,GUI)应用程序的过程。Java提供了一组丰富的类库和工具,使得开发人员可以快速、方便地创建交互性强、用户友好的应用程序。 Java GUI图形界面编程的主要组成部分是Swing和JavaFX两个库。Swing是一个基于Java的图形用户界面工具包,提供了丰富的用户界面组件,如按钮、文本框、表格等,可以在应用程序中创建和管理这些组件,实现用户界面的交互。JavaFX是Java平台上的富客户端应用程序框架,提供了更强大、更灵活的界面组件和布局控制,可以创建更加先进、更具吸引力的GUI应用程序。 使用Java GUI图形界面编程进行应用程序开发有许多优点。首先,Java GUI库提供了丰富的组件和控件,开发人员可以根据自己的需求选择合适的组件,快速构建出用户界面。其次,Java GUI库具有良好的跨平台性,可以在不同操作系统上运行,并且具有相同的用户界面。再者,Java提供了强大的事件驱动模型,使得应用程序能够响应用户的操作,实现交互性。 总之,Java GUI图形界面编程是一种灵活、方便、强大的开发方式,使得开发人员能够更加简单地创建出功能强大、用户友好的应用程序。无论是简单的桌面应用还是复杂的企业级应用,都可以通过Java GUI图形界面编程来实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

摸鱼哥myg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值