王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

一、GUI概述1.1、GUI概述

Graphical User Interface(图形用户接口)

用图形的方式,来显示计算机操作的界面,

CLI:

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

如DOS,

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

1.2、Awt与Swing

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

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

1.3、结构

二、布局管理器2.1、布局

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

2.2、常见的布局管理

FlowLayout(流式布局管理器)

从左到右的顺序排列

Panel默认的布局管理器。

BorderLayout(边界布局管理器)

东、南、西、北、中

Fram默认的面布局管理器。

GridLayout(网格布局管理器)

规则的矩阵

CardLayout(卡片布局管理器)

选项卡

GridBagLayout(网络包布局管理器)

非规则的矩阵

2.3、

importjava.awt.*;publicclassAwtDemo1 {publicstaticvoidmain(String[] args) {//声明一个容器,Frame并设置标题Frame f=newFrame("Hello World");//设置窗体大小第一个参数是宽度,第二个参数是高度f.setSize(600, 300);//设置窗体运行的位置,屏幕中显示的位置f.setLocation(400, 200);//设置窗体布局方式,默认为BorderLayout,设置为流式布局f.setLayout(newFlowLayout());//声明新的控件Button btn=newButton("按键");//将控件添加到窗体f.add(btn);//设置窗体可见,默认是不可见的f.setVisible(true);

}

}

三、事件3.1、事件监听机制

事件源(组件)awt包或者swing中的那些图形界面组件

事件(Event):每一个事件源都自己的特有的对应的事件和共性事件

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

以上3种,都在java中已经定义好了,直接获取对象用使用就可以

事件处理(引发事件后处理方式):我们要做的事情是,就是产生的动作进行处理.

3.2、事件监听机制特点

3.3、 示例

WindowsListener 接口有7个方法要重写,一般为直接实现而使用其子类

WindwoAdapter()

接收窗口事件的抽象适配器类。此类中的方法为空。此类存在的目的是方便创建侦听器对象。

扩展此类可创建WindowEvent侦听器并为所需事件重写该方法。(如果要实现WindowListener接口,则必须定义该接口内的所有方法。此抽象类将所有方法都定义为 null,所以只需针对关心的事件定义方法。)

只需要继承 WindwoAdapter类,实现需要的方法即可

importjava.awt.*;importjava.awt.event.*;publicclassAwtDemo1 {publicstaticvoidmain(String[] args) {//声明一个容器,Frame并设置标题Frame f=newFrame("Hello World");//设置窗体大小第一个参数是宽度,第二个参数是高度f.setSize(600, 300);//设置窗体运行的位置,屏幕中显示的位置f.setLocation(400, 200);//设置窗体布局方式,默认为BorderLayout,设置为流式布局f.setLayout(newFlowLayout());//声明新的控件Button btn=newButton("按键");//添加监听事件//将控件添加到窗体f.add(btn);//设置窗体可见,默认是不可见的f.setVisible(true);//添加监听器f.addWindowListener(newMyWinEvent());

}

}classMyWinEventextendsWindowAdapter{

@OverridepublicvoidwindowActivated(WindowEvent e) {//激活窗口时调用super.windowActivated(e);

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

}

@OverridepublicvoidwindowClosed(WindowEvent e) {//当窗口已被关闭时调用super.windowClosed(e);

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

}

@OverridepublicvoidwindowClosing(WindowEvent e) {//窗口正处在关闭过程中时调用。super.windowClosing(e);

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

}

@OverridepublicvoidwindowDeactivated(WindowEvent e) {//停用窗口时调用。super.windowDeactivated(e);

System.out.println("我被停用了");

}

@OverridepublicvoidwindowDeiconified(WindowEvent e) {//取消图标化窗口时调用super.windowDeiconified(e);

System.out.println("取消图标化窗口");

}

@OverridepublicvoidwindowGainedFocus(WindowEvent e) {//该 Window 被设置为聚焦 Window 时调用,聚焦 Window 意味着该 Window 或其某个子组件将接收键盘事件。super.windowGainedFocus(e);

System.out.println("获取焦点");

}

@OverridepublicvoidwindowIconified(WindowEvent e) {//图标化窗口时调用。super.windowIconified(e);

System.out.println("图标化窗口时调用。");

}

@OverridepublicvoidwindowLostFocus(WindowEvent e) {//该 Window 不再为聚焦 Window 时调用,不再为聚焦 Window 意味着键盘事件不再传递到该 Window 或其任意子组件。super.windowLostFocus(e);

System.out.println("失去焦点");

}

@OverridepublicvoidwindowOpened(WindowEvent e) {//已打开窗口时调用。super.windowOpened(e);

System.out.println("窗口打开了");

}

@OverridepublicvoidwindowStateChanged(WindowEvent e) {//窗口状态改变时调用。super.windowStateChanged(e);

System.out.println("窗口状态改变时调用。");

}

}

