GUI(图形用户界面)
交互方式:图形化界面和dos命令行。
全称:Graphical User Interface(图形用户接口)----CLI:Commandline User Interface
重点是:如何实现的?Java为GUI提供的对象都在AWT和Swing包中。
|---java.Awt:Abstract Window ToolKit(抽象窗口工具包)需要调用本地系统方法实现功能,属于重量级控件.
|---java.Swing:在AWT的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全由java实 现。增强了移植性,属轻量级控件。[重量级是和系统的依赖比较强,但是轻量完全跨平台还有swt
component构建,组件 Button按钮Lable标签:封装文字使用,将文字作为组件对象。
Checkbox复选框TextComponen文本组件:文本框和文本域
Container容器可以添加组件Window窗口Panel面板Frame框架Dialog对话框FileDialog文对话框。
布局管理器:
容器中的组件的排放方式,就是布局。
常见的布局管理器:
FlowLayout(流式布局管理器):左->右;Panel默认的布局管理器
BorderLayout(边界布局):东西南北中;Frame默认的
GridLayout(网络布局):规则的矩形
CardLayout(卡片布局):选项卡
GridBagLayout(网格包布局):非规则的矩形【最痛苦就是做界面】
最牛的是:坐标式布局(画布;组件都在旁边;代码自动生成)
Component类:
publicabstract class Component extends Objectimplements ImageObserver,MenuContainer,Serializable
component是一个具有图形表示能力的对象,可在屏幕上显示,并可与用户进行交互。典型图形用户 界面中的按钮、复选框和滚动条都是组件示例。方法特多
图形化界面是由其他线程处理。
Frame f=newFrame(“my awt”);//创建窗体
f.setSize(500,400);f.setLocation(300,200);//设置窗体大小,位置
f.setLayout(new FlowLayout());//设置布局
Button b=new Button(“我是按钮”);//定义组件
f.add(b);//在frame中添加按钮控件
//窗口的关闭处理,关于窗口找Window,监听器都是以Listener结尾。
//WindowAdapter接收窗口事件的抽象适配器,此类中的方法为空,此类存在的目的是方便创建侦听器对象。抽象类。
//①使用实现WindowListener的方法来做,需要覆盖7中方法,可以我只是用关闭的动作。费劲的方法。
//②因为WindowListener的子类WindowAdapter已经实现了WindowListener接口,并且复写了所有方法。所以我只要继承WindowAdapter覆盖我需要的方法即可。
添加:class ** extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.out.println(“window closing””e.toString()”);System.exit();}}//导包//事件信息//
f.addWindowListener(new **());//需要接受一个接口的子类对象
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){System.out.println(“关闭”);System.exit();}
public void windowActivated(WindowEvent e){sop(“wohuole”);}//前置//像异常
public void windowOpened(WindowEvent e){sop(“wobeidakaile”);}});//匿名内部类
f.setVisible(true);//让窗体显示
事件监听机制:
事件源:就是awt包或者swing包中的那些图形界面组件。
事件:每一个事件源都有自己特有的对应事件和共性事件。
监听器:将可以触发某一个事件的动作(不只一个动作)都已经封装到了监听器中。
以上三者,在java中都已经定义好了,直接获取其对象来用就可以了,我们的任务:对产生的动作进行处理。
写awt程序:将图形化界面和主函数分离出来。[开发常用]
import java.awt*;import java.awt.event.*;
class FrameDemo{
private Frame f;private Button but;//定义该图形中所需的组件的引用。
FrameDemo(){ init()}初始化就有图形
public void init(){
f=new Frame(“my frame”);//创建窗口
//对frame进行基本设置
f.setBounds(300,100,400,500);设置坐标长宽
f.setLayout(new FlowLayout());设置布局方式,避免按钮很大。
but=new Button(“my button”);//初始化
f.add(but);将组建添加到frame中
myEvent();加载一下窗体上的事件
f.setVisible(true);显示}
private void myEvent(){//事件
f.addWindowListener(new WindowAdapter(){
public void windowClosing(windowEvent e){System.exit(0);}
});//用匿名类完成
}//添加按钮,具备退出程序的功能。事件源。监听器选择:通过关闭窗体事件了解到想要知道哪个组件具备什么样的特有监听器。
需要查看该组件对象的功能。button有方法:addActionListener();
but.addActionListener(new ActionListener(){
Public void actionPerformed(ActionEvent e){sop(“tuichu”);System.exit();}
})
}main(){}
鼠标事件:
but.addMouseListener(newMouseAdapter(){
private intcount=1;
public voidmouseEntered(MouseEvent e){//复写方法,里面接受的是MouseEvent e
if(e.getClickCount()==2, 3) //双击动作,或三连击动作
sop(“鼠标就来了”+count++);}})
很好玩,处理的改变而改变。按钮有活动,但是鼠标的click会比按钮先执行,更具体。
键盘事件:
but.addKeyListener(newKeyAdapter(){//注册了一个键盘监听
public void keyPress(KeyEvent e){//KeyEvent封装了键盘。
if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER){sop(“ele”);}//组合键
System.out.println(KeyEvent.getKeyText(e.getKeyCode())+”..”+e.getKeyCode());
System.out.println(e.getKeyChar()+”..”+e.getKeyCode());//键值和对应码}})
TextField tf=new TextField(20);创建组件文本框,设置长度为20个字。然后使用f.add(tf);添加到Frame 中。添加事件:tf.addKeyListener(newKeyAdapter(){
Publicvoid keyPressed(KeyEvent e){
intcode=e.getKeyCode();
if(!(code>=KeyEvent.VK_0&&code<=KeyEvent.VK_9)){
sop(code+”...是非法的”);
e.consume();//非法时,数值不会进入文本框。}}});
get.KeyChar()和get.KeyCode(),分别是获取字符,获取对应码。
/*模拟地址指向文件==列出指定目录的内容*/
class MyWindowDemo{
Private Framef;private TextField tf;privateButton but;private TextArea ta;
private Dialogd;privat Label lab;private Button okBut;
MyWindowDemo(){ init(); }
public void init(){
f=new Frame(“my window”);f.setBounds(300,100,600,500);f.setLayout(newFlowLayout());
tf=new TextField(60);but=new Button(“转到”);ta=new TextArea(25,70);
f.add(tf);f.add(but);f.add(ta);myEvent();f.setVisible(true);
d=new Dialog(f,”提示信息-self”,true);d.setBounds(400,200,200,150);
d.setLayout(new FlowLayout());lab=new Label();okBut=new Button(“确定”);
d.add(lab);d.add(okBut);
}public void myEvent(){
tf.addKeyListener(new KeyAdapter(){
Public void keyPressed(KeyWvent e){
If(e.getKeyCOde()==KeyEvent.VK_ENTER) showDir();
}})
okBut.addActionListener(new ActionListener(){
Public void actionPerformed(ActionEvent e){
d.setVisible(false);
}});
d.addWindowListener(new WindowAdapter(){
Public void windowClosing(WindowEvent e){
d.setVisible(false);
}});
but.addActionListener(new ActionListened(){
Public void actionPerformed(ActionEvent e){showDir();
}});
f.addWindowListener(new WindowAdapter(){
Public void windowClosing(WindowEvent e){
System.exit(0);
}});
}private voidshowDir(){
StringtextdirPath=tf.getText();//ta.setText(text);//System.out.pritln(text);
File dir=new File(dirPath);
If(dir.exists()&&dir.isDiretory()){
ta.setText(“”);
String[]names=dir.list();
For(Stringname:names){
ta.setTextappend(name+”\r\n”);
}}//tf.setText(“”);
else{
String info=”你输入的信息:”+dirPath+”是错误的。请确定!”;
lab.setText(info);
d.setVisible(true);}
}public staticvoid main(String[] args){
new MyWindowDemo();}}
/*制作菜单*/
class MyMenuDemo{
private Framef;private MenuBar mb;private Menu m,subMenu;
private MenuItemcloseItem,subItm;//声明引用
myMenuDemo(){init();}//构造
public voidinit(){
f=new Frame(“my window”);//地方
f.setBound(300,100,500,600);
f.setLayout(new FlowLayout());
mb=new MenuBar();m=new Menu(“文件”);
subMenu=new Menu(“子菜单”);//菜单有箭头,条目没有
subItem=new MenuItem(“子条目”);closeItem=new MenuItem(“退出”);//条目
subMenu.add(subItem);//子菜单生一个子条目
m.add(subMenu|subItem);m.add(closeItem);mb.add(m);
f.setMenuBar(mb);myEvent();f.setVisible(true);
}private voidmyEvent(){
closeItem.addActionListener(new ActionListener(){
Public void actionPerformed(ActionEvent e){System.exit(0);}
});
f.addWindowListener(new WindowAdapter(){
Public void windowClosing(WindowEvent e){
System.exit(0);//退出
}});}
public static voidmain(String[] args){
new MyMenuDemo();}}
/*文件的打开与保存*/
Class MyMenuTest{
private Framef;private MenuBar bar;private TextArea ta;private Menu fileMenu;
private MenuItemopenItem,saveItem,closeItem;private FileDialog openDia,saveDia;private File file;
MyMenuTest(){init();}
public voidinit(){
f=new Frame(“my window”);f.setBounds(300,100,650,600);
bar=new MenuBar();ta=new TextArea();
fileMenu=new Menu(“文件”);
openItem=new MenuItem(“打开”);saveItem=new MenuItem(“保存”);
closeItem=new MenuItem(“退出”);
fileMenu.add(openItem);fileMenu.add(saveItem);fileMenu.add(closeItem);
bar.add(fileMenu);f.setMenuBar(bar);
openDia=new FileDialog(f,”我要打开”,FileDialog.LOAD);
sasveDia=new FileDialog(f,”我要保存”,FileDialog.SAVE);
f.add(ta);myEvent();f.setVisible(true);
}private void myEvent(){
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;
File=new File(dirPath,fileName);
}try{BufferedWriter bufw=new BufferedWriter(new FileWriter(file));
String text=ta.getText();bufw.write(text);bufw.close();
}catch(IOException ex){throw new RuntimeException();}
}});
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(“”);
File file=new File(dirPath,fileName);
try{BufferedReader bufr=new BufferedReader(newFileReader(file));String line=null;
while((line=bufr.readLine())!=null){ta.append(line+”\r\n”);}bufr.close();}
catch(IOException ex){throw new RuntimeException(“读取失败”);}
}
});
closeItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){System.exit(0);}
});
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);//退出
}});
}public static void main(String[] args){
new MyMenuTest();}}
双击文件的制作:
1,打包。编译时:javac-d c:\myclassMyMenuTest.java 目录下会出现一个文件夹mymenu
2,使用jar:jar -cvf my.jarmymenu 错误是:加载清单属性失败。找不到主函数的类...
2,创建文本: 一,Main-Class:空格 mymenu.MyMenuTest回车固定格式
二,jar -cvfm my.jar 1.txt mymenu
3,jar文件注册:工具->文件夹选项->文件类型->高级->jar可设置。关联。