首页 技术 正文
技术 2022年11月22日
0 收藏 840 点赞 3,701 浏览 5214 个字
  • GUI(Graphical User Interface),图形用户接口
  • CLI(Command Line User Interface),命令行用户接口

1. 容器 Container

GUI主要位于 java.awt 和 javax.swing 两个包及其子包中

Container 和 Component  是AWT中的两个核心类,Container是Component子类,Container子类对象可以“容纳”别的Component对象。

【Java基础总结】GUI

Frame是Window的子类,由Frame或其子类创建的对象为一个窗体。

    public static void main(String[] args){
Frame f = new Frame("AwtDemo1");
f.setSize(500, 300); //设置窗体大小(width, high)
f.setLocation(300, 200); //设置窗体位置(x, y) //设置Frame显示
f.setVisible(true);
}

对于Frame的编程而言,比较常用的方式是继承Frame类,并且在构造函数或者新编写一个函数来设置Frame的信息

public class AwtDemo2 extends Frame{
public AwtDemo2(){
this.setTitle("Awt Demo2");
this.setBounds(500, 300, 300, 200); //x, y, width, height
this.setVisible(true);
}
public static void main(String[] args){
new AwtDemo2();
}
}

运行上面代码会生成如下这样一个窗体:

【Java基础总结】GUI

Panel对象可以看成可以容纳Component的空间,可以拥有自己的布局管理器。

2. 组件 Component

代码1:按钮、标签、复选框 、下拉列表等组件

public class ComponentDemo1 extends Frame{    public static void main(String[] args){
new ComponentDemo1();
}
public ComponentDemo1(){
//标签Label,第一个参数是标题,第二个参数是排列方式
this.add(new Label("姓名", Label.LEFT));
//文本框,第一个参数是标题,第二个参数是长度
TextField tf = new TextField ("小明", 15);
this.add(tf);
//文本域TextArea,第一参数是行数,第二个参数是列数
TextArea ta = new TextArea (2, 25);
ta.setText ("你好!"); //设置默认内容
this.add(ta);
//复选框Checkbox,第一个参数是名称,第二参数可选,表示是否选中状态
Checkbox cb1 = new Checkbox ("系统管理员");
Checkbox cb2 = new Checkbox ("用户", true);
Checkbox cb3 = new Checkbox ("过客", true);
//将复选框添加到复选框组里,就变成单选框
CheckboxGroup g = new CheckboxGroup();
cb1.setCheckboxGroup (g);
cb2.setCheckboxGroup (g);
cb3.setCheckboxGroup (g);
//setBounds 移动组件并调整其大小。
cb1.setBounds (20,60,140,20);
cb2.setBounds (20,80,140,20);
cb3.setBounds (20,100,140,20); this.add(cb1);
this.add(cb2);
this.add(cb3);
//下拉列表Choice
Choice choice = new Choice();
choice.add("英语");
choice.add("法语");
choice.add("德语");
this.add(choice);
//列表List,可以多选
List list = new List(3, true);
list.add("大学物理");
list.add("数据结构");
list.add("计算机网络");
this.add(list); this.setTitle("AWT组件库Component演示1");
this.setLayout(new FlowLayout());
this.setBounds(200, 200, 400, 200); this.setVisible(true);
}
}

运行结果:

【Java基础总结】GUI

代码2:对话框和文件对话框

public class ComponentDemo2 extends Frame{
public static void main(String[] args){
new ComponentDemo2();
}
public ComponentDemo2(){
this.setTitle("AWT组件库Component演示2");
this.setBounds(200, 200, 400, 200); final Label fileLabel = new Label("选中的文件名");
this.add(fileLabel, BorderLayout.CENTER); //默认是BorderLayout //对话框Dialog,第一参数是父窗口,第二参数是标题,第三个参数表示是否显示
final Dialog dialog = new Dialog(this, "对话框ok", false);
Label label = new Label("Welcome to come!");
dialog.add(label);
dialog.setBounds(300, 300, 150, 100);
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
dialog.setVisible(false);
}
});
//按钮Button,第一参数是名称
Button btn = new Button("ok");
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dialog.setVisible(true);
}
});
this.add(btn, BorderLayout.NORTH); //文件对话框FileDialog,第1个参数是父窗口,第2个参数是标题,第3个参数是文件对话框的作用是打开还是保存
final FileDialog fileDialog = new FileDialog(this, "打开文件", FileDialog.LOAD);
fileDialog.setBounds(300, 300, 150, 100);
Button btn2 = new Button("打开文件");
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
fileDialog.setVisible(true);
String dir = fileDialog.getDirectory();
String file = fileDialog.getFile();//获取选中的文件名称
fileLabel.setText(dir + "" + file);
}
});
this.add(btn2, BorderLayout.SOUTH);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
this.setVisible(true);
}
}

运行结果:

【Java基础总结】GUI

3. 布局管理器 Layout

下面这窗体中的组件太乱了,有什么办法让他们变的整齐一些吗?使用布局管理器可以达到布局整齐的效果!

【Java基础总结】GUI

三种布局管理器:

  • BorderLayout
  • FlowLayout
  • GridLayout

简单的代码说明:

 public class LayoutDemo1 extends Frame{
public static void main (String args[]) {
new LayoutDemo1(args[0]);
} public LayoutDemo1(String layout){
this.setTitle(layout);
this.setBounds(300, 200, 200, 200);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
super.windowClosing(e);
}
}); switch(layout){
case "BorderLayout" :
addBorderLayout();
break;
case "FlowLayout" :
addFlowLayout();
break;
case "GridLayout" :
addGridLayout();
break;
default:
addBorderLayout();
break;
}
this.setVisible(true);
}
public void addBorderLayout(){
this.setLayout(new BorderLayout());
this.add(new Button("North"), BorderLayout.NORTH);
this.add(new Button("South"), BorderLayout.SOUTH);
this.add(new Button("East"), BorderLayout.EAST);
this.add(new Button("West"), BorderLayout.WEST);
this.add(new Button("Center"), BorderLayout.CENTER);
}
public void addFlowLayout(){
this.setLayout(new FlowLayout());
this.add(new Button("Button1"));
this.add(new Button("Button2"));
this.add(new Button("Button3"));
this.add(new Button("Button4"));
}
public void addGridLayout(){
//new GridLayout(3,2) 构造一个3行2列的网格
setLayout(new GridLayout(3,2));
add(new Button("1"));
add(new Button("2"));
add(new Button("3"));
add(new Button("4"));
add(new Button("5"));
add(new Button("6")); }
}

运行结果:

【Java基础总结】GUI     【Java基础总结】GUI    【Java基础总结】GUI

4. 菜单

简单的菜单演示:

class MenuDemo1 extends Frame{
private MenuBar menuBar;
private Menu menu, secondLevelMenu;
private MenuItem exitItem,subItem; MenuDemo1(){
this.setTitle("MenuDemo1");
this.setBounds(200,200,500,400); //setBounds(x, y, width, high)
//菜单栏
menuBar = new MenuBar();
//一级菜单
menu = new Menu("文件");
//二级菜单
secondLevelMenu = new Menu("新建");
//子菜单
exitItem = new MenuItem("退出");
subItem = new MenuItem("Java文件"); secondLevelMenu.add(subItem);
menu.add(secondLevelMenu);
menu.add(exitItem);
menuBar.add(menu);
//添加菜单栏
this.setMenuBar(menuBar);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
this.setVisible(true);
}
public static void main(String[] args){
new MenuDemo1();
}
}

运行结果:

【Java基础总结】GUI

5. 事件处理机制

6. 绘图

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,965
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781