四、Action事件4.1、Action事件

addActionListener(ActionListener l)

添加指定的动作侦听器,以接收发自此按钮的动作事件

只有一个方法

actionPerformed(ActionEvent e)

发生操作时调用。

用于接收操作事件的侦听器接口。对处理操作事件感兴趣的类可以实现此接口,而使用该类创建的对象可使用组件的addActionListener方法向该组件注册。在发生操作事件时,调用该对象的actionPerformed方法。

4.2、示例

importjava.awt.*;importjava.awt.event.*;classActionDemo

{publicstaticvoidmain(String[] args)

{newFrameDemo();

}

}classFrameDemo

{//定义成员变量frame和ButtonprivateFrame f;privateButton btn;publicFrameDemo()

{

init();

}publicvoidinit()

{//初始人成员变量,并设置初始值f=newFrame("我的Frame窗体");//设置窗口位置水平与垂直坐标,宽度与高度f.setBounds(200,200,600,500);//f.setLocation(400,100);//可见的f.setVisible(true);//设置布局f.setLayout(newFlowLayout());//给窗体添中监听器,事件myEvent();

btn=newButton("我的按键");//添控件添加到frame窗体上f.add(btn);//给按键添加Action事件btn.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e)

{

System.out.println("按键事件!,按键关闭窗口");

System.exit(0);

}

});

}//事件方法privatevoidmyEvent()

{//添加事件f.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvent e)

{

System.out.println("myFram 关闭");

System.exit(0);

}

});

}

}

五、鼠标、键盘与对话框事件5.1、示例

/*鼠标与键盘事件*/importjava.awt.*;importjava.awt.event.*;classAwtDemo3

{publicstaticvoidmain(String[] args)

{newmouseAndKey();

}

}classmouseAndKey

{privateFrame f;privateButton btn;privateTextField tf;publicmouseAndKey()

{

init();

}//初始化publicvoidinit()

{

f=newFrame("My Frame");

btn=newButton("My Button");//指定列数tf=newTextField(20);//设置位置,大小f.setBounds(200,300,500,400);

f.setLayout(newFlowLayout());//流式布局f.add(btn);

f.add(tf);

f.setVisible(true);//为窗口添加监听器myEvent();//添加鼠标监听器addMouseEvent();//添加键盘监听器addMykeyEvent();//添加文件框监听器addTfEvent();

}//窗口监听器privatevoidmyEvent()

{

f.addWindowListener(newWindowAdapter(){//添加关闭事件publicvoidwindowClosing(WindowEvent e)

{

System.out.println("窗口关闭了");

System.exit(0);

}

});

}//添加鼠标事件privatevoidaddMouseEvent()

{

btn.addActionListener(newActionListener(){//活动状态publicvoidactionPerformed(ActionEvent e){

System.out.println("窗口活动状态action");

}

});//添加鼠标监听器btn.addMouseListener(newMouseAdapter(){/*int clickedCount=1;

//单击事件

public void mouseClicked(MouseEvent e)

{

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

}

});*///双击事件intdoubleClickCount=1;publicvoidmouseClicked(MouseEvent e)

