黑马程序员 Java面向对象——GUI用户图形界面


面向对象

GUI(图形用户界面)

GUIGraphical User Interface(图形用户接口)。

用图形的方式,来显示计算机操作的界面,这样更方便更直观。


CLICommand line User Interface (命令行用户接口)

就是常见的Dos命令行操作。需要记忆一些常用的命令,操作不直观。

举例:

比如:创建文件夹,或者删除文件夹等
Java为GUI提供的对象都存在Java.Awt和Javax.Swing两个包中。

GUI(Awt和Swing)

Awt与 Swing

Java.Awt:Abstract Window ToolKit (抽象窗口工具包)。

需要调用本地系统方法实现功能,属于重量级控件。

Javax.Swing:在AWT的基础上,建立的一套图形界面系统。

其中提供了更多的组件,而且完全由Java实现。增强了移植性,属轻量级控件。

继承关系

Component

|--Button(按钮)

|--Label(标签)

|--Checkbox(复选框)

|--TextComponent(文本框)

|--TextArea(单行文本框)

|--TextField(多行文本框)

|--Container(容器,特殊组件,可通过add方法添加其他组件)

|--Panel(画板)

|--Window(窗口)

|--Frame(窗体)

|--Dialog(对话框)

|--FileDialog(文件对话框)

布局管理器

容器中的组件的排放方式,就是布局。

常见的布局管理器: 

FlowLayout(流式布局管理器)

从左到右的顺序排列。(中左右)

Panel默认的布局管理器。

BorderLayout(边界布局管理器)

东,南,西,北,中。

Frame默认的布局管理器。

GridLayout(网格布局管理器)

规则的矩阵。

CardLayout(卡片布局管理器)

选项卡。

GridBagLayout(网格包布局管理器)

非规则的矩阵。

(图形化布局基本设计)

类 Component

 void setLocation(int x, int y) 
          将组件移到新位置。
 void setSize(int width, int height) 
          调整组件的大小,使其宽度为 width,高度为 height
 void setVisible(boolean b) 
          根据参数 b 的值显示或隐藏此组件。

类 Container

 Component add(Component comp)
          将指定组件追加到此容器的尾部。

类 Frame

构造方法摘要
Frame()
          构造一个最初不可见的 Frame 新实例()。
Frame(String title)
          构造一个新的、最初不可见的、具有指定标题的 Frame 对象。

类 Button

构造方法摘要
Button()
          构造一个标签字符串为空的按钮。
Button(String label)
          构造一个带指定标签的按钮。

类 FlowLayout

构造方法摘要
FlowLayout()
          构造一个新的 FlowLayout,它是居中对齐的,默认的水平和垂直间隙是 5 个单位。

import java.awt.Frame;

import java.awt.Button;

import java.awt.FlowLayout;

/*
创建图形化界面:
1.创建frame窗体。
2.对窗体进行基本设置。
比如大小,位置,布局。
3.定义组件。
4.将组件通过窗体的add方法添加到窗体中。
5.让窗体显示。
*/

public class Test{
public static void main(String...args){
Frame f =new Frame("我的电脑");
f.setSize(1025,550);
f.setLocation(0,100);
f.setLayout(new FlowLayout());

Button b = new Button("360安全卫士");
f.add(b);
f.setVisible(true);
System.out.println("Hello 新长城");
}

}

(事件监听机制)

事件监听机制的特点:

1,事件源。

2,事件。

3,监听器。

4,事件处理。

事件源:就是Awt包或者Swing包中的那些图形界面组件。

事件:每一个事件源都有自己特有的对应事件和共性事件。
监听器:将可以触发某一个事件的动作(不只一个动作)都已经封装到了监听器中。

以上三者,在Java中都已经定义好了。
直接获取其对象来用就可以了。


我们要做的事情是,就是对产生的动作进行处理。

(自定义事件事件处理方式)

import java.awt.Frame;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

类 Window

void addWindowListener(WindowListener l)
          添加指定的窗口侦听器,以从此窗口接收窗口事件。

接口 WindowListener 类 WindowAdapter

 void windowActivated(WindowEvent e)
          将 Window 设置为活动 Window 时调用。
 void windowClosed(WindowEvent e)
          因对窗口调用 dispose 而将其关闭时调用。
 void windowClosing(WindowEvent e)
          用户试图从窗口的系统菜单中关闭窗口时调用。
 void windowDeactivated(WindowEvent e)
          当 Window 不再是活动 Window 时调用。
 void windowDeiconified(WindowEvent e)
          窗口从最小化状态变为正常状态时调用。
 void windowIconified(WindowEvent e)
          窗口从正常状态变为最小化状态时调用。
 void windowOpened(WindowEvent e)
          窗口首次变为可见时调用。

