黑马程序员_GUI

来源:互联网 发布:mac os x英文操作界面 编辑:程序博客网 时间:2024/05/29 18:49

----------- android培训、java培训、java学习型技术博客、期待与您交流! ------------

1、GUI:Graphical User Interface,即图形用户界面,用于计算机与用户交互的一种方式
2、计算机与用户交互的形式:GUI和CLI
      GUI: Graphical User Interface,图形用户接口,用图形方式,来显示计算机操作界面,方便直观。
      CLI: Command Line User Interface,命令行用户接口,即常见的Dos命令行操作,须记住一些命令,不直观。
3、java也将这种界面封装为对象,其中的对象都放在了两个包中:java.Awt包和javax.Swing包。
java.Awt包:Abstract Window Toolkit,即抽象窗口工具箱。要调用本地系统方法实现功能,属重量级控件。
javax.Swing包:在AWT的基础上建立的一套图形界面系统,其中提供了更多的组件,且完全有java实现,增强了移植性,属轻量级控件。
GUI编程的一般过程。
1、创建Frame容器:Frame f = new Frame("my Frame");可设置标题
2、对窗体进行设置:如大小,位置,布局等。
      f.setSize(int wight,int hight);
      f.setLocation(int x,int y),设置横纵坐标;
      f.setBounds(int wight,int hight,int x,int y),者直接用这个对大小和位置设置
      f.setLayout(Layout layout),参数为指定的布局管理器,如FlowLayout
3、定义组件:如Button b = new Button(“Button”),可设置组件的名称
4、将组件通过窗体的add方法添加到窗体中,f.add(b);
5、让窗体显示,通过setVisible(boolean b),通过设置参数是true还是false是否显示窗体
案例1:打开指定的目录下的目录条目:
import java.awt.*;import java.awt.event.*;import java.io.*;class  MyWindowDemo{//创建全局变量private Frame f;private Button but;private TextField tf;private TextArea ta;private Dialog d;private Label lab;private Button okBut;//构造函数,初始化窗体MyWindowDemo(){init();}//创建窗体和组件,并将事件添加进来public void init(){//设置窗体f = new Frame("my window");f.setBounds(300,200,600,500);f.setLayout(new FlowLayout());//创建组件but = new Button("转到");tf = new TextField(60);ta = new TextArea(25,75);d = new Dialog(f,"提示信息-self",true);d.setBounds(300,100,300,150);d.setLayout(new FlowLayout());lab = new Label();okBut = new Button("确定");//将组件添加到窗体f.add(tf);f.add(but);f.add(ta);d.add(lab);d.add(okBut);//添加事件myEvent();//设置窗体可见f.setVisible(true);}//常见引发的时间private void myEvent(){//给but添加一个活动监听器but.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){showInfo();}});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)showInfo();}});//关闭窗体事件f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});//给对话框添加一个监听器,关闭对话框事件d.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){d.setVisible(false);}});}//操作文件,对文件夹的内容读取private void showInfo(){String dirPath = tf.getText();File file = new File(dirPath);if(file.exists()&&file.isDirectory()){ta.setText("");String[] names = file.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();}}


----------- android培训、java培训、java学习型技术博客、期待与您交流! ------------

原创粉丝点击