{if(e.getClickCount()==2)

{

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

}

}//鼠标进入事件intcount=1;publicvoidmouseEntered(MouseEvent e)

{

System.out.println("鼠标进入控件"+count++);

}

});

}privatevoidaddMykeyEvent()

{

btn.addKeyListener(newKeyAdapter(){//按下某个键时调用此方法publicvoidkeyPressed(KeyEvent e)

{//System.out.println("按下:"+KeyEvent.getKeyText(e.getKeyCode())+"...."+e.getKeyCode());/*//按下esc键关闭窗口

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

{

System.exit(0);

}*/if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)

{

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

}

}//释放某个键时调用此方法。publicvoidkeyReleased(KeyEvent e)

{//System.out.println("释放:"+KeyEvent.getKeyText(e.getKeyCode())+"...."+e.getKeyCode());}//键入某个键时调用此方法。publicvoidkeyTyped(KeyEvent e)

{//System.out.println("输入:"+KeyEvent.getKeyText(e.getKeyCode())+"...."+e.getKeyCode());}

});

}privatevoidaddTfEvent(){

tf.addKeyListener(newKeyAdapter(){publicvoidkeyPressed(KeyEvent e)

{intcode=e.getKeyCode();if(!(code>=KeyEvent.VK_0 && code<=KeyEvent.VK_9))

{

System.out.println(code+"....是不合法的!");//不能输入进去e.consume();

}

}

});

}

}

示例、

/*在文本框中输入目录,点击转到 按键,将该目录中的文件与文件

夹名称列在下面的文本区域中*/importjava.awt.*;importjava.awt.event.*;importjava.io.*;classTextFieldDemo

{publicstaticvoidmain(String[] args)

{newMyTextField();

}

}/*private Frame f; 窗体

private Button btn; 按键

private TextField tf; 文本框

private TextArea ta; 多行文本域

private Dialog d; 对话框

private Label lab; 文本信息

private Button okBtn; 确定按键*/classMyTextField

{privateFrame f;privateButton btn;privateTextField tf;privateTextArea ta;privateDialog d;privateLabel lab;privateButton okBtn;publicMyTextField()

{

init();

}//初始化publicvoidinit()

{

f=newFrame("根据目录列出所有文件");

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

f.setLayout(newFlowLayout());

tf=newTextField(30);

btn=newButton("转到");

ta=newTextArea(20,50);/*提示窗口*/d=newDialog(f,"提示信息",true);

lab=newLabel();

okBtn=newButton("确定");

d.add(lab);

d.add(okBtn);

d.setBounds(400,200,240,150);

d.setLayout(newFlowLayout());//将控件添回到窗体f.add(tf);

f.add(btn);

f.add(ta);//设置窗体可见f.setVisible(true);//添加事件myEvent();

}publicvoidmyEvent()

{//窗体监听器,f.addWindowListener(newWindowAdapter(){//关闭事件publicvoidwindowClosing(WindowEvent e)

{

System.exit(0);

}

});//文本框添加事件监听,输入完成,敲回车执行tf.addKeyListener(newKeyAdapter()

{publicvoidkeyPressed(KeyEvent e)

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

{//调用showDir();

}

}

}

);//按键添加监听器,事件输入完成,点按键执行btn.addActionListener(newActionListener()

{publicvoidactionPerformed(ActionEvent e)

{//调用显示showDir();

}

}

);//对话框事件//窗体监听器,d.addWindowListener(newWindowAdapter(){//关闭事件publicvoidwindowClosing(WindowEvent e)

{//将对话框隐藏d.setVisible(false);

}

});

okBtn.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e)