类 WindowEvent

public class Test{

public static void main(String...args){

Frame f =new Frame("我的电脑");

f.setSize(1025,550);

f.setLocation(0,100);

f.setLayout(new FlowLayout());

Button b =new Button("360安全卫士");

f.add(b);

f.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.out.println("我要退出了");

System.out.println(e.toString());

System.exit(0);

}

public void windowActivated(WindowEvent e){

System.out.println("激活");

}  

public void windowOpened(WindowEvent e){

System.out.println("打开");

});

f.setVisible(true);

System.out.println("Hello 新长城");

}

}
/*
WinodwListener接口
abstract class WindowAdapter implements WindowListener{

void windowActivated(WindowEvent e){}

void windowClosed(WindowEvent e){}

void windowClosing(WindowEvent e){}

void windowDeactivated(WindowEvent e){} 

void windowDeiconified(WindowEvent e){}

void windowIconified(WindowEvent e){}

void windowOpened(WindowEvent e){}

}
*/
/*
class MyWin implements WindowListener{

覆盖7个方法。可以我只用到了关闭的动作。

其他动作都没有用到,可是却必须复写。

}

*/
/*

因为WindowListener的子类WindowAdapter已经实现了WindowListener接口。
并覆盖了其中的所有方法。那么我只要继承自Windowadapter覆盖我需要的方法即可。

*/

/*
class MyWin extends WindowAdapter{

public void windowClosing(WindowEvent e){

System.out.println("window closing---"+e.toString());

System.exit(0);

}

}

*/

化难为易:

1)、确定事件源(容器或组件)

2、通过事件源对象的addxxxListener()方法将监听器注册到该事件源上。

3、该方法中接收XXXListener对象,或者XXXListener的子类XXXAdapter对象。

4、一般用匿名内部类来表示。

5、再覆盖方法的时候,方法的参数一般是XXXEvent类型的变量接收。

6、一般XXXListener方法超过3个或者3个就一定会有一个XXXAdapter子类对象。(也称为适配器)

(按钮触发事件)

import java.awt.Frame;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

接口 ActionListener

方法摘要
 void actionPerformed(ActionEvent e)
          发生操作时调用。

类 ActionEvent

public class Test{

//定义该图形中所需的组件的引用。

private Frame f ;

private Button b ;

Test(){

init();

}

public void init(){

f =new Frame("我的电脑");

//对frame进行基本设置。

f.setSize(1025,550);

f.setLocation(0,100);

f.setLayout(new FlowLayout());

b =new Button("退出");

//将组件添加到frame中

f.add(b);

//加载一下窗体上事件。

myEvent();

//显示窗体;

f.setVisible(true);

}

private void myEvent(){

f.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.out.println("我要退出了");

System.out.println(e.toString());

System.exit(0);

}

public void windowActivated(WindowEvent e){

System.out.println("激活");

}  

public void windowOpened(WindowEvent e){

System.out.println("打开");

});

//让按钮具备退出程序的功能

/*

按钮就是事件源。

那么选择哪个监听器呢?

通过关闭窗体示例了解到,想要知道哪个组件具备什么样的特有监听器。

需要查看该组件对象的功能。

通过查阅Button的描述。发现按钮支持一个特有监听addActionListener。

*/

b.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

System.out.println("退出");

System.exit(0);

}

});

}

public static void main(String...args){

new Test();

System.out.println("Hello 新长城");

}

}

(鼠标监听事件)
import java.awt.Frame;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

类 Component

 void addMouseListener(MouseListener l)
          添加指定的鼠标侦听器,以接收发自此组件的鼠标事件。

接口 MouseListener 类 MouseAdapter

 void mouseClicked(MouseEvent e)
          鼠标按键在组件上单击(按下并释放)时调用。
 void mouseEntered(MouseEvent e)
          鼠标进入到组件上时调用。
 void mouseExited(MouseEvent e)
          鼠标离开组件时调用。
 void mousePressed(MouseEvent e)
          鼠标按键在组件上按下时调用。
 void mouseReleased(MouseEvent e)
          鼠标按钮在组件上释放时调用。

