黑马程序员-GUI

来源:互联网 发布:魅蓝note6网络频段 编辑:程序博客网 时间:2024/05/17 22:24
java.Awt:  Abstract Window ToolKit  需要调用本地方法实现功能,属重量级控件。
java.Swing:在AWT的基础上,建立一套图形界面系统。其中提供了更多的的组件。增强了移植性。  

基础关系图:


Componet:组件。
container:容器。

Label:标签,封装文字
Checkbox:复选框。
TextArea:文本区域。
TextField:文本框。

window:窗口。
Panel:面板。窗体中使用多种布局时使用。
Frame:框架。
Dialog:对话框。 

布局:组件的排放方式。
new FlowLayout():流式布局,从左到右顺序排列。默认是居中。panel的默认布局。
new BorderLayout():边界布局,东南西北中。Frame的默认布局。没有指定方向会自动填充窗体。
new GridLayout():网格布局。规则的矩阵。(计算器)。
new CardLayout():卡片布局。选项卡 (右键属性)
new GridBagLayout():网格包布局。非规则矩阵。
 定义图形化界面的一般步骤:
1,创建窗体。
2,进行基本设置。大小,位置,布局。
3,定义组件。
4,将组件add到窗体中。
5,将窗体显示。setVisible(true)

事件监听机制:


特点:
1,事件源:就是awt包或者swing包中的那些图形界面的组件
2,事件:每一个事件源都有自己特有的对应事件和共性事件。
3,监听器:将可以触发某个事件的动作(不止一个)都已经封装到了监听器当中。
4,事件处理。

事件监听示例:
例如。窗口关闭。使用addWindowListener(WindowListener l),由于WidowListener接口中的抽象方法过多。所以定义了一个类WindowAdapter实现了WindowListener的抽象方法。但是方法为空。定义为抽象的,所以现在就可以使用WindowListener的子类当作参数传递了。想监听什么动作,就复写几个方法。

鼠标事件MouseEvent内部有获取鼠标键和次数的方法。

键盘事件对象KeyEvent内部有getKeyCode()和getKeyTest().内部大量常量值。可以用来做判断。
InputEvent 有判断组合键是否按下的方法。consume()取消默认的处理方式。屏蔽键。

import java.awt.*;import java.awt.event.*;class FrameDemo {public static void main(String[] args) {Frame f = new Frame("hehe");f.setBounds(350,150,600,400);f.setVisible(true);f.setLayout(new FlowLayout());TextField tf = new TextField(20);f.add(tf);Button b = new Button("按钮");f.add(b);tf.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e){if(!(e.getKeyCode()>=KeyEvent.VK_0&&e.getKeyCode()<=KeyEvent.VK_9))//屏蔽键e.consume();}});b.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e){if(e.getKeyCode()==KeyEvent.VK_ENTER)System.out.print("enter");}});b.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){System.out.println("anxia");}});f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});}}
new Dialog(依附的窗体,名字,是否锁定当前窗体)

Menu:菜单。
MenuBar:菜单栏。
MenuItem:条目。
菜单栏与窗体相关联用Frame f; f.setMenuBar(mb);
MenuBar只能添加Menu,  Menu可以添加Menu或者MenuItem,添加的Menu还可以有子菜单。

FileDialog:调用系统资源的文件打开和储存的对话框。
new FileDialog(f,name,FileDialog.LOAD)
new FileDialog(f,name,FileDialogSAVE)
此对象有获取选择文件名和路径的方法(包括存储时填写的文件名和当前路径)。分别为getFile()和getDirectory()。

import java.awt.*;import java.awt.event.*;import java.io.*;class MenuDemo {private Frame f ;private TextArea ta;private MenuBar mb;private Menu m1,m2;private MenuItem open,othersave,exit,save;private Dialog d ;private Button b;private Label lb;private File file;private FileDialog fdopen;private FileDialog fdsave;MenuDemo(){init();}public void init(){f=new Frame("记事本");ta=new TextArea(40,50);mb=new MenuBar();m1=new Menu("菜单");m2=new Menu("子条目");open=new MenuItem("打开");othersave=new MenuItem("另存为");save=new MenuItem("保存");exit=new MenuItem("退出");d =new Dialog(f,"错误",true);b=new Button ("确定");lb = new Label("文件类型错误,只支持.txt或者.java文件");fdsave= new FileDialog(f,"另存为",FileDialog.SAVE);fdopen= new FileDialog(f,"打开",FileDialog.LOAD);f.setMenuBar(mb);mb.add(m1);m1.add(m2);m2.add(open);m2.add(save);m2.add(othersave);m1.add(exit);d.setBounds(450,200,300,100);d.setLayout(new FlowLayout());d.add(lb);d.add(b);f.setBounds(350,100,500,400);f.add(ta);f.setVisible(true);myevent();}public void myevent(){f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});exit.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){System.exit(0);}});open.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){ta.setText("");fdopen.setVisible(true);String s = fdopen.getFile();String s1 =fdopen.getDirectory();if(s==null||s1==null)return;if(s.endsWith("java")||s.endsWith("txt")){file=new File(s1,s);BufferedReader br=null;try{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("读取失败");}}else{d.setVisible(true);}}});save.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){BufferedWriter wr=null;if(file==null){othersave();}else{try{wr=new BufferedWriter(new FileWriter(file));wr.write(ta.getText());wr.close();}catch (IOException ex){throw new RuntimeException("保存失败");}}}});othersave.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){othersave();}});b.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){d.setVisible(false);}});}public void othersave(){//建立文件名过滤器FileDialog fdsave=new FileDialog(f,"打开",FileDialog.LOAD);File file=null;FilenameFilter ff=new FilenameFilter(){public boolean accept(File dir,String name){return dir.getName().endsWith("java")||dir.getName().endsWith("txt");}};fdsave.setFilenameFilter(ff);fdsave.setVisible(true);String s2=fdsave.getFile();String s3 =fdsave.getDirectory();if(s2==null||s3==null)return;file =new File(s3,s2);try{if(!(file.exists()))file.createNewFile();}catch (IOException ex){throw new RuntimeException("建立失败");}BufferedWriter bw =null;try{bw=new BufferedWriter(new FileWriter(file));bw.write(ta.getText());bw.close();}catch (IOException ex1){throw new RuntimeException("保存失败");}}public static void main(String[] args) {new MenuDemo();}}




原创粉丝点击