{//将对话框隐藏d.setVisible(false);

}

});

}privatevoidshowDir()

{//获取文本框输入的内容String dirPath=tf.getText();//封装为文件File dir=newFile(dirPath);//判断是不是目录同时是否存在if(dir.exists()&&dir.isDirectory())

{//存在并且目录存在就把多行文本框清空ta.setText("");//获取文件列表File [] files=dir.listFiles();for(File f:files)

{//向多行文本域中添加内容,增加ta.append(f.getName()+"\n");

}

}else{//对话框信息设置String info="您输入的路径:"+dirPath+" 是错误的请重新输入";

lab.setText(info);//对话框显示d.setVisible(true);

}//将文本框清空tf.setText("");

}

}

六、菜单6.1、示例

/*菜单:

private Frame f; 窗体

private MenuBar mb; 菜单栏

private Menu m; 菜单

private MenuItem closeItem; 菜单项关闭菜单*/importjava.awt.*;importjava.awt.event.*;classMenuDemo

{privateFrame f;privateMenuBar mb;privateMenu m;privateMenu subMenu;//子菜单的子菜单privateMenuItem closeItem;privateMenuItem subItem;//子菜单条目属于subMenu的子菜单项//构造方法publicMenuDemo()

{

init();

}/*初始化*/publicvoidinit()

{//窗体f=newFrame("有菜单的窗体");//设置位置,宽,高f.setBounds(300,100,500,400);//布局f.setLayout(newFlowLayout());//菜单栏mb=newMenuBar();//菜单m=newMenu("文件");

subMenu=newMenu("子菜单");//菜单项closeItem=newMenuItem("退出");

subItem=newMenuItem("子菜单条目");//菜单添加菜单项subMenu.add(subItem);

m.add(subMenu);

m.add(closeItem);//菜单栏添加菜单mb.add(m);//窗体设置菜单栏f.setMenuBar(mb);//设置显示f.setVisible(true);

myEvent();

}/*设置监听器*/privatevoidmyEvent()

{/*通过右上角X关闭窗体*/f.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvent e)

{

System.out.println("通过关闭X退出,这个窗体");

System.exit(0);

}

});/*通过菜单退出*/closeItem.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e)

{

System.out.println("通过菜单退出,这个窗体");

System.exit(0);

}

});

}/*测试*/publicstaticvoidmain(String[] args)

{newMenuDemo ();

}

}

七、打开与关闭文件

7.1、示例,添加打开菜单项

FileDialogpublicFileDialog(Frame parent,

String title,

int mode)

创建一个具有指定标题的文件对话框窗口,用于加载或保存文件。如果mode的值为LOAD,那么文件对话框将查找要读取的文件,所显示的文件是当前目录中的文件。如果mode的值为SAVE,则文件对话框将查找要写入文件的位置。

参数:parent- 对话框的所有者title- 对话框的标题mode- 对话框的模式,可以是FileDialog.LOAD或FileDialog.SAVE/*菜单:

private Frame f; 窗体

private MenuBar mb; 菜单栏

private Menu m; 菜单

private MenuItem openItem; 打开

private MenuItem closeItem; 关闭

private MenuItem saveItem;保存

//构造方法*/packagemymenu;importjava.awt.*;importjava.awt.event.*;importjava.io.*;publicclassMenuDemo1

