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

黑马程序员_JAVA学习日记_JAVA中图形化界面GUI编程

2012-08-21 00:22 567 查看
黑马程序员-学习日记

黑马程序员_JAVA学习日记_JAVA中图形化界面GUI编程

------- android培训java培训、期待与您交流! ----------

一:图形化界面GUI编程:

GUI

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

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

CLI

Command line User Interface (命令行用户接口)就是常见的Dos命令行操作。

需要记忆一些常用的命令,操作不直观。

举例:比如:创建文件夹,或者删除文件夹等

Java为GUI提供的对象都存在java.Awt和javax.Swing两个包中。

Awt与Swing

java.awt:Abstract Window ToolKit (抽象窗口工具包),需要调用本地系统方法实现功能。属重量级控件。

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

Container:为容器,是一个特殊的组件,该组件中可以通过add方法添加其他组件进来。

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

常见的布局管理器:

FlowLayout(流式布局管理器)

从左到右的顺序排列。

Panel默认的布局管理器。

BorderLayout(边界布局管理器)

东,南,西,北,中

Frame默认的布局管理器。

GridLayout(网格布局管理器)

规则的矩阵

CardLayout(卡片布局管理器)

选项卡

GridBagLayout(网格包布局管理器)

非规则的矩阵

Container常用子类:Window Panel(面板,不能单独存在。)

Window常用子类:Frame Dialog

简单的窗体创建过程:

Frame f = new Frame(“my window”);

f.setLayout(newFlowLayout());

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

f.setLocation(300,200);//设置窗体出现在屏幕的位置

f.setVisible(true);

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

通过事件源对象的addXXXListener()方法将侦听器注册到该事件源上。

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

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

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

事件触发后会把事件打包成对象传递给该变量。(其中包括事件源对象。通过getSource()或者,getComponent()获取。)

MenuBar,Menu,MenuItem

先创建菜单条,再创建菜单,每一个菜单中建立菜单项。

也可以菜单添加到菜单中,作为子菜单。

通过setMenuBar()方法,将菜单添加到Frame中。

二:双击jar包执行的步骤。

1,定义配置信息文件。

Main-Class:空格包名.类名回车

2,jar-cvfm jar包名配置文件名称 需要加入到jar包中的包或者类。

3,验证,通过winrar,打开jar中的配置文件,查看是否信息已写入。

如果没有,打jar动作失败。

4,在工具-文件夹选项-文件类型,对jar类型文件进行设定,

关联jdk\bin\javaw.exe 后面要跟一个参数 -jar.

如果你的jdk是安装完成的,那么该步骤可以省略。

5,双击执行。告别DOS命令行。

import java.awt.*;

importjava.awt.event.*;

class FrameDemo{

public static voidmain(String[] args) {

//1,通过frame创建窗体。

Frame f = newFrame("my frame");

//2,对窗体进行简单设置。

//2.1大小。

f.setSize(500,400);

//2.2设置窗体的屏幕上的位置。

f.setLocation(400,100);

//2.3给窗体设置指定的布局。窗体默认布局是边界布局。BorderLayout

f.setLayout(newFlowLayout());

//3,给窗体添加一个组件。

//创建一个按钮对象。

Button but = newButton("my button");

//4,将组件添加到窗体中。

f.add(but);

想要实现窗体的关闭效果。

窗体就作为一个事件源。

需要在窗体注册一个窗体监听器。监听可以操作窗体的动作。

//f.addWindowListener(newSubWin());

f.addWindowListener(newWindowAdapter(){

public voidwindowClosing(WindowEvent e){

System.exit(0);

}

});

but.addActionListener(newActionListener(){

public voidactionPerformed(ActionEvent e){

System.out.println("我是一个按钮");

System.exit(0);

}

});

//让窗体进行显示。

f.setVisible(true);

System.out.println("HelloWorld!");

}

}

监听接口中,只要方法没有超过3个,该接口就没有Adapter。

一般都有适配器。只有2,3个没有适配器的。后面会使用到。

class SubWinextends WindowAdapter

{

public voidwindowClosing(WindowEvent e){

System.out.println("我关..."+e);

System.exit(0);

}

public voidwindowOpened(WindowEvent e){

System.out.println("你开,你开,你开玩笑吧");

}

}

三:事件监听机制。

1,事件源。

就是被操作的组件。

2,事件。

会出现的一些事情。

每一个事件的发生,有可能对应的动作有很多。

将这些动作都与事件源相关联即可。

只要有一个动作发生,就引发了对应的事件。

可是多个动作直接和事件源关联,这样不利于应用和扩展。

所以,将这些动作进行了封装。封装成了一个对象。

让这个对象和事件源关联即可.

这个封装后的对象就形象的称为监听器.

监听器和事件源相关联,也就是将监听器注册到了事件源身上.

当有外部动作作用到该事件源上时,监听器会自动判断这个动作,如果该动作符合监听中的某一个动作,

那么这时,监听动作所对应的事件就发生了,该事情会被封装成一个事件对象,并传递给正在发生的这个动作.

该动作其实就是一个函数,这个函数就会执行.并对事件进行处理。

import java.awt.*;

importjava.awt.event.*;

