面板Panel

面板

主要步骤:

1、new一个frame窗口

格式
  Frame frame = new Frame()

2、设置窗口的大小、位置、可见性

格式位置及大小
  frame.setBounds(400,400,400,400);
可见性
  frame.setVisible(true);

3、设置frame窗口的布局格式(分为流式布局,东西南北中,表格布局等)

frame的布局能决定所添加的面板的位置

窗口布局的格式:

流式布局
  frame.setLayout(new FlowLayout(FlowLayout.CENTRE))(居中)
  frame.setLayout(new FlowLayout(FlowLayout.LEFT))(居左)
  frame.setLayout(new FlowLayout(FlowLayout.RIGHT))(居右)
东西南北中布局
  frame.setLayout(new BorderLayout.EAST)(东)
  frame.setLayout(new BorderLayout.WEST)(西)
  frame.setLayout(new BorderLayout.SOUTH)(南)
  frame.setLayout(new BorderLayout.NORTH)(北)
  frame.setLayout(new BorderLayout.CENTRE)(中)
表格布局
  frame.setLayout(new GridLayout(r,c))(r行c列)
各个布局有关代码:
点击查看代码
package com.myblog.firstjavaproject2;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class GUI04 {
    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()的括号中添加FlowLayout.left即可
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.setSize(200,200);
        //把按钮添加上去
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.setVisible(true);
        //设置监听事件,用来关闭窗口
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
               System.exit(0);
            }
        });*/
//东西南北中
        //设置按钮
        Button button1 = new Button("east");
        Button button2 = new Button("west");
        Button button3 = new Button("north");
        Button button4 = new Button("south");
        Button button5 = new Button("centre");
        //添加按钮,并且设置位置
        frame.add(button1,BorderLayout.EAST);
        frame.add(button2,BorderLayout.WEST);
        frame.add(button3,BorderLayout.NORTH);
        frame.add(button4,BorderLayout.SOUTH);
        frame.add(button5,BorderLayout.CENTER);
        frame.setBounds(400,400,400,400);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });*/
//表格布局
        Button button1 = new Button("btn1");
        Button button2 = new Button("btn2");
        Button button3 = new Button("btn3");
        Button button4 = new Button("btn4");
        Button button5 = new Button("btn5");
        frame.setLayout(new GridLayout(2,3));
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        frame.setBounds(400,400,400,400);
        frame.pack();
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

}

4、new一个面板Panel

格式
  Panel panel = new Panel()

5、设置面板的布局

面板的布局可以决定添加在该面板上的组件的位置
同窗口布局,只将frame换成panel

6、设置面板的位置大小

格式同窗口设置,只将frame换成panel

7、将面板添加到窗口中

  frame.add(button1);

8、设置监听事件以关闭窗口

  frame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
           System.exit(0);
        }
    })

9,有关代码

点击查看代码
package com.myblog.firstjavaproject2;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class GUI03 {
    public static void main(String[] args) {
//      需要窗口,则new一个Frame
     Frame frame=new Frame("带面板的窗口");
     //需要面板,则new一个Panel
      Panel panel =new Panel();
      //设置布局,默认面板中为空
    frame.setLayout(null);
      //设置窗口坐标和宽高
      frame.setBounds(400,400,400,400);
      //设置窗口背景颜色
      frame.setBackground(new Color(1, 152, 253, 255));
      //设置面板的坐标和宽高,注意这里面板的范围不能超出窗口的范围
      panel.setBackground(new Color(1,1,1));
      //设置面板的背景颜色
      panel.setBounds(200,200,200,200);
      //面板和窗口的关系,在窗口中添加一个面板
        frame.add(panel);
        //设置面板和窗口的可见性
        frame.setVisible(true);
        panel.setVisible(true);
        //设置监听事件,即为关闭窗口,这里使用适配器  WindowAdapter(),然后添加 System.exit(0)
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });


    }
}

posted @ 2022-03-22 20:49  i_归零  阅读(201)  评论(0编辑  收藏  举报