{privateFrame f;privateMenuBar mb;privateMenu fileMenu;privateMenuItem openItem;privateMenuItem closeItem;privateMenuItem saveItem;privateFileDialog openDia;//打开对话框privateFileDialog saveDia;//保存对话框privateTextArea ta;privateFile file;//构造方法publicMenuDemo1()

{

init();

}/*初始化*/publicvoidinit()

{//窗体f=newFrame("有菜单的窗体");//设置位置,宽,高f.setBounds(300,100,650,500);//布局//f.setLayout(new FlowLayout());//菜单栏mb=newMenuBar();//菜单fileMenu=newMenu("文件");//菜单项openItem=newMenuItem("打开");

saveItem=newMenuItem("保存");

closeItem=newMenuItem("退出");//菜单添加菜单项fileMenu.add(openItem);//添加打开fileMenu.add(saveItem);//添加保存fileMenu.add(closeItem);//添加关闭//菜单栏添加菜单mb.add(fileMenu);//窗体设置菜单栏f.setMenuBar(mb);//打开,关闭对话框openDia=newFileDialog(f,"打开",FileDialog.LOAD);

saveDia=newFileDialog(f,"保存",FileDialog.SAVE);

ta=newTextArea();

f.add(ta);

myEvent();//设置显示f.setVisible(true);

}/*设置监听器*/privatevoidmyEvent()

{/*通过右上角X关闭窗体*/f.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvent e)

{

System.out.println("通过关闭X退出,这个窗体");

System.exit(0);

}

});/*打开*/openItem.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e)

{

openDia.setVisible(true);

show();

}

});/*保存*/saveItem.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e)

{

save();

}

});/*通过菜单退出*/closeItem.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e)

{

System.out.println("通过菜单退出,这个窗体");

System.exit(0);

}

});

}privatevoidshow()

{

String dirPath=openDia.getDirectory();//路径String fileName=openDia.getFile();//文件名//打印路径System.out.println(dirPath+"..."+fileName);if(dirPath==null|| fileName==null)return;

ta.setText("");

File file=newFile(dirPath,fileName);//封装为文件BufferedReader br=null;try{

br=newBufferedReader(newFileReader(file));

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

{

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

}

}catch(IOException e)

{

}finally{try{if(br!=null)

br.close();

}catch(IOException e)

{

}

}

}/*保存*/privatevoidsave()

{if(file==null){//设置saveDia.setVisible(true);

String dirPath=saveDia.getDirectory();//目录String fileName=saveDia.getFile();//文件//判断路径和文件名是否为空if(dirPath==null|| fileName==null)//啥也不做return;//如果文件为空就,newfile=newFile(dirPath,fileName);

}//不为空就直接保存BufferedWriter bw=null;try{

bw=newBufferedWriter(newFileWriter(file));//获取多行文本框内容String context=ta.getText();//System.out.println(context);bw.write(context);//bw.flush();}catch(IOException e)

{

}finally{try{if(bw!=null)

bw.close();

}catch(IOException ex)

{

}

}

}/*测试*/publicstaticvoidmain(String[] args)

{newMenuDemo1 ();

}

}/*FileDialog

public FileDialog(Frame parent,

String title,

int mode)创建一个具有指定标题的文件对话框窗口,用于加载或保存文件。

如果 mode 的值为 LOAD,那么文件对话框将查找要读取的文件,所显示的文件是当前目录中的文件。如果 mode 的值为 SAVE,则文件对话框将查找要写入文件的位置。

参数:

parent - 对话框的所有者

title - 对话框的标题

mode - 对话框的模式,可以是 FileDialog.LOAD 或 FileDialog.SAVE*/

八、打包8.1、将示中打所为jar文件

编译带包的源文件

javac -d F:\java\day22\menu MenuDemo1.java

打包

Manifest-Version: 1.0

Created-By: 1.8.0_60 (OracleCorporation)

Main-Class: mymenu.MenuDemo1

每个标签冒号后都跟上一个空格

最后一行需要有一个回车键,否则后面那一行是不能打包进去的

没有空格会报

java.io.IOException: invalid header field

at java.util.jar.Attributes.read(Attributes.java:393)

at java.util.jar.Manifest.read(Manifest.java:182)

at java.util.jar.Manifest.<init>(Manifest.java:52)

at sun.tools.jar.Main.run(Main.java:132)

at sun.tools.jar.Main.main(Main.java:1022)

1.txt

Main-Class: mymenu.MenuDemo1

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航