一、GUI概述
1、GUI与CLI
1) GUI(Graphical User Interface):图形用户接口,用图形的方式,来显示计算机操作的界面,这样更方便更直观。
GUI编程通常为桌面应用编程,不用于WEB编程。
2) CLI(Command Line User Interface) :命令行用户接口)就是常见的DOS命令行操作。需要记忆一些常用的命令,操作不直观。
2、AWT与swing
1) AWT(Abstract Window ToolKit): 抽象窗口工具包。
a、不属于扩展包。
b、需要调用本地系统方法实现功能。
c、依赖于本地。
d、属重量级控件。
2) swing:javax扩展工具包。
a、属于扩展包。
b、在AWT的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全由Java实现。
c、增强了移植性。
d、属轻量级控件。
3、继承体系
在GUI当中一切均为组件。
左侧为容器组件
右侧为具体内容组件(基本组件)
2、容器与内容
1) 所有GUI画面均由容器与内容组成。
2) 容器内可以包含容器。
3) 容器内可以包含基本组件。
二、容器类:主要容器为Container:
1、Window窗口,单独窗体容器。可以单独使用。
2、Frame:基本窗体框架。
Dialog:对话框。
3、Panel :面板容器。无法单独使用。存在于其他容器当中。
4、 以Frame为例,完成窗体创建步骤:
第一步:创建窗体对象:Frame frame = new Fram();
第二步:设置窗体属性:frame.setLayout(new FlowLayout());
第三步:让窗体可见:frame.setVisible();
5、创建窗体对象方法:
public Frame() throws HeadlessException
public Frame(String title) throws HeadlessException
6、设置窗体可见方法:
public void setVisible(boolean b)
public void show() 已过时
7、设置窗体属性方法:
public void setResizable(boolean resizable) 设置是否由用户调节窗体大小
public void setTitle(String title) 设置窗体标题,注意与设置名称不同。名称为组件在代码中的名称。并非显示出来。
public void setSize(int width, int height) 设置窗体尺寸
public void setSize(Dimension d) 设置窗体尺寸
public void setLocation(int x, int y) 设置窗体起始位置
public void setLocation(Point p)设置窗体起始位置
public void setBackground(Color c) 设置组件背景颜色
public void setIconImage(Image image)设置title小图标
8、Toolkit类(awt工具包)
public static Toolkit getDefaultToolkit() 获取工具包对象
public abstract Image getImage(String filename) 获取图片
9、设置窗体属性方法:
public void setLayout(LayoutManager mgr)设置布局方式
10、LayoutManager:不同的布局管理器,使用不同的子类对象
A、布局即组件的排列方式。
B、FlowLayout(流式布局管理器)它是Panel默认的布局管理器。
C、BorderLayout(边界布局管理器)它是Frame默认的布局管理器。
D、GridLayout(网格布局管理器)
E、GridBagLayout(网格包布局管理器)
F、CardLayout(卡片布局管理器)
11、容器添加组件方法
public Component add(Component comp)
public void setMenuBar(MenuBar mb)
12、组件获取焦点方法
public void requestFocus()
13、JOptionPane
showXXXDialog返回三种基本对话框(模态窗口)
showMessageDialog消息通知
showConfirmDialog询问确定
showInputDialog要求输入
三、基本组件
1、大部分组件设置方式类似窗体属性设置。
2、主要基本组件:
Button:按钮
Checkbox:复选框
Choice:单选框
TextComponent:
TextArea:文本域
TextField:文本框(宽度设置为public TextField(int columns))
菜单组件:MenuBar:菜单条MenuItem:菜单项 Menu:菜单
四、事件监听机制
1、针对于某个组件完成对应的事件监听,当用户完成了对应的事件则触发执行对应代码。
2、事件监听机制组成:
A、事件源:被监听的组件
B、事件:具体的用户动作事件
C、监听器:用来监听事件源(大量使用了适配器模式)
D、事件处理:处理事件的代码
五、组件对应监听机制
frame窗体监听:
public void addWindowListener(WindowListener l)窗体事件
WindowListener/WindowAdapter方法:
void windowClosing(WindowEvent e)关闭窗口时调用方法
public void addKeyListener(KeyListener l) 键盘事件
KeyListener方法:
void keyPressed(KeyEvent e) 按下某按键时调用方法
KeyEvent:键盘事件抽象类
public char getKeyChar()获取按下的字符
public void consume() 反悔事件默认的处理
六、代码
package gui;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class MyWindowDemo {
private Frame frame;
private MenuBar menuBar;
private Menu fileMenu,editMenu,helpMenu;
private MenuItem openMenuItem,saveMenuItem,closeMenuItem;
private Menu changeMenu;
private MenuItem menuItem1,menuItem2,menuItem3,notepadMenuItem;
private Dialog dialog;
private Label label;
private Button okButton;
private FileDialog openDialog,saveDialog;
private File file;
private TextArea textArea;
private TextField textField;
private Button button,blueButton,redButton;
public MyWindowDemo(){
init();
}
//窗口中组件添加
public void init() {
frame = new Frame("MyFrame");
frame.setBounds(400,140,600,450);
frame.setLayout(new FlowLayout());
menuBar = new MenuBar();
frame.setMenuBar(menuBar);
fileMenu = new Menu("File");
menuBar.add(fileMenu);
openMenuItem = new MenuItem("Open");
fileMenu.add(openMenuItem);
saveMenuItem = new MenuItem("Save");
fileMenu.add(saveMenuItem);
closeMenuItem = new MenuItem("Close");
fileMenu.add(closeMenuItem);
editMenu = new Menu("Edit");
menuBar.add(editMenu);
changeMenu = new Menu("ChangeTitle");
editMenu.add(changeMenu);
menuItem1 = new MenuItem("Good Good Study");
changeMenu.add(menuItem1);
menuItem2 = new MenuItem("Day Day Up");
changeMenu.add(menuItem2);
menuItem3 = new MenuItem("Good Lucky");
changeMenu.add(menuItem3);
notepadMenuItem = new MenuItem("Notepad");
editMenu.add(notepadMenuItem);
helpMenu = new Menu("Help");
menuBar.add(helpMenu);
textField = new TextField(65);
frame.add(textField);
button = new Button("Click");
frame.add(button);
textArea = new TextArea(20,80);
frame.add(textArea);
blueButton = new Button("Blue");
frame.add(blueButton);
redButton = new Button("Red");
frame.add(redButton);
//Dialog
dialog = new Dialog(frame,"mistake message",true);
dialog.setBounds(550,300,250,100);
dialog.setLayout(new FlowLayout());
label = new Label();
dialog.add(label);
okButton = new Button("OK");
dialog.add(okButton);
//FileDialog,操作后弹出FileDialog对话框
openDialog = new FileDialog(frame,"Open FileDialog",FileDialog.LOAD);
saveDialog = new FileDialog(frame,"Save FileDialog",FileDialog.SAVE);
myEvent();
frame.setVisible(true);
}
public void myEvent() {
//窗口监听
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
//活动监听
closeMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//键盘监听
// textField.addKeyListener(new KeyAdapter(){
// public void keyPressed(KeyEvent e){
// char ch = e.getKeyChar();
// if(!('0'<=ch && ch<='9'))//只能输入0--9的数
// public void consume()使用此事件,以便不会按照默认的方式由产生此事件的源代码来处理此事件。
// e.consume();
// textArea.append(ch+"");
// }
// });
//显示目录里面的内容
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String text = textField.getText();
File file = new File(text);
if(file.exists() && file.isDirectory()) {
textArea.setText("");
String[] list = file.list();
for (String string : list) {
textArea.append(string+"\r\n");
}
} else {
label.setText("invalid dir:"+text);
dialog.setVisible(true);
}
}
});
okButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
}
});
dialog.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dialog.setVisible(false);
}
});
//鼠标监听
blueButton.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e){
frame.setBackground(new Color(0,255,0));
}
public void mouseExited(MouseEvent e){
frame.setBackground(new Color(255,255,255));
}
});
redButton.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e) {
frame.setBackground(new Color(255,0,0));
}
public void mouseExited(MouseEvent e){
frame.setBackground(new Color(255,255,255));
}
});
//打开记事本
notepadMenuItem.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("notepad.exe");
} catch (IOException e1) {
e1.printStackTrace();
}
}});
//改Frame框标题
menuItem1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
frame.setTitle(menuItem1.getLabel());
}
});
menuItem2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
frame.setTitle(menuItem2.getLabel());
}
});
menuItem3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
frame.setTitle(menuItem3.getLabel());
}
});
//open文件
openMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
openDialog.setVisible(true);
String dirPath = openDialog.getDirectory();
String filePath = openDialog.getFile();
if(dirPath==null || filePath==null)
return;
textArea.setText("");
file = new File(dirPath,filePath);
try {
BufferedReader bufr = new BufferedReader(new FileReader(file));
String line ;
while((line=bufr.readLine())!=null){
textArea.append(line+"\r\n");
}
bufr.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
});
//save文件
saveMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(file==null){
saveDialog.setVisible(true);
String dirPath = saveDialog.getDirectory();
String filePath = saveDialog.getFile();
if(dirPath==null || filePath==null)
return;
file = new File(dirPath,filePath);
}
try {
BufferedWriter bufw = new BufferedWriter(new FileWriter(file));
String text = textArea.getText();
bufw.write(text);
bufw.flush();
bufw.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
});
}
public static void main(String[] args) {
new MyWindowDemo();
}
}
运行界面:
<img src="https://img-blog.csdn.net/20150423211532035" alt="" />

3014

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