public class Test{

private Frame f ;

private Button b ;

Test(){

init();

}

public void init(){

f =new Frame("我的电脑");

f.setSize(1025,550);

f.setLocation(0,100);

f.setLayout(new FlowLayout());

b =new Button("360安全卫士");

f.add(b);

myEvent();

f.setVisible(true);

}

private void myEvent(){
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.out.println("我要退出了");
System.exit(0);
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//System.out.println("活动 action ok");
}
});
b.addMouseListener(new MouseAdapter(){
private int count = 1;
private int clickCount =1;
public void mouseEntered(MouseEvente){
//System.out.println("鼠标来了"+count++);
//System.exit(0);
public void mouseExited(MouseEvent e) {
//System.out.println("鼠标走了");
}
public void mouseClicked(MouseEvent e){
if(e.getClickCount()==2)
System.out.println("鼠标双击");
//System.out.println("鼠标单击"+clickCount++);
});
}
public static void main(String...args){

new Test();

System.out.println("Hello 新长城");

}

}

一定要用面向对象思考问题。

鼠标监视器应该属于所有组件所具备的,所以鼠标减速器应该定义在Component里面

鼠标所点击的次数记录,应该定义在鼠标事件里面。

注意:

按钮的获得监听应该定义在按钮里面。

按钮活动和按钮被点击是按钮点击先执行,因为按键点击更为具体。

活动监听非得用鼠标,用键盘也是可以的。

Event 事件 Listener 监听器 Adapter 适配器

(键盘监听事件)

import java.awt.Frame;

import java.awt.Button;

import java.awt.FlowLayout;

import java.awt.TextField;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

类 Component

 void addKeyListener(KeyListener l)
          添加指定的按键侦听器,以接收发自此组件的按键事件。

接口 KeyListener 类 KeyAdapter

 void keyPressed(KeyEvent e)
          按下某个键时调用此方法。
 void keyReleased(KeyEvent e)
          释放某个键时调用此方法。
 void keyTyped(KeyEvent e)
          键入某个键时调用此方法。

类 TextField

构造方法摘要
TextField()
          构造新文本字段。

public class Test{

private Frame f ;

private Button b ;

private TextField tf;

Test(){

init();

}

public void init(){

f =new Frame("我的电脑");

f.setSize(1025,550);

f.setLocation(0,100);

f.setLayout(new FlowLayout());

b =new Button("360安全卫士");

f.add(b);

tf =new TextField(10);

f.add(tf);

myEvent();

f.setVisible(true);

}

private void myEvent(){

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

}

}

});

f.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.out.println("我要退出了");

System.exit(0);

}

});

/*

b.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

//System.out.println("action ok");

}

});

b.addMouseListener(new MouseAdapter(){

private int count = 1;

private int clickCount =1;

public void mouseEntered(MouseEvent e){

//System.out.println("鼠标来了"+count++);

//System.exit(0);

public void mouseExited(MouseEvent e) {

//System.out.println("鼠标走了");

}

public void mouseClicked(MouseEvent e){

if(e.getClickCount()==2)

System.out.println("鼠标双击");

//System.out.println("鼠标单击"+clickCount++);

});

//给Button添加一个键盘监听

b.addKeyListener(new KeyAdapter(){

public void keyPressed(KeyEvent e){

//if(e.getKeyText(e.getKeyCode())=="Esc")

//if(e.getKeyCode()==27)

//组合键

if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)

System.exit(0);

System.out.println(KeyEvent.getKeyText(e.getKeyCode()));

System.out.println(e.getKeyChar()+"..."+e.getKeyCode());

}

});

*/

}

public static void main(String...args){

new Test();

System.out.println("Hello 新长城");

}

}

GUI 练习-列出指定目录内容

import java.awt.Frame;

import java.awt.Button;

import java.awt.FlowLayout;

import java.awt.TextField;

import java.awt.TextArea;

import java.awt.Label;

import java.awt.Dialog;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

类 TextArea

构造方法摘要
TextArea()
          构造一个将空字符串作为文本的新文本区。

类 Label

构造方法摘要
Label()
          构造一个空标签。

类 Dialog

构造方法摘要
Dialog(Frame owner,String title, boolean modal)
          构造一个最初不可见的 Dialog,它带有指定的所有者 Frame、标题和模式。

public class Test{

private Frame f ;

private Button b ;

private TextField tf;

private TextArea ta;

private Dialog d;

private Label lab;

private Button buf;

Test(){

init();

}

public void init(){

f =new Frame("Window");

f.setBounds(0,100,1025,550);

f.setLayout(new FlowLayout());

tf =new TextField(100);

ta =new TextArea("Hello",28,107);

b =new Button("转到");

d =new Dialog(f,"提示信息-self",true);

d.setBounds(400,250,300,200);

d.setLayout(new FlowLayout());

lab =new Label();

buf =new Button("确定");

d.add(lab);

d.add(buf);


f.add(tf);

f.add(b);

f.add(ta);


myEvent();

f.setVisible(true);

}

private void myEvent(){

tf.addKeyListener(new KeyAdapter(){

public void keyPressed(KeyEvent e){

if(e.getKeyCode()==KeyEvent.VK_ENTER)

showDir();

}

});

buf.addKeyListener(new KeyAdapter(){

public void keyPressed(KeyEvent e){

if(e.getKeyCode()==KeyEvent.VK_ENTER)

d.setVisible(false);

}

});

buf.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

d.setVisible(false);

}

});

