您的位置:首页 > 产品设计 > UI/UE

黑马程序员 Java自学总结十五 GUI

2013-08-09 15:19 375 查看
------ ASP.Net+Android+IO开发.Net培训期待与您交流! ------



总结内容来源于黑马毕老师的java基础教学视频

GUI:

Graphical User Interface(图形用户接口),用图形的方式,来显示计算机操作的界面.

Java为GUI提供的对象都在java.awtjavax.swing这两个包中。

Awt和Swing

java.awt:abstractWindow ToolKit(抽象窗口工具集,需要调用本地系统方法实现功能,属于重量级(是指跟平台结合紧密)控件。

javax.swing:在AWT的基础上,建立的一套图形界面系统,其中提供更多的组件,而且完全由java实现,增强了移植性,属于轻量级(与重量级相反)控件。

创建图形化界面:

1.创建Frame窗体

2.对窗体进行基本设置:大小setSize(横向,纵向),

位置setLocation(横坐标,纵坐标),

布局setLayout(布局类的对象).

3.定义组件.

4.将组件通过窗体的add方法添加到窗体中.

5.让窗体显示,通过setVisible(true).

事件监听机制的特点:

1.事件源:就是awt包和swing包中的图形界面组件.

2.事件:每一个事件源都有自己特有的对应事件和共性事件.

事件包括:窗体事件,Action事件,鼠标事件,键盘事件等.

3.监听器:将可以触发某一个事件的动作(不止一个)都已经封装到了监听器中.

以上三者,在java中都已经定义好.直接获取对象用就可以了

4.事件处理:我们只需要对产生的动作进行处理.

[java] view
plaincopy

import java.awt.*;

import java.awt.*;

import java.awt.event.*;

class AwtDemo

{

public static void main(String[] args)

{

Frame f = new Frame("my awt");

f.setSize(500,400);//设置Frame大小

f.setLocation(500,200);//设置Frame位置

//设置Frame布局

f.setLayout(new FlowLayout());

Button b = new Button("按钮");

f.add(b);//把按钮组件添加到Frame中

//添加监听事件

f.addWindowListener(new MyWin());

f.setVisible(true);//设置Frame可见

}

}

class MyWin extends WindowAdapter

{

//复写windowClosing方法,实现关闭动作

public void windowClosing(WindowEvent e)

{

System.out.println("我被关闭了");

System.exit(0);

}

//复写windowActivated方法

public void windowActivated(WindowEvent e)

{

System.out.println("我被激活了");

}

//复写windowOpened方法

public void windowOpened(WindowEvent e)

{

System.out.println("我被打开了");

}

}

组件Frame和Button触发事件

[java] view
plaincopy

import java.awt.*;

import java.awt.event.*;

//想要知道组件具备什么样的监听器,需要查看该组件的对象的功能.

class FrameDemo

{

private Frame f;

private Button b;

FrameDemo()

{

init();

}

public void init()

{

f = new Frame("my frame");

//对frame进行基本设置,setBounds()相当于setLocation()和setSize()

f.setBounds(300,100,500,400);

f.setLayout(new FlowLayout());

b = new Button("my button");

//将按钮组件添加到frame中

f.add(b);

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

MyEvent();

//显示frame

f.setVisible(true);

}

public void MyEvent()

{

f.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

System.out.println("退出,是按钮干的");

System.exit(0);

}

});

}

public static void main(String[] args)

{

new FrameDemo();

}

}

GUI--鼠标和键盘事件

[java] view
plaincopy

import java.awt.*;

import java.awt.event.*;

class MouAndKeyDemo

{

private Frame f;

private Button b;

private TextField tf;

MouAndKeyDemo()

{

init();

}

public void init()

{

f = new Frame("my frame");

//对frame进行基本设置,setBounds()相当于setLocation()和setSize()

f.setBounds(300,100,500,400);

f.setLayout(new FlowLayout());

//建立一个文本框对象,设定长度20,并添加到frame中

tf = new TextField(20);

f.add(tf);

//将按钮组件添加到frame中

b = new Button("my button");

f.add(b);

//调用我的事件方法

MyEvent();

//显示frame

f.setVisible(true);

}

public void MyEvent()

{

//为TextField添加一个键盘监听器

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

}

}

});

//为farme添加一个窗口监听器

f.addWindowListener(new WindowAdapter()

{

//复写WindowAdapter类中的'关闭窗口'方法

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

//为button添加一个键盘监听器

b.addKeyListener(new KeyAdapter()

{

//复写KeyAdapter类中的'键盘键入'方法

public void keyPressed(KeyEvent e)

{

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

System.exit(0);

String s = e.getKeyText(e.getKeyCode());

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

}

});

//为button添加一个活动监听器

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

System.out.println("action on");

}

});

//为button添加一个鼠标监听器

b.addMouseListener(new MouseAdapter()

{

private int count = 1;

private int clickcount = 1;

//鼠标指向事件

public void mouseEntered(MouseEvent e)

{

System.out.println("鼠标进入到该组件"+count++);

}

//鼠标点击事件

public void mouseClicked(MouseEvent e)

{

if(e.getClickCount()==2)

System.out.println("鼠标双击一次"+clickcount++);

}

});

}

public static void main(String[] args)

{

new MouAndKeyDemo();

}

}

列出指定目录内容

[java] view
plaincopy

import java.awt.*;

import java.awt.event.*;

import java.io.*;

