黑马程序员——GUI

来源:互联网 发布:淘宝买二手苹果靠谱吗 编辑:程序博客网 时间:2024/05/16 00:29

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

 GUI
Graphical User Interface(图形用户接口)。
用图形的方式,来显示计算机操作的界面,这样更方便更直观。

 

Java为GUI提供的对象都存在java.Awt抽象窗口工具包,需要调用本地系统方法实现功能。属重量级控件 

javax.Swing基于Awt基础,建立一套图形界面系统,提供更多组件,完全由java实现。增强移植性,属轻量级控件

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

 

常见的布局管理器:
FlowLayout(流式布局管理器)
从左到右的顺序排列。
Panel默认的布局管理器。
BorderLayout(边界布局管理器)
东,南,西,北,中
Frame默认的布局管理器。
GridLayout(网格布局管理器)
规则的矩阵
CardLayout(卡片布局管理器)
选项卡
GridBagLayout(网格包布局管理器)
非规则的矩阵

 

简单的窗体创建过程:Frame  f = new Frame(“my window”);f.setLayout(new FlowLayout());    //设置布局管理器f.setBounds(400,400,600,500);//设置窗体大小及出现位置//f.setSize(600,500);    //设置窗体大小//f.setLocation(400,400);    //设置窗体出现在屏幕的位置f.setIconImage(Toolkit.getDefaultToolkit().createImage("qq.png"));Button button = new Button("我是一个按钮");f.add(button);//显示窗体f.setVisible(true);

 

 

事件监听机制

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

2,通过事件源对象的addXXXListener()方法将侦听器注册到该事件源上。
该方法中接收XXXListener的子类对象,或者XXXListener的子类XXXAdapter的子类对象。一般用匿名内部类来表示。
3,在覆盖方法的时候,方法的参数一般是XXXEvent类型的变量接收。

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

 

 

 窗口事件关闭窗体

//事件源是窗体,把监听器注册到事件源上//事件对象传递给监听器f.addWindowListener(new WindowAdapter() {        public void windowClosing(WindowEvent e) {System.out.println("关闭");         //退出虚拟机,关闭窗口        System.exit(0);    }});


 

按钮关闭窗体

but.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){System.exit(0);}});


 

 鼠标监听器(MouseListener)

 void mouseClicked(MouseEvent e)
          鼠标按键在组件上单击(按下并释放)时调用。
 void mouseEntered(MouseEvent e)
          鼠标进入到组件上时调用。
 void mouseExited(MouseEvent e)
          鼠标离开组件时调用。
 void mousePressed(MouseEvent e)
          鼠标按键在组件上按下时调用。
 void mouseReleased(MouseEvent e)
          鼠标按钮在组件上释放时调用。

 

but.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++);}});


 键盘按下事件

but.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e){//Control和Control按下if(e.isControlDown() && e.getKeyCode()==KeyEvent.VK_Control)System.out.println("ctrl+enter is run");//根据码获取所按的键//System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"..."+e.getKeyCode());}});


 

文本框键盘监听事件

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(KeyEvent.getKeyText(code)+"....非法");e.consume();//屏蔽}}});


//列出指定目录内容//错误提示对话框import java.awt.*;import java.awt.event.*;import java.io.*;class MyWindowDemo{private Frame f;private TextField tf;private Button but;private TextArea ta;private Dialog d;//对话框private Label lab;//文字private Button okBut;MyWindowDemo(){init();}public void init(){f = new Frame("my window");f.setBounds(400,200,600,400);f.setLayout(new FlowLayout());tf= new TextField(30);but = new Button("转到");ta = new TextArea(15,40);//不关闭无法操作其它d = new Dialog(f,"提示信息-self",true);d.setBounds(400,200,300,180);d.setLayout(new FlowLayout());lab = new Label();okBut = new Button("确定");d.add(lab);d.add(okBut);f.add(tf);f.add(but);f.add(ta);myEvent();f.setVisible(true);}public void myEvent(){d.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){d.setVisible(false);}});okBut.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){d.setVisible(false);}});tf.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e){//键盘事件-回车if (e.getKeyCode()==KeyEvent.VK_ENTER)showDir();}});but.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){showDir();}});f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});}private void showDir(){String dirPath = tf.getText();//指定的目录File dir = new File(dirPath);if(dir.exists() && dir.isDirectory()){//存在并且是目录ta.setText("");//清空String[] names = dir.list();for (String name : names ){ta.append(name+"\r\n");}}else{String info = "输入的信息:"+dirPath+"是错误的,请重新输入";lab.setText(info);d.setVisible(true);}}public static void main(String[] args) {new MyWindowDemo();}}


 

 

//菜单应用:打开保存退出import java.awt.*;import java.awt.event.*;import java.io.*;class  MyMenuText{private Frame f;private MenuBar bar;//菜单栏private Menu fileMenu ;//子菜单private MenuItem closeItem,openItem,saveItem;//条目private FileDialog openDia,saveDia;private TextArea ta;private File file;MyMenuText(){init();}public void init(){f = new Frame("my window");f.setBounds(400,200,600,400);bar = new MenuBar();ta= new TextArea();fileMenu = new Menu("文件");closeItem = new MenuItem("退出");saveItem = new MenuItem("保存");openItem = new MenuItem("打开");fileMenu.add(openItem);fileMenu.add(saveItem);fileMenu.add(closeItem);bar.add(fileMenu);f.setMenuBar(bar);openDia = new FileDialog(f,"我要打开",FileDialog.LOAD);//打开saveDia = new FileDialog(f,"我要保存",FileDialog.SAVE);//保存f.add(ta);myEvent();f.setVisible(true);}public 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 = new File(dirPath,fileName);try{BufferedReader bufr = new BufferedReader(new FileReader(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 MyMenuText();}}


 

 

0 0
原创粉丝点击