GUI

2021/7/9 6:06:12

本文主要是介绍GUI,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

目录
  • GUI
    • Frame
    • Panel
      • GridLayout
      • FlowLayout
      • BorderLayout
    • 练习

GUI

Frame

public class TestFrame {
    public static void main(String[] args) {
        Frame frame = new Frame("java图形界面");
//        设置可见性
        frame.setVisible(true);
//        设置窗口大小
        frame.setSize(400,400);
        frame.setBackground(Color.PINK);
//        弹出的初始位置
        frame.setLocation(200,200);
//        设置大小固定
        frame.setResizable(false);
    }
}

Panel

//面板 可以看成一个空间 但是不能单独存在
public class TestPanel {
    public static void main(String[] args) {
        Frame frame = new Frame();
//        布局的概念
        Panel panel = new Panel();

//        设置布局
        frame.setLayout(null);//不设置 默认置顶 所以给它个空
        frame.setBounds(300,300,300,300);
        frame.setBackground(Color.pink);

//        panel设置坐标 相对于frame
        panel.setBounds(50,50,200,200);
        panel.setBackground(Color.gray);

//        面板放入窗口中
        frame.add(panel);
        frame.setVisible(true);

//        监听事件 窗口关闭事件
//        适配器模式
        frame.addWindowListener(new WindowAdapter() {
//            窗口点击关闭的时候做的事情
            @Override
            public void windowClosing(WindowEvent e) {
//                结束程序
                System.exit(0);
            }
        });
    }
}

GridLayout

//表格布局
public class TestGridLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("");

        Button btn1 = new Button("btn1");
        Button btn2 = new Button("btn2");
        Button btn3 = new Button("btn3");
        Button btn4 = new Button("btn4");
        Button btn5 = new Button("btn5");
        Button btn6 = new Button("btn6");

        frame.setLayout(new GridLayout(3, 2));

        frame.add(btn1);
        frame.add(btn2);
        frame.add(btn3);
        frame.add(btn4);
        frame.add(btn5);
        frame.add(btn6);

        frame.pack();
        frame.setBounds(200, 200, 200, 200);
        frame.setVisible(true);

    }
}

FlowLayout

public class TestFlowLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");

//        设置为流式布局
        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
//        frame.setLayout(new FlowLayout(FlowLayout.LEFT));

        frame.setSize(200,200);
        frame.setVisible(true);

        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
    }

BorderLayout

//东西南北中布局
public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();

        Button east = new Button("east");
        Button west = new Button("west");
        Button south = new Button("south");
        Button north = new Button("north");
        Button center = new Button("center");

        frame.add(east,BorderLayout.EAST);
        frame.add(west,BorderLayout.WEST);
        frame.add(south,BorderLayout.SOUTH);
        frame.add(north,BorderLayout.NORTH);
        frame.add(center,BorderLayout.CENTER);

        frame.setBounds(200,200,200,200);
        frame.setVisible(true);


    }
}

练习

public class Think1 {
    public static void main(String[] args) {
        Frame frame = new Frame("联系布局");
        Button btn1 = new Button("btn1");
        Button btn2 = new Button("btn2");
        Button btn3 = new Button("btn3");
        Button btn4 = new Button("btn4");
        Button btn5 = new Button("btn5");
        Button btn6 = new Button("btn6");
        Button btn7 = new Button("btn7");
        Button btn8 = new Button("btn8");
        Button btn9 = new Button("btn9");
        Button btn10 = new Button("btn10");

        Panel panel1 = new Panel();
//        panel1.setLayout(new BorderLayout());
        Panel panel = new Panel();
        panel.setLayout(new GridLayout(2,1));
        panel.add(btn2);
        panel.add(btn3);
        panel1.setLayout(new GridLayout(1,3));
//        panel1.add(btn1,BorderLayout.EAST);
//        panel1.add(panel,BorderLayout.CENTER);
//        panel1.add(btn4,BorderLayout.WEST);
        panel1.add(btn1);
        panel1.add(panel);
        panel1.add(btn4);

        Panel panel2 = new Panel();
        Panel panel3 = new Panel();
        panel3.setLayout(new GridLayout(2,2));
        panel3.add(btn6);
        panel3.add(btn7);
        panel3.add(btn8);
        panel3.add(btn9);
        panel2.setLayout(new GridLayout(1,3));
        panel2.add(btn5,BorderLayout.EAST);
        panel2.add(panel3,BorderLayout.CENTER);
        panel2.add(btn10,BorderLayout.WEST);

        frame.setLayout(new GridLayout(2,1));

        frame.add(panel1);
        frame.add(panel2);
        frame.setBounds(200, 200, 400, 400);
        frame.setVisible(true);
    }
}


这篇关于GUI的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程