d.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){ 

d.setVisible(false);

}

});

b.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

showDir();

}

});

f.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){ 

System.exit(0);

}

});

}

public void showDir(){

String dirPath = tf.getText();

File dir =new File(dirPath);

if(dir.exists()&&dir.isDirectory()){

String[] names = dir.list();

ta.setText("");

for(String name : names){

ta.append(name+"\r\n");

}

}

else{

String info ="你输入的信息:"+dirPath+"是错误的 请重新输入";

lab.setText(info);

d.setVisible(true);

}

}

public static void main(String...args){

new Test();

System.out.println("Hello 新长城");

}

}

(GUI 练习-记事本小软件)

import java.awt.Frame;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.TextArea;
import java.awt.Dialog;
import java.awt.MenuBar;
import java.awt.Menu;
import java.awt.MenuItem;
import java.awt.FileDialog;
import java.io.File;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

类 MenuBar

构造方法摘要
MenuBar()
          创建新的菜单栏。

类 Menu

构造方法摘要
Menu(String label)
          构造具有指定标签的新菜单。

类 MenuItem

构造方法摘要
MenuItem(String label)
          构造具有指定的标签且没有键盘快捷方式的新菜单项。

类 Frame

 void setMenuBar(MenuBar mb)
          将此窗体的菜单栏设置为指定的菜单栏。

package Menu;

public class Test {

private Frame window;

private MenuBar bar;

private Menu file;

private MenuItem openItem,saveItem,closeItem;

private FileDialog openDia,saveDia;

private TextArea ta;

private File f;

Test(){

init();

}

public void init(){

window =newFrame("Window");

window.setBounds(0,100,1025,550);

//window.setLayout(new FlowLayout());


ta =new TextArea();

bar =new MenuBar();

file =new Menu("文件");

openItem =new MenuItem("打开");

saveItem =new MenuItem("保存");

closeItem =new MenuItem("关闭");


bar.add(file);

file.add(openItem);

file.add(saveItem);

file.add(closeItem);


openDia =new FileDialog(window,"打开",FileDialog.LOAD);

saveDia =new FileDialog(window,"另存为",FileDialog.SAVE);

window.add(ta);

window.setMenuBar(bar);

myEvent();

window.setVisible(true);

}

private void myEvent(){

openItem.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

openDia.setVisible(true);

String dirPath = openDia.getDirectory();

String fileName = openDia.getFile();

if(dirPath==null || fileName==null)

return ;

ta.setText("");

f =new File(dirPath,fileName);

try{

printFile(f);

}

catch (Exception ex){

throw new RuntimeException("读取失败");

}

}

});

saveItem.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

if(file!=null){

saveDia.setVisible(true);

String dirPath = saveDia.getDirectory();

String fileName = saveDia.getFile();

if(dirPath==null || fileName==null)

return ;

f =new File(dirPath,fileName);

}

try{

writeFile(f);

}

catch (IOExceptionex){

throw new RuntimeException("另存为失败");

}

}

});

closeItem.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

System.exit(0);

}

});

window.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){ 

System.exit(0);

}

});

}

private voidwriteFile(File file)throws IOException{


BufferedWriter bufw = 

new BufferedWriter(new FileWriter(file));

String text = ta.getText();

bufw.write(text);

bufw.close();

}

private void printFile(File file)throws IOException{

BufferedReader bufr = 

new BufferedReader(new FileReader(file));

String line =null;

while((line=bufr.readLine())!=null){

ta.append(line+"\r\n");

}

bufr.close();

}

public static void main(String... args) {

new Test();

}

}

制作可以双击执行的Jar包

1,将多个类封装到了一个包(package)中。

2,定义一个Jar包的配置信息。

定义一个文件1.properties 。文件内容内容为:Main-Class:(空格)包名.类名(回车)

3,将多个class文件封装到一个目录中,打成Jar包。

Jar -cvfm my.Jar 1.properties 包名

4,通过Win RAR程序进行验证,查看该Jar的配置文件中是否有自定义的配置信息。

5,通过工具--文件夹选项--文件类型--Jar类型文件,通过高级,定义该Jar类型文件的打开动作的关联程序。

JDK\Bin\Javaw.exe -Jar

6,双击试试。


相关内容