/*

会用到的知识点包括

Frame ,Button ,TextField ,TextArea ,Dialog ,Label

需求,列出指定目录的内容.

分析:

1.一个Frame,一个TextField用来输入指定路径,

2.一个Area用来存文件名,一个Button用来确认输入的路径是否正确,如果正确,

则取出TextField中输入的数据,先判断用数据是否存在,是否是目录,如果是,

用File的list()方法取出目录下的文件名称存放到String[]数组中,然后用

高级for循环遍历数组,并把文件名字存放到TextArea中.

*/

class MyWindowDemo

{

private Frame f;

private Button b;

private TextField tf;

private TextArea ta;

private Dialog dl;

private Label l;

private Button okBur;

MyWindowDemo()

{

init();

}

public void init()

{

f = new Frame("my frame");

f.setBounds(500,100,600,500);

f.setLayout(new FlowLayout());

f.setVisible(true);

tf = new TextField(65);

f.add(tf);

b = new Button("转到");

f.add(b);

ta = new TextArea(25,72);

f.add(ta);

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

dl.setBounds(400,200,240,120);

dl.setLayout(new FlowLayout());

l = new Label();

okBur = new Button("确定");

dl.add(okBur);

myEvent();

}

public void myEvent()

{

//关闭Frame

f.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

okBur.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

dl.setVisible(false);

}

});

//为TextField添加键盘监听器

tf.addKeyListener(new KeyAdapter()

{

public void keyPressed(KeyEvent e)

{

//实现敲回车就能实现Button的功能

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

{

ta.setText("");

String s = tf.getText();

File file = new File(s);

if (file.exists() && file.isDirectory())

{

String[] names = file.list();

for(String name : names)

{

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

}

tf.setText("");

}

else

{

dl.add(l);

l.setText("你输入的目录无效");

dl.setVisible(true);

tf.setText("");

}

}

}

});

//为按钮添加一个Action监听器

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String s = tf.getText();

File file = new File(s);

if (file.exists() && file.isDirectory())

{

String[] names = file.list();

for(String name : names)

{

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

}

tf.setText("");

}

else

{

dl.add(l);

l.setText("你输入的目录无效");

dl.setVisible(true);

tf.setText("");

}

}

});

}

public static void main(String[] args)

{

new MyWindowDemo();

}

}

文件的打开和保存

[java] view
plaincopy

//会用到的知识点包括

//Frame, MenuBar, Menu, MenuItem, FileDialog, TextArea

package mymenu;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

public class MyMenuDemo

{

private Frame f;

private MenuBar mb;

private Menu m;

private Menu subm;

private MenuItem mi;

private MenuItem submi;

private MenuItem openmi,savemi;

private FileDialog fd;

private File file;

private TextArea ta;

MyMenuDemo()

{

init();

}

public void init()

{

f = new Frame("my window");

f.setBounds(300,100,650,600);

ta = new TextArea();

mb = new MenuBar();

ta = new TextArea();

m = new Menu("文件");

subm = new Menu("子菜单");

mb.add(m);

openmi = new MenuItem("打开");

savemi = new MenuItem("保存");

mi = new MenuItem("退出");

submi = new MenuItem("子项");

subm.add(submi);

m.add(subm);

m.add(openmi);

m.add(savemi);

m.add(mi);

f.setMenuBar(mb);

f.add(ta);

f.setVisible(true);

myEvent();

}

public void myEvent()

{

f.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

//打开事件

openmi.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

fd = new FileDialog(f,"open",FileDialog.LOAD);

fd.setVisible(true);

try

{

ta.setText("");

//如果点了Filedialog的取消,就直接结束.

if(fd.getDirectory()==null || fd.getFile()==null)

return ;

file = new File(fd.getDirectory(),fd.getFile());

System.out.println(file);

BufferedReader br =

new BufferedReader(new FileReader(file));

String line = null;

while ((line=br.readLine())!=null)

{

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

}

br.close();

}

catch (IOException ex)

{

throw new RuntimeException("该文件类型不匹配");

}

}

});

//保存事件

savemi.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

//判断文件是否已经存在,不存在就弹出Filedialog

if (file==null)

{

fd = new FileDialog(f,"save",FileDialog.SAVE);

fd.setVisible(true);

//如果点了Filedialog的取消,就直接结束

if(fd.getDirectory()==null || fd.getFile()==null)

return ;

file = new File(fd.getDirectory(),fd.getFile());

}

try

{

BufferedWriter bw =

new BufferedWriter(new FileWriter(file));

bw.write(ta.getText());

bw.close();

}

catch (IOException ex)

{

throw new RuntimeException("创建文件失败");

}

}

});

mi.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

System.exit(0);

}

});

}

public static void main(String[] args)

{

new MyMenuDemo();

}

}

GUI双击jar包执行

写好读到GUI程序,可以封装成jar包,通过对系统进行设置,使得双击jar包后,程序可以执行。以上边的写的记事本程序为例:

操作步骤:

1.在源文件的第一行加上包:package mymenu

2.dos命令行中启动“javac -d c:\myclass MyMenuTest.java”命令。

3.在menu包所在目录下,新建一个名为“1.txt”的文本文档,里边录入一下内容:

Main-Class: mymenu.MyMenuTest

注意:1,冒号后面要有一个空格;2,末尾一定要加换行符,即敲一下Enter键。

4.在dos切换到c:\myclass目录下,启动”jar –cvfm my.jar 1.txt mymenu”命令,这时得到的jar包即可启动。

------ ASP.Net+Android+IO开发.Net培训期待与您交流! ------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: