第一 GUI概述
public class AwtDemo {
public static void main(String[] args) {
Frame f = new Frame("my awt");// 开启另外一个线程,具备默认的布局,是边界布局
f.setSize(500, 200);// 设置大小,宽、高
f.setVisible(true);// 设置窗体可见
f.setLocation(300, 200);// 设置本地位置,也就是窗体初始化位置
f.setLayout(new FlowLayout());// 设置窗体布局为流式布局
Button button = new Button("我是一个按钮");// 建立一个按钮
f.add(button);// 将按钮和窗体相关联
// 给窗体加事件监听器,只要监听器方法超过三个的,都有适配器,而Button的ActionListener就是少数没有适配器的监听器
f.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
System.out.println(e);
System.out.println("opened");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println(e);
System.out.println("closing");
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println(e);
System.out.println("closed");
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println(e);
System.out.println("activated");
}
});
}
}/*
* 简单的优化
*/
public class FrameDemo {
// 定义该图形中所需的组件的引用。
private Frame f;//窗体
private Button btn;//按钮
private TextField tf;
FrameDemo() {
init();
}
// 初始化
public void init() {
//初始化组件
f = new Frame("my frame");
btn = new Button("关闭窗体按钮");
tf = new TextField(20);//创建文本框,并指定大小
// 对frame进行基本参数设定
f.setBounds(200, 200, 500, 400);// 设置距离x,y和宽高
f.setLayout(new FlowLayout());// 设置为流式布局
f.add(btn);// 按钮关联窗体
f.add(tf);//文本框关联窗体
f.setVisible(true);// 显示窗体
setEvent();// 加载事件
}
// 设定事件
private void setEvent() {
// 窗体事件
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// 按钮事件
btn.addActionListener(new ActionListener() {
// private int count = 1;
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("button action event");
// System.out.println("按钮关闭窗体");
// System.exit(0);
// Button b = (Button) e.getSource();
// Frame f1 = (Frame) b.getParent();
//
// f1.add(new Button("button-" + count++));
// f1.validate();
}
});
// 鼠标事件,一般的控件都具备
// 给按钮添加鼠标事件
btn.addMouseListener(new MouseAdapter() {
int count = 1;
int mousecount = 1;
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("点击鼠标" + mousecount++);
if (e.getClickCount() == 2) {
System.out.println("双击事件");
}
}
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("鼠标进入到该组件" + count++);
}
});
// 给按钮添加键盘事件
btn.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
// System.out.println(KeyEvent.getKeyText(e.getKeyCode()) +
// "...."
// + e.getKeyChar() + "...." + e.getKeyCode());
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {// 单键
System.exit(0);
}
// 组合键
if (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_ENTER) {
System.exit(0);
}
}
});
//给文本框添加键盘事件,限制输入
tf.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e){
int code = e.getKeyCode();
if(!(code >= KeyEvent.VK_0 && code <= KeyEvent.VK_9)){
System.out.println(code + "输入非法");
e.consume();//改变事件的默认处理方式
}
}
});
}
public static void main(String[] args) {
new FrameDemo();
}
}第二 其他组件
import java.awt.Button;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
/*
* 图形化界面,一个类似目录查找界面
*/
public class MywindowDemo {
// 创建主窗体
// 创建窗体
private Frame frame;
// 文本框
private TextField textField;
// 按钮
private Button button;
// 文本区域
private TextArea textArea;
// 创建对话框窗体
// 创建对话框
private Dialog dialog;
// 对话框文本
private Label label;
// 对话框的确定按钮
private Button okbutton;
// 对象初始化
MywindowDemo() {
init();
}
// 初始化设定
public void init() {
// 创建主窗体
frame = new Frame("主窗体");
// 设置主窗体大小属性
frame.setBounds(200, 200, 600, 500);
// 设置主窗体布局为流式布局
frame.setLayout(new FlowLayout());
// 创建一个有具体列大小的文本框
textField = new TextField(40);
// 创建主窗体按钮
button = new Button("转到");
// 创建文本区域
textArea = new TextArea(30, 70);
// 添加属性到主窗体
frame.add(textField);
frame.add(button);
frame.add(textArea);
// 设置事件
mainEvent();
// 设置主窗体显示
frame.setVisible(true);
}
private void mainEvent() {
// 设定主窗体关闭事件
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// 设置文本框中enter键的作用
textField.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
showDir();
}
}
});
// 设置”转到“按钮事件
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showDir();
}
});
}
// 设置对话框中所以的事件
private void minorEvent() {
// 设置对话框的关闭事件
dialog.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
dialog.setVisible(false);// 让对话框不显示
}
});
// 设置确定按钮事件
okbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);// 让对话框不显示
}
});
// 设置确定按钮enter事件
okbutton.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
dialog.setVisible(false);// 让对话框不显示
}
}
});
}
// 显示目录的方法
private void showDir() {
String dirPath = textField.getText();// 得到输入的路径
File file = new File(dirPath);// 把路径封装到File对象中
// 判断是否存在,已经是否是目录,提高代码的健壮性
if (file.exists() && file.isDirectory()) {
textArea.setText("");// 情况文本区域
String[] names = file.list();
for (String name : names) {
textArea.append(name + "\r\n");
}
} else {
// 创建对话框窗体
dialog = new Dialog(frame, "提示信息-self", true);
// 设置对话框属性
dialog.setBounds(100, 200, 300, 100);
// 设置对话框布局
dialog.setLayout(new FlowLayout());
// 创建对话框文本
label = new Label();
// 创建确定按钮
okbutton = new Button("确定");
String info = "你输入的信息" + dirPath + "有误";
label.setText(info);
// 关联控件
dialog.add(label);
dialog.add(okbutton);
// 加载事件
minorEvent();
// 设置显示
dialog.setVisible(true);
}
}
public static void main(String[] args) {
new MywindowDemo();
}
}二、菜单:Menu
1、Menu:菜单,Menu extends MenuItem;有右三角的图标存在,可添加Menu和MenuItem,也就是可以添加条目和菜单,当添加菜单时也就有了子菜单,也就有了小三角。
2、MenuBar:菜单栏,可添加菜单和条目
3、MenuItem:条目,及菜单项目,无右三角的图标存在,是最终的菜单项。
4、菜单的事件处理和组件一样,可以对类型为MenuItem和Menu的对象这个事件源添加活动监听ActionListener,并进行相关的事件处理。
看实例:/*
* 实现菜单
*/
public class MenuDemo {
private Frame f;
private MenuBar menuBar;// MenuBar菜单条,下面放菜单menu
// menu菜单下即可以放menu也可以放menuitem,放menu默认会有个箭头,默认是会有子菜单的
private Menu mainMenu, secondMenu;
private MenuItem item1, item2;// menuitem菜单中最终条目
MenuDemo() {
init();
}
public void init() {
f = new Frame("my window");
f.setBounds(200, 100, 400, 500);
f.setLayout(new FlowLayout());
menuBar = new MenuBar();
mainMenu = new Menu("文件");
secondMenu = new Menu("含子菜单");
item1 = new MenuItem("关闭");
item2 = new MenuItem("第二级子菜单");
f.setMenuBar(menuBar);
menuBar.add(mainMenu);
mainMenu.add(secondMenu);
mainMenu.add(item1);
secondMenu.add(item2);
myEvent();
f.setVisible(true);
}
public void myEvent() {
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
item1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
new MenuDemo();
}
}第三 练习
一、jar包双击执行:
既然是图形化界面,就需要通过图形化界面的形式运行程序,而不是是用Dos命令行执行,那么如何通过双击程序就执行程序呢?这就需要将程序的class文件打包,步骤如下:
1、首先要在java文件中导入一个包,没有则需创建一个包,如package mymenu;也就是给java文件分配一个目录
2、生成包:通过编译javac -d c:\myclass MenuTest.java,此时则在c盘下的myclass文件夹下生成了所有的.class文件
3、在此目录下新建一个文件,如1.txt或者其他任意名称任意扩展名的文件都可,然后在其中编辑固定的格式:“Main-Class: mymenu.MenuDemo”,只写引号中的内容。注意:需要需要在冒号后有一个空格,在文件末尾要回车。
4、编译:jar -cvfm my.jar 1.txt mymenu即可。如果想添加其他信息,则直接编译jar即可得出相应的命令
5、此时双击即可执行。
说明:
1)在固定格式中:
a.如果无空格:在编译的时候,就会报IO异常,提示无效的头字段,即invalid header field。这说明1.txt在被IO流读取。
b.如果无回车:在列表清单.MF中不会加入相应的加载主类的信息,也就是说配置清单的属性主类名称不会加载进清单中,也就不会执行。
2)jar文件必须在系统中注册,才能运行。注册方法如下:
A.对于XP系统:
a.打开任意对话框,在菜单栏点击工具按钮,选择文件夹选项
b.选择新建--->扩展名,将扩展名设置为jar,确定
c.选择高级,可更改图标,然后点击新建,命名为open,
d.在用于可执行应用程序中,点浏览,将jdk下的bin的整个文件路径添加进来,并在路径后添加-jar即可。
B.对于win7系统:
a.改变打开方式:右击.jar文件,点击打开方式,选择默认程序为jdk下bin中的javaw.exe应用程序。
b.修改关联程序的注册表:打开注册表(win+r),找到注册表路径\HKEY_CLASSES_ROOT\Aplications\javaw.exe\shell\open\command下的字符串值,右击点修改,将值改为:"C:\Program Files\Java\jre6\bin\javaw.exe" -jar "%1"其中-jar前的路径就是javaw.exe的路径。保存
c.双击即可执行jar程序,如果仍不能执行,则需下载最新版的jdk。
示例:/*
* 简易记事本
*/
public class MenuTest {
private Frame f;
private MenuBar menuBar;
private Menu menu;
private MenuItem openItem, saveItem, closeItem;
private TextArea textArea;
private FileDialog openDia, saveDia;// 保存文件的对话框
private File file;
MenuTest() {
init();
}
public void init() {
f = new Frame("my frame");// 初始化窗口
menuBar = new MenuBar();// 初始化菜单栏
menu = new Menu("文件");// 初始化文件菜单
openItem = new MenuItem("打开");// 初始化打开条目
saveItem = new MenuItem("保存");// 初始化保存条目
closeItem = new MenuItem("关闭");// 初始化关闭条目
textArea = new TextArea();// 初始化文本区域
openDia = new FileDialog(f, "我要打开", FileDialog.LOAD);// 打开,对话框的模式
saveDia = new FileDialog(f, "我要保存", FileDialog.SAVE);// 保存,对话框的模式
// 设置属性
f.setBounds(100, 100, 600, 600);
// 关联控件
f.setMenuBar(menuBar);
menuBar.add(menu);
menu.add(openItem);
menu.add(saveItem);
menu.add(closeItem);
f.add(textArea);
// 加载事件
myEvent();
// 设置显示
f.setVisible(true);
}
/*
* 时间处理方法
*/
private void myEvent() {
// 设置窗体的关闭事件
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// 设置菜单中的关闭事件
closeItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// 设置菜单中打开事件
openItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
openDia.setVisible(true);// 设置打开对话框显示
// 获取目录
String dirPath = openDia.getDirectory();
// 获取文件名
String name = openDia.getFile();
// 点击对话栏的“取消”按钮,设定
if (dirPath == null || name == null) {
return;
}
// 清空文本区域
textArea.setText("");
// 把获取到的文件封装到File对象中
file = new File(dirPath, name);
// 开始将文件中的内容读取到文本区域中
BufferedReader bfr = null;
try {
bfr = new BufferedReader(new FileReader(file));// 建立读取流
String line = "";
while ((line = bfr.readLine()) != null) {
textArea.append(line + "\r\n");
}
} catch (FileNotFoundException e1) {
new RuntimeException("文件不存在");
} catch (IOException e1) {
new RuntimeException("读取文件失败");
} finally {
if (bfr != null) {
try {
bfr.close();
} catch (IOException e1) {
new RuntimeException("关闭资源失败");
}
}
}
}
});
// 设置保存事件
saveItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (file == null) {
saveDia.setVisible(true);// 设置保存对话框显示
// 设置保存路径
String dirPath = saveDia.getDirectory();// 得到目录
String name = saveDia.getFile();// 得到文件
// 点击取消按钮
if (dirPath == null || name == null) {
return;
}
file = new File(dirPath, name);
}
// 开始保存文件
BufferedWriter bfw = null;
try {
bfw = new BufferedWriter(new FileWriter(file));// 建立写入流
String text = textArea.getText();// 得到需要写入文件的内容
bfw.write(text);// 写内容
bfw.flush();// 刷新缓冲区
} catch (IOException e1) {
new RuntimeException("写入文件失败");
} finally {
if (bfw != null) {
try {
bfw.close();
} catch (IOException e1) {
new RuntimeException("关闭资源失败");
}
}
}
}
});
}
public static void main(String[] args) {
new MenuTest();
}
}
本文详细介绍Java图形用户界面(GUI)编程的基础知识,包括GUI与CLI的区别、Java中实现GUI的两大包(AWT和Swing)特性,以及如何使用布局管理器创建窗口界面。此外,还介绍了事件监听机制和常用组件的使用方法。
1276

被折叠的 条评论
为什么被折叠?