class AwtDemo{

private Frame f;

private Buttonbut,okBut;

private TextFieldtf;

private Dialog d;

private Label lab;

AwtDemo(){

init();

}

public void init()

{

f = newFrame("my frame");

f.setSize(500,400);

f.setLocation(400,100);

f.setLayout(newFlowLayout());

but = newButton("my button");

tf = newTextField(10);

d = newDialog(f,"错误提示信息",true);

d.setSize(300,200);

d.setLocation(500,100);

d.setLayout(newFlowLayout());

okBut = newButton("确 定");

lab = new Label();

d.add(lab);

d.add(okBut);

f.add(but);

f.add(tf);

myEvent();

f.setVisible(true);

}

private voidmyEvent(){

but.addMouseListener(newMouseAdapter(){

public voidmouseClicked(MouseEvent e){

if(e.getClickCount()==2)

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

}

public voidmouseEntered(MouseEvent e){

System.out.println("鼠标进入");

}

});

okBut.addActionListener(newActionListener(){

public voidactionPerformed(ActionEvent e){

d.setVisible(false);

}

});

d.addWindowListener(newWindowAdapter(){

public voidwindowClosing(WindowEvent e){

d.setVisible(false);

}

});

//定义一个文本框,该框中只能输入数字,因为接收是qq号。

tf.addKeyListener(newKeyAdapter(){

public voidkeyPressed(KeyEvent e){

int code =e.getKeyCode();

if(!(code>=KeyEvent.VK_0&& code<=KeyEvent.VK_9)){

//System.out.println("只能是数字,别的不行");

lab.setText("只能是数字,"+e.getKeyChar()+"不行");

d.setVisible(true);

e.consume();//取消事件的默认处理效果。

}

}

});

but.addKeyListener(newKeyAdapter()

{

public voidkeyPressed(KeyEvent e){

//System.out.println(KeyEvent.getKeyText(e.getKeyCode())+":::"+e.getKeyCode());

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

System.out.println("ctrl+enter run");

}

});

f.addWindowListener(newWindowAdapter(){

public voidwindowClosing(WindowEvent e){

System.exit(0);

}

});

}

}

class MouseKeyDemo{

public static voidmain(String[] args) {

new AwtDemo();

}

}

编写记事本程序的代码如下:

import java.awt.*;

importjava.awt.event.*;

class MyMenu

{

private Frame f;

private MenuBarmb;

private MenuItemmi,mi2;

private Menufm,sub;

MyMenu()

{

init();

}

public void init()

{

f = newFrame("my menu demo");

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

mb = newMenuBar();

fm = newMenu("文件");

mi = newMenuItem("关闭");

fm.add(mi);

sub = newMenu("子菜单");

mi2 = newMenuItem("演示菜单");

sub.add(mi2);

fm.add(sub);

mb.add(fm);

f.setMenuBar(mb);

myEvent();

f.setVisible(true);

}

private voidmyEvent()

{

mi.addActionListener(newActionListener()

{

public voidactionPerformed(ActionEvent e)

{

System.exit(0);

}

});

f.addWindowListener(newWindowAdapter()

{

public voidwindowClosing(WindowEvent e)

{

System.exit(0);

}

});

}

}

class MenuDemo

{

public static voidmain(String[] args)

{

new MyMenu();

}

}

package mymenu;

import java.awt.*;

importjava.awt.event.*;

import java.io.*;

class MyNote

{

private Frame f;

private MenuBarbar;

private MenufileMenu;

private MenuItemopenItem,saveItem,closeItem;

private TextAreatextArea;

private FileDialogfileDia;

private File file;

MyNote()

{

init();

}

public void init()

{

f = newFrame("my note ");

f.setBounds(400,100,550,600);

bar = newMenuBar();

fileMenu = newMenu("文件");

openItem = newMenuItem("打开");

saveItem = newMenuItem("保存");

closeItem = newMenuItem("关闭");

fileMenu.add(openItem);

fileMenu.add(saveItem);

fileMenu.add(closeItem);

bar.add(fileMenu);

textArea = newTextArea();

f.setMenuBar(bar);

f.add(textArea);

fileDia = newFileDialog(f);

myEvent();

f.setVisible(true);

}

private voidmyEvent()

{

saveItem.addActionListener(newActionListener()

{

public voidactionPerformed(ActionEvent e)

{

//当要操作的文件已经存在,文件保存对话框是不需要弹出的。

//只要要操作的文件不存在是,才会弹出对话框,进行文件目录和文件名称的指定。

if(file==null)

{

fileDia.setTitle("我的保存文件");

fileDia.setMode(FileDialog.SAVE);

fileDia.setVisible(true);

String str_dir = fileDia.getDirectory();

String str_file = fileDia.getFile();

if(str_dir==null || str_file==null)

return ;

file = new File(str_dir,str_file);

}

String text =textArea.getText();

FileWriter fw =null;

try

{

fw = new FileWriter(file);

fw.write(text);

fw.flush();

fw.close();

}

catch (IOExceptionex)

{

ex.printStackTrace();

}

}

});

openItem.addActionListener(newActionListener()

{

public voidactionPerformed(ActionEvent e)

{

fileDia.setTitle("我的打开文件");

fileDia.setMode(FileDialog.LOAD);

fileDia.setVisible(true);

String str_dir =fileDia.getDirectory();

String str_file =fileDia.getFile();

//System.out.println(str_dir+"....."+str_file);

if(str_dir==null|| str_file==null)

return ;

textArea.setText("");

//为了获取数据。

file = newFile(str_dir,str_file);

//通过字符读取流进行数据的获取。

BufferedReaderbufr = null;

try

{

bufr = new BufferedReader(newFileReader(file));

String line = null;

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

{

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

}

bufr.close();

}

catch (IOExceptionex)

{

ex.printStackTrace();

}

}

});

closeItem.addActionListener(newActionListener(){

public voidactionPerformed(ActionEvent e){

System.exit(0);

}

});

f.addWindowListener(newWindowAdapter()

{

public voidwindowClosing(WindowEvent e)

{

System.exit(0);

}

});

}

}

class MenuTest

{

public static voidmain(String[] args)

{

new MyNote();

}

}

------- android培训java培训、期待与您交流! ----------  详细请查看:http://edu.csdn.net/heima/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: