Frame-Pannel-Button综合

简介: Frame-Pannel-Button综合

1、Frame已经介绍过,不懂可以参考 博客-Frame

2、Pannel:用在Frame中的,类似一种面板放在Frame框架中,使用时需要配合Frame

因为生成的Frame框不能X掉,写下面这个就可以把生成的界面X掉,不至于强行停止程序

frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
package 狂神说__AWT;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

//面板
public class TestPannel {
    public static void main(String[] args) {
        Frame frame=new Frame();
        Panel panel=new Panel();
        frame.setLayout(null);
        //框架的坐标大小颜色
        frame.setBounds(300,300,400,400);
        frame.setBackground(new Color(133, 240, 192));
        //在frame框架中设置面板
        panel.setBounds(100,100,100,100);
        panel.setBackground(new Color(255, 18, 30));
        //框架中加入面板
        frame.add(panel);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

3、Button按钮有三种类型:
(1)FlowLayout 流式布局,指定按钮在左边、右边或者中间

package 狂神说__AWT;

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

//Layout布局
public class TestFlowLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();

        frame.setVisible(true);
        frame.setBounds(200,200,400,400);
        //按钮
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        //流式布局
        //frame.setLayout(new FlowLayout()); //中心
        //frame.setLayout(new FlowLayout(FlowLayout.LEFT)); //左边
        frame.setLayout(new FlowLayout(FlowLayout.RIGHT)); //左边

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

(2)BorderLayout 东西南北布局

package 狂神说__AWT;

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

public class TestGridLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setBounds(200,200,400,400);
        frame.setVisible(true);
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");
        Button button4 = new Button("button4");
        Button button5 = new Button("button5");
        Button button6 = new Button("button6");
        //表格布局
        frame.setLayout(new GridLayout(3,2));//三行两列
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        frame.add(button6);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

综合:用布局如下的界面:

package 狂神说__AWT;

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

public class FrameComprehensive {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Panel panel1 = new Panel(new BorderLayout());
        Panel panel2 =new Panel(new GridLayout(2,1));//这个面板分成两行一列
        Panel panel3=new Panel(new BorderLayout());
        Panel panel4=new Panel(new GridLayout(2,2));//这个面板分成两行两列
        frame.setVisible(true);
        frame.setBounds(200,200,400,400);
        frame.setLayout(new GridLayout(2,1));
        panel1.add(new Button("button1"),BorderLayout.WEST);
        panel1.add(new Button("button3"),BorderLayout.EAST);
        panel2.add(new Button("button5"));
        panel2.add(new Button("button6"));
        panel1.add(panel2,BorderLayout.CENTER);
        panel3.add(new Button("button2"),BorderLayout.WEST);
        panel3.add(new Button("button4"),BorderLayout.EAST);
        for (int i = 7; i <=10; i++) {
            panel4.add(new Button("button"+i));
        }
        panel3.add(panel4,BorderLayout.CENTER);
        frame.add(panel1);
        frame.add(panel3);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

相关文章
|
1月前
有关element UI el-table 跟el-dialog搭配使用出现的问题,背景问题,穿透问题
有关element UI el-table 跟el-dialog搭配使用出现的问题,背景问题,穿透问题
69 0
|
1月前
|
小程序 前端开发
【微信小程序】-- 常用的基础内容组件介绍 -- text & rich-text & progress & icon(七)
【微信小程序】-- 常用的基础内容组件介绍 -- text & rich-text & progress & icon(七)
SAP UI5 Responsive Grid Layout 里的 Label-Field Ratio 在屏幕类型 S 下的表现
对于 SAP UI5 SimpleForm 的每种尺寸,我们可以定义用于标签(labelSpanXL、labelSpanL、labelSpanM、labelSpanS)、字段(隐式)和空网格列(emptySpanXL、emptySpanL、emptySpanM、emptySpanS)的网格列数。
|
10月前
|
JavaScript 芯片
PADS Layout添加工艺边和Mark点的方法和步骤
PCB在进行贴片加工的时候(SMT),一般有3种方式(基于开钢网的情况):全人工、半自动、全自动。全人工就是刷钢网,放置元器件都是人工操作。半自动是指人工刷钢网,放置元器件上自动贴片机。全自动是指刷钢网和放置元器件都是机器自动完成。对于全人工的我们就很好理解,毕竟人是活的,最智能的,遇到突发情况都可以想办法处理。
516 0
从0开发游戏引擎之2D基础组件的实现(Image;Label;Button)
从0开发游戏引擎之2D基础组件的实现(Image;Label;Button)
|
Android开发
NavigationView中,动态增加item以及menu
NavigationView中,动态增加item以及menu
320 0
|
C#
艾伟_转载:C# WinForm开发系列 - CheckBox/Button/Label/ProgressBar
包含自定义颜色显示的CheckBox,水晶效果按钮,透明圆角Label,Vista效果的ProgressBar等控件(文章及相关代码搜集自网络,仅供参考学习,版权属于原作者! ). 1.自定义颜色显示的CheckBox coloredcheckcontrols.
1443 0
|
前端开发 Android开发
6.4 Android绘图技巧(Primary:四大方法&Layer)
1.Canvas的四大金刚 Canvas.save()这个方法从字面上可以理解为保存画布,作用就是将之前的所有已绘制的图像保存起来。让后续的操作就好像在一个新的图层上操作一样,这一点与Photoshop中的图层理解基本一致。
798 0
|
Web App开发 测试技术