JAVA图形化编程

---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------GUI编程

 

GUI:Graphical User Interface (图形用户接口)

 

Java为GUI提供的对象都封装在java.Awt和java.Swing两个包中。

 

Awt:抽象窗口工具包,需要调用本地系统方法实现功能,属于重量级控件。

 

Swing:在AWT的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全由Java实现,增强了移植性,属于轻量级控件。

 

布局管理器:

容器中组件的排放方式,就是布局。

 

常见的布局管理器有:

FlowLayout(流式布局管理器)

 

从左到右的顺序排列

Panel默认的布局管理器

 

BorderLayout(边界布局管理器)

 

GridLayout(网格布局管理器)

 

CardLayout(卡片布局管理器)

 

GridBagLayout(网格布局管理器)

 

AWT工具包中类的继承关系:

 

图形化编程步骤:

1、 创建Frame窗体

2、 对窗体进行基本设置,包括大小,位置,布局等。

3、 定义组件

4、 将组件通过窗体的add方法添加到窗体中

5、为窗体中个组件添加事件,监听

6、 通过setVisible()方法显示窗体

 

 

以下是两个简单的图形化编程程序:

1、文件打开,保存程序:

import java.awt.*;

importjava.awt.event.*;

importjava.io.*;

 

public class MyMenuDemo {

 

    private Framef;

    private TextAreata;

    private MenuBarmb;

    private Menum, subMenu;

    private MenuItemopenItem,saveItem,closeItem,subItem;

    private FileDialogopenDia, saveDia;

    private Filefile;

    MyMenuDemo() {

        init();

    }

 

    void MyEvent() {

 

    }

 

    public void init() {

        f = new Frame("my window");

        f.setBounds(300, 100, 500, 600);

        f.setLayout(new FlowLayout());

        mb = new MenuBar();

        m = new Menu("文件");

        ta = new TextArea();

        subMenu = new Menu("子菜单");

        closeItem = new MenuItem("退出");

        openItem = new MenuItem("打开");

        openItem.addActionListener(new ActionListener(){

 

            public void actionPerformed(ActionEvent e) {

                // TODO Auto-generated method stub

                openDia.setVisible(true);

                String filename =openDia.getFile();

                String path = openDia.getDirectory();

                if(path==null||filename==null)

                    return;

                ta.setText("");

                file = new File(path,filename);

                try {

                    BufferedReader bufr =new BufferedReader(new FileReader(file));

                    String line =null;

                    try {

                        while((line=bufr.readLine())!=null){

                            ta.append(line+"\r\n");

                        }

                    } catch (IOExceptione1) {

                        // TODO Auto-generated catch block

                        e1.printStackTrace();

                    }

                } catch (FileNotFoundExceptione1) {

                    // TODO Auto-generated catch block

                    e1.printStackTrace();

                }

                ta.append(path);

                ta.append(filename);

                ta.append("\r\n");

            }

           

        });

        saveItem = new MenuItem("保存");

        saveItem.addActionListener(new ActionListener(){

 

            public void actionPerformed(ActionEvent arg0) {

                // TODO Auto-generated method stub

           

                if(file==null){

                    saveDia.setVisible(true);

                    String path =saveDia.getDirectory();

                    String filename =saveDia.getFile();

                    if(path ==null||filename==null)

                        return;

                    file = new File(path,filename);

                    }

                try {

                        BufferedWriter bufw = new BufferedWriter(new FileWriter(file));

                        String text =ta.getText();

                        bufw.write(text);

                        bufw.flush();

                        bufw.close();

                    }

                   

                    catch (IOExceptione) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

                }

               

                }

            }

            );

        closeItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                System.exit(0);

            }

        });

        f.addWindowListener(new WindowAdapter(){

            public void windowClosing(WindowEvent e){

                System.exit(0);

            }

        });

        subItem = new MenuItem("子条目");

        m.add(openItem);

        m.add(saveItem);

        m.add(closeItem);

        // m.add(subItem);

        m.add(subMenu);

        subMenu.add(subItem);

        mb.add(m);

        f.add(ta);

        f.setMenuBar(mb);

        openDia = new FileDialog(f,"打开",FileDialog.LOAD);

        saveDia = new FileDialog(f,"保存",FileDialog.SAVE);

        f.setVisible(true);

    }

 

    public static void main(String[] args) {

 

        new MyMenuDemo();

    }

 

}

 

2、文件浏览器

importjava.awt.*;

importjava.awt.event.*;

importjava.io.*;

 

public class DocExplore {

 

    private Framef;

    private TextFieldtf;

    private TextAreata;

    private Buttonbt;

    DocExplore() {

        init();

    }

 

    void MyEvent() {

 

    }

 

    public void init() {

        f = new Frame("my window");

        ta = newTextArea(30,60);

        f.setBounds(300, 100, 500, 600);

        f.setLayout(new FlowLayout());

        //定义监听器,实现对话框关闭功能

        f.addWindowListener(new WindowAdapter(){

            public void windowClosing(WindowEvent e){

                System.exit(0);

            }

        });

        tf =new TextField(30);

        f.add(tf);

        bt =new Button("转到");

        //定义监听器,实现文件浏览功能

        bt.addActionListener(new ActionListener(){

 

            public void actionPerformed(ActionEvent arg0) {

                // TODO Auto-generated method stub

                String path = tf.getText();

                File dir = new File(path);

                if(dir.exists()&&dir.isDirectory()){

                    ta.setText("");

                    String[] names =dir.list();

                    for(Stringname:names){

                        ta.append(name+"\r\n");

                    }

                }

            }

           

        });

        f.add(bt);

        f.add(ta);

        f.setVisible(true);

    }

 

    public static void main(String[] args) {

 

        new DocExplore();

    }

 

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值