毕向东之图形化界面GUI

/*GUI(图形用户接口):用图形的方式来显示计算机操作的界面
 java为GUI提供的对象都存在java.Awt和java.Swing两个包中
 Awt(Abstract Window ToolKit):抽象窗口工具包,不能跨平台,对系统依赖性强
 Swing:完全由java实现,增强了移植性能跨平台
 组件包括:Button(按钮),Label(标签,放置文本的组件),Checkbooks(复选框),TextComponent(文本组件):(TextArea(文本区域),TextField(文本框))
 部分体系:
 Component(组件)
     |--Container:容器,一个特殊的组件,可以用add方法添加其他组件进来
         |--Panel(面板)
         |--Window(窗口)
             |--Frame(框架)
             |--Dialog(对话框)
                 |--FileDialog
容器中的组件的排序方式就是布局,常见的布局方式有:
FlowLayout(流式布局管理器):从左至右的顺序排列,Panel默认的布局方式
BorderLayout(边界布局管理器):东南西北中,Frame默认的布局方式
GridLayout(网格布局管理器):规则的矩阵
CardLayout(卡片布局管理器):选项卡
GridBagLayout(网络包布局管理器):非规则的矩阵
==========================================
创建图形化界面的步骤:
1.创建Frame窗体
2.对窗体进行基本的设置:如大小,位置,布局
3.定义组件
4.将组件通过窗体的add方法添加到窗体中
5.让窗体显示,通过setVisible(true);
 */
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Test1 {
    public static void main(String[] args) {
        Frame f=new Frame("我的框架");//框架名
        f.setSize(400, 300);//设置框架的长宽
        f.setLocation(300,200);//位置,改变起点的坐标,默认左上角
        f.setLayout(new FlowLayout());//设置组件的布局方式
        Button b=new Button("按钮上的字");
        f.add(b);//将按钮添加到框架中
        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.out.println("关闭");
                System.exit(0);
            }//添加一个监听器,使窗口能关闭
        });//使用了匿名内部类
        f.setVisible(true);//设置看得见的,让框架显示
    } 
}

 

 

/*因为WindowListener为接口,实例化需要复写它的很多方法,
 所以在关闭窗口new的对象为它的子类,该类内部已经复写了这个接口的方法,
 当需要关闭操作时只需要复写关闭方法,一般有三个方法 以上的都new其子类适配器
 */
import java.awt.*;
import java.awt.event.*;
public class Test2 {
    public static void main(String[] args) {
        Frame f=new Frame("java");
        f.setSize(500, 400);
        f.setLocation(300,300);
        Button b=new Button("按钮");
        TextField tf=new TextField(20);//文本框
        f.setLayout(new FlowLayout());
        f.add(tf);
        f.add(b);
        f.addWindowListener(new WindowAdapter(){//窗口监听器
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });//WindowAdapter为WindowListener的子类
        b.addActionListener(new ActionListener(){//活动监听器
            public void actionPerformed(ActionEvent e){
                System.out.println("按钮活动");
            }
        });
        b.addMouseListener(new MouseAdapter(){//鼠标监听器
            int count=1;
            public void mouseClicked(MouseEvent e){
                if(e.getClickCount()==2)
                    System.out.println("双击");
                else
                    System.out.println("单击"+count);
            }
            public void mouseEntered(MouseEvent e){
                System.out.println("鼠标进入到按钮上"+count++);
            }
        });
        b.addKeyListener(new KeyAdapter(){//键盘监听器
            public void keyPressed(KeyEvent e){
                if(e.isShiftDown() && KeyEvent.VK_ENTER==e.getKeyCode())
                    System.out.println("按下了Shift+回车键");//组合键的使用
                else{
                    System.out.println(KeyEvent.getKeyText(e.getKeyCode()));
                }//将按下的键以字符串的形式输出
            }
        });
        tf.addKeyListener(new KeyAdapter(){//文本框添加键盘监听器
            public void keyPressed(KeyEvent e){
                int code=(int)e.getKeyChar();
                if(!(code<=KeyEvent.VK_9 && code>=KeyEvent.VK_0)){
                    System.out.println(e.getKeyChar()+"是非法的");
                    e.consume();//让非法的不写到文本框里去,取消默认操作
                }
            }
        });
        f.setVisible(true);
    }
}

 

 

/*需要:在文本框里写入目录,点击转到按钮,将该目录中的文件与文件夹名称列在下面的文本区域中(搜索功能)

 */
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class Test3 {
    public static void main(String[] args) {
        Frame f=new Frame("名称");
        f.setSize(400, 300);
        f.setLocation(200, 100);
        f.setLayout(new FlowLayout());
        TextField tf=new TextField(20);//文本区域
        TextArea ta=new TextArea(20, 40);
        Button but=new Button("转到");
        f.add(tf);
        f.add(but);
        f.add(ta);
        //错误提示对话框
        Dialog d=new Dialog(f,"错误提示",true);//true代表弹出对话框时不能点击其他窗口
        d.setBounds(300, 150, 250, 150);//位置和大小的设置
        d.setLayout(new FlowLayout());
        Button diaBut=new Button("确定");
        Label lab=new Label();//放置文本的组件,一个标签只显示一行只读文本
        d.add(lab);
        d.add(diaBut);
        //设置关闭
        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
        //按钮的处理动作:把要找到文件打印到文本区域中
        but.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent e){//单击
                String str=tf.getText();//获取文本框里的内容
                tf.setText("");//清空文本框
                ta.setText("");//这样可以清空文本区之前显示的
                File f=new File(str);
                if(f.exists()&&f.isDirectory()){//存在且是目录
                    for(File file:f.listFiles()){
                        ta.append(file.toString()+"\n");//将内容追加到文本区
                    }
                }else{//不存在则给出错误提示
                    lab.setText("输入的路径:"+str+"错误");
                    d.setVisible(true);
                }
            }
        });
        //点击确定后退出对话框
        diaBut.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent e){
                d.setVisible(false);//隐藏对话框
            }
        });
        f.setVisible(true);
    }
}

 

 

/*菜单条由菜单组成,菜单里面有菜单项(MenuItem)
 MenuBar->Menu->MenuItem
 */
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Test4 {
    public static File file=null;
    public static void main(String[] args) {
        Frame f=new Frame("框架名");
        f.setBounds(200, 100, 500, 600);
        f.setLayout(new BorderLayout());//边界布局方式
        TextArea ta=new TextArea();//不定大小,因为是边界布局所以全屏
        f.add(ta);
        MenuBar mb=new MenuBar();//菜单条
        f.setMenuBar(mb);
        Menu m=new Menu("文件");//菜单
        Menu subMenu=new Menu("新建");//菜单
        MenuItem openMitem=new MenuItem("打开");
        MenuItem saveMitem=new MenuItem("保存");
        MenuItem subMenu_sub=new MenuItem("java工程");//菜单项
        MenuItem closeItem=new MenuItem("退出");//菜单项 
        mb.add(m);
        m.add(subMenu);//菜单里面添加菜单
        subMenu.add(subMenu_sub);
        m.add(openMitem);
        m.add(saveMitem);
        m.add(closeItem);//菜单里面添加菜单项
        FileDialog fd=new FileDialog(f,"打开",FileDialog.LOAD );
        FileDialog fdsave=new FileDialog(f,"保存",FileDialog.SAVE );
        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);//活动监听,f发生操作时就执行
            }
        });
        closeItem.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                System.exit(0);
            }
        });
        openMitem.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                fd.setVisible(true);//让加载对话框显示
                BufferedReader bufr=null;
                try{
                    if(fd.getDirectory()==null || fd.getFile()==null)
                        return;//当为空时,取消时不执行任何操作,避免异常的发生
                    file=new File(fd.getDirectory()+fd.getFile());
                    bufr=new BufferedReader(new FileReader(file));
                    String line;
                    ta.setText(null);//清空文本区域
                    while((line=bufr.readLine())!=null){
                        ta.append(line+"\n");
                    }
                }catch(IOException ioe){
                    System.exit(0);
                }finally{
                    if(bufr!=null){
                        try{
                            bufr.close();
                        }catch(IOException ioe){
                            System.exit(0);
                        }
                    }
                }
            }
        });
        saveMitem.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                if(file==null){//空就显示出来,可以选择性的保存到指定位置
                    fdsave.setVisible(true);
                }
                file=new File(fdsave.getDirectory()+fdsave.getFile());
                BufferedWriter bufw;
                try{
                    bufw=new BufferedWriter(new FileWriter(file));
                    String s=ta.getText();
                    bufw.write(s);
                    bufw.close();
                }catch(IOException ioe){
                    throw new RuntimeException();
                }
            }
        });
        f.setVisible(true);
    }
}

 

posted @ 2016-05-31 12:54  ts-android  阅读(246)  评论(0编辑  收藏  举报