java GUI界面编程加法_Java--GUI编程(一)

本文介绍了Java GUI编程的基础,包括Frame窗口的创建和设置,Panel面板的使用,以及三种布局管理器:FlowLayout、BorderLayout和GridLayout的详细操作。通过实例展示了如何利用这些布局管理器构建复杂的窗口界面。
摘要由CSDN通过智能技术生成

2020-04-11

19:42:22

GUI:图形用户界面(Graphical User Interface)

今天开始进入了AWT的学习:万事如期而至

938e2c7ace9e635df023850d30a212af.png

进行窗口界面化首先需要一个Frame。

Frame

这个Frame是基本的窗口。

frame中可以添加各种组件。eg:panel、button、TextArea、Label…

首先尝试着做一个小窗口

public class MyFrame extendsFrame {//可能存在多个窗口,我们需要一个计数器

static int id = 0;

MyFrame(){}

MyFrame(String title,Color color ,int x,int y ,int w ,inth){super("MyFrame"+ (id++));

setResizable(false);

setBackground(color);

setBounds(x,y,w,h);

setVisible(true);

}

}

这是封装后的一个小窗口。

上面也是基本的属性设置。

Panel面板

public classTestPanel {public static voidmain(String[] args) {

Frame frame= newFrame();

Panel panel= newPanel();

frame.setLayout(null);

frame.setBounds(300,300,500,500);

frame.setBackground(Color.GREEN);

panel.setBounds(300,300,200,200);

panel.setBackground(Color.RED);

frame.addWindowListener(newWindowAdapter() {

@Overridepublic voidwindowClosing(WindowEvent e) {int i = 0;

System.exit(i);

}

});

frame.add(panel);

frame.setVisible(true);

}

}

现在到了最重要的一个point:

三种布局管理器:

1、流式布局:FlowLayout

这是一种自动的布局的管理器:

可以选动设置成靠左、居中、靠右 三种形式

在这个布局中添加组件时,这个管理器都会自动的为你排布局面。

public classTestFlowLayout {public static voidmain(String[] args) {

Frame frame= new Frame("FlowLayout");

frame.setLayout(newFlowLayout(FlowLayout.CENTER));

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");

frame.add(button1);

frame.add(button2);

frame.add(button3);

frame.add(button4);

frame.add(button5);

frame.addWindowListener(newWindowAdapter() {

@Overridepublic voidwindowClosing(WindowEvent e) {

System.exit(0);

}

});

frame.pack();

frame.setVisible(true);

}

}

f4b195ca9ebd0a94f921db2b82f67925.png

2、东南西北中布局:BorderLayout

这个布局管理器需要你自行的为你的组件分配到这个布局的各个位置。

public classTestBordLayout {public static voidmain(String[] args) {

Frame frame= newFrame();

frame.setLayout(newBorderLayout());//东南西北中布局需要自行定义方向

Button east= new Button("east");

Button west= new Button("west");

Button north= new Button("north");

Button south= new Button("south");

Button center= new Button("center");

frame.add(east,BorderLayout.EAST);

frame.add(west,BorderLayout.WEST);

frame.add(north,BorderLayout.NORTH);

frame.add(south,BorderLayout.SOUTH);

frame.add(center,BorderLayout.CENTER);

frame.pack();

frame.setVisible(true);

frame.addWindowListener(newWindowAdapter() {

@Overridepublic voidwindowClosing(WindowEvent e) {

System.exit(0);

}

});

}

}

af6565089be88678610098a81c40b201.png

3、表格布局:GirdLayout

这个布局管理器需要你自动的为你的组件分配到这个布局的各个位置。

你可以自行的设置这个布局的行列数。

public classTestGirdLayout {public static voidmain(String[] args) {

Frame frame= newFrame();//表格布局 自动填充

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

Button east= new Button("east");

Button west= new Button("west");

Button north= new Button("north");

Button south= new Button("south");

Button center1= new Button("center1");

Button center2= new Button("center2");

frame.add(east );

frame.add(west );

frame.add(north );

frame.add(south );

frame.add(center1);

frame.add(center2);

frame.pack();

frame.setVisible(true);

frame.addWindowListener(newWindowAdapter() {

@Overridepublic voidwindowClosed(WindowEvent e) {

System.exit(0);

}

});

}

}

c2d13ad890608d9e04b74b098a0e280a.png

最后的最后,我们来做一个练习题:

用今天所学的做一个面板如图:

c157117a817cc49d1172a11b618f5d88.png

public classDemo {public static voidmain(String[] args) {

Frame frame= new Frame("Table!");

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

frame.setBounds(300,300,500,500);

frame.setBackground(new Color(255, 223, 220));

Panel p1= new Panel(newBorderLayout());

Panel p2= new Panel(new GridLayout(2,1));

Panel p3= new Panel(newBorderLayout());

Panel p4= new Panel(new GridLayout(2,2));

p1.add(new Button("1"),BorderLayout.EAST);

p1.add(new Button("2"),BorderLayout.WEST);

p2.add(new Button("3"));

p2.add(new Button("4"));

p1.add(p2,BorderLayout.CENTER);

p3.add(new Button("5"),BorderLayout.EAST);

p3.add(new Button("6"),BorderLayout.WEST);int i = 4;for (int i1 = 0; i1 < i; i1++) {

p4.add(new Button( String.valueOf(i1 + 7)));

}

p3.add(p4,BorderLayout.CENTER);

frame.add(p1);

frame.add(p3);

frame.addWindowListener(newWindowAdapter() {

@Overridepublic voidwindowClosed(WindowEvent e) {

System.exit(0);

}

});

frame.setVisible(true);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值