UI ——user interface(用户接口)
GUI——graphical user interface (图形用户接口)
java为GUI提供的对象都在java.Awt和java.Swing两个包中
Label | 标签,用来显示文本,不可编辑 | Toolbar | 工具栏 | deactive | 钝化的 |
Button | 按钮 | Statusbar | 状态栏 | TextField | 文本框,单行 |
Checkbox | 复选框(比如在方框中打√,可多选) | Titlebar | 标题栏 | TextArea | 文本域,多行 |
RadioBox | 单选按钮(一组按钮中选一个) | Scrollbar | 滚动条 | password | 密码 |
Menubar | 菜单栏 | Taskbar | 任务栏 | Dialog | 对话框 |
Menu | 菜单 | active | 激活的 | MenuItem | 菜单项 |
自创记事本:
package gUIDemo;
import java.awt.event.MouseAdapter;
public class GUIDemo extends MouseAdapter {
public static void main(String[] args) {
MyWindow w = new MyWindow();
}
}
package gUIDemo;
import java.awt.Component;
import java.awt.FileDialog;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
* 窗体
*/
public class MyWindow extends JFrame {
private static final long serialVersionUID = 1L;
//成员变量
private JButton btnSave;
private JButton btnClose;
private JButton btnOpen;
//文本域
private JTextArea textArea;
//滚动面板
private JScrollPane scrollPane;
/*
* 构造函数
*/
public MyWindow() {
iniWindow();
}
private void iniWindow() {
// 设置窗体大小
this.setSize(600, 400);
// 设置窗口位置
this.setLocation(500, 200);
// 设置布局管理器
this.setLayout(null);// 不设置
//创建适配器对象
MyButtonAdaptor mba = new MyButtonAdaptor();
// 鼠标点击事件
btnSave = new JButton("保存");
btnSave.addMouseListener(mba);
//设置按钮边界
btnSave.setBounds(220, 300, 100, 50);
this.add(btnSave);
btnClose = new JButton("关闭");
btnClose.setBounds(340, 300, 100, 50);
this.add(btnClose);
btnClose.addMouseListener(mba);
btnOpen = new JButton("打开");
btnOpen.setBounds(100, 300, 100, 50);
this.add(btnOpen);
btnOpen.addMouseListener(mba);
//创建JtextArea
textArea = new JTextArea();
textArea.setBounds(0, 0, 500, 300);
//this.add(textArea);
//滚动面板
scrollPane = new JScrollPane(textArea);
scrollPane.setBounds(0, 0, 500, 300);
this.add(scrollPane);
// 设置可见性
this.setVisible(true);
}
/*
* 自定义按钮处理程序
*/
class MyButtonAdaptor extends MouseAdapter{
//单击
public void mouseClicked(MouseEvent e) {
Component comp = e.getComponent();
//是否是保存按钮
if(comp == btnSave){
System.out.println("点击了保存按钮");
String cont = textArea.getText();
try {
FileOutputStream fos = new FileOutputStream("d:/xxx.txt");
fos.write(cont.getBytes());
fos.close();
textArea.setText("");
} catch (Exception e1) {
e1.printStackTrace();
}
}
else if(comp == btnClose){
System.out.println("点击了关闭按钮");
}
else if(comp == btnOpen){
FileDialog fd = new FileDialog(MyWindow.this);
fd.setVisible(true);
String fname = fd.getFile();
BufferedReader br = null;
if(fname != null){
try {
File f = new File(fd.getDirectory(), fname);
br = new BufferedReader(new InputStreamReader(new FileInputStream(f), "gbk"));
String line = null ;
while((line = br.readLine()) != null){
String old = textArea.getText();
old = old == null ? "" : old ;
textArea.setText(old + "\r\n" + line);
}
}
catch (Exception e1) {
e1.printStackTrace();
}
finally {
if(br != null) {
try {
br.close();
}
catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
}
}
}