zoukankan      html  css  js  c++  java
  • 【转】java图形界面设计(AWT)

    【转自】http://blog.sina.com.cn/s/blog_616e189f0100ne1t.html

    1.       基本的java Frame操作。

    Java的图形界面的类主要包括AWT和Swing。在AWT中图形元素的父类为Component。

    继承关系如下:Component->Container->Window->Frame->JFrame。(注意:Swing对AWT进行了扩展)。

    下面给出一个简单的java图形程序:

    package com.guan.visualTest.frameTest;

     

    import java.awt.Button;

    import java.awt.event.WindowAdapter;

    import java.awt.event.WindowEvent;

     

    import javax.swing.JFrame;

     

    public class MainFrame {

     

        public static void main(String[] args)

        {

           //创建frame

           JFrame frame = new JFrame("welcome!!");

           //调整frame的大小和初始位置

           frame.setSize(400, 400);

           frame.setLocation(100, 100);

           //新建5个Button

           Button button1 = new Button("hello1");

           Button button2 = new Button("hello2");

           Button button3 = new Button("hello3");

           Button button4 = new Button("hello4");

           Button button5 = new Button("hello5");

           //将5个Button添加到frame中

           frame.add(button1,"East");

           frame.add(button2,"West");

           frame.add(button3,"South");

           frame.add(button4,"Center");

           frame.add(button5,"North");

           //增加窗口监听事件,使用内部类方法,并用监听器的默认适配器

           frame.addWindowListener(new WindowAdapter(){

     

               //重写窗口关闭事件

               @Override

               public void windowClosing(WindowEvent arg0) {

                  System.exit(0);

               }

              

              

           });

       

           //显示窗体

           frame.setVisible(true);

        }  

    }

    执行效果:

    java图形界面设计(AWT)

    2.       AWT的布局管理器

    AWT中主要有四种布局管理器:FlowLayout、GridLayout、BorderLayout和CardLayout。

    下面给出这四种布局管理器的源码:

    package com.guan.visualTest.frameTest;

     

    import java.awt.BorderLayout;

    import java.awt.Button;

    import java.awt.CardLayout;

    import java.awt.FlowLayout;

    import java.awt.Frame;

    import java.awt.GridLayout;

    import java.awt.Panel;

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import java.awt.event.WindowAdapter;

    import java.awt.event.WindowEvent;

     

     

    public class YourFrame extends Frame{

       

        private static final long serialVersionUID = 1L;

        Panel borderLayoutPanel;

        Panel cardLayoutPanel;

        Panel flowLayoutPanel;

        Panel gridLayoutPanel;

       

        private void generateGridLayoutPanel() {

           gridLayoutPanel new Panel();

           gridLayoutPanel.setLayout(new GridLayout(2,2));

          

           Button button1 = new Button("button1");

           Button button2 = new Button("button2");

           Button button3 = new Button("button3");

           Button button4 = new Button("button4");

          

           gridLayoutPanel.add(button1);

           gridLayoutPanel.add(button2);

           gridLayoutPanel.add(button3);

           gridLayoutPanel.add(button4);     

        }

       

        private void generateFlowLayoutPanel() {

           flowLayoutPanel new Panel();

           flowLayoutPanel.setLayout(new FlowLayout());

           Button button1 = new Button("button1");

           Button button2 = new Button("button2");

           Button button3 = new Button("button3");

           Button button4 = new Button("button4");

           Button button5 = new Button("button5");

          

           button1.addActionListener(new ActionListener() {

              

               @Override

               public void actionPerformed(ActionEvent e) {

                  ((Button)e.getSource()).setLabel("welcome ");

               }

           });

          

           flowLayoutPanel.add(button1);

           flowLayoutPanel.add(button2);

           flowLayoutPanel.add(button3);

           flowLayoutPanel.add(button4);

           flowLayoutPanel.add(button5);

        }

       

        private void generateBorderLayoutPanel() {

           borderLayoutPanel new Panel();

           borderLayoutPanel.setLayout(new BorderLayout());

           Button button1 = new Button("South");

           Button button2 = new Button("West");

           Button button3 = new Button("East");

           Button button4 = new Button("North");

           Button button5 = new Button("Center");

          

           borderLayoutPanel.add(button1,BorderLayout.SOUTH);

           borderLayoutPanel.add(button2,BorderLayout.WEST);

           borderLayoutPanel.add(button3,BorderLayout.EAST);

           borderLayoutPanel.add(button4,BorderLayout.NORTH);

           borderLayoutPanel.add(button5,BorderLayout.CENTER);

        }

       

        private void genrateCardLayoutPanel() {

           cardLayoutPanel new Panel();

           final CardLayout cl = new CardLayout();

           cardLayoutPanel.setLayout(cl);

          

           Button button1 = new Button("black");

           Button button2 = new Button("red");

          

           ActionListener al = new ActionListener() {

              

               @Override

               public void actionPerformed(ActionEvent e) {

                  cl.next(cardLayoutPanel);             

               }

           };

          

           button1.addActionListener(al);

           button2.addActionListener(al);

          

           cardLayoutPanel.add(button1,"1");

           cardLayoutPanel.add(button2,"2");

        }

       

        public YourFrame(String panelName) {

           super("panelName");

           generateBorderLayoutPanel();

           generateFlowLayoutPanel();

           generateGridLayoutPanel();

           genrateCardLayoutPanel();

          

           setLayout(new GridLayout(2,2));

          

           add(borderLayoutPanel);

           add(flowLayoutPanel);

           add(gridLayoutPanel);

           add(cardLayoutPanel);

          

           setSize(800, 800);

           setLocation(100,100);

          

           addWindowListener(new WindowAdapter(){

               @Override

               public void windowClosing(WindowEvent arg0) {

                  System.exit(0);

               }

           });

        }

       

       

       

        public static void main(String[] args) {

           YourFrame yourFrame = new YourFrame("welcome");

           yourFrame.setVisible(true);

        }

    }

    运行结果:

    java图形界面设计(AWT)

    3.       菜单栏的实现:

    菜单栏关键的类包括MenuBar、Menu和MenuItem。下面给出测试代码:

     

    package com.guan.visualTest.frameTest;

     

    import java.awt.FileDialog;

    import java.awt.Frame;

    import java.awt.Menu;

    import java.awt.MenuBar;

    import java.awt.MenuItem;

    import java.awt.TextArea;

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import java.awt.event.WindowAdapter;

    import java.awt.event.WindowEvent;

    import java.io.FileInputStream;

    import java.io.FileNotFoundException;

    import java.io.IOException;

     

    public class MenuFrame {

    public static void main(String[] args) {

        final Frame frame = new Frame();

        frame.setSize(800,800);

        frame.setLocation(100,100);

        frame.addWindowListener(new WindowAdapter(){

           @Override

           public void windowClosing(WindowEvent e) {

               System.exit(0);

           }

        });

     

        final TextArea ta = new TextArea();

        frame.add(ta);

       

        //创建菜单栏

        MenuBar mb = new MenuBar();

       

        //创建菜单

        Menu file = new Menu("File");

        Menu edit = new Menu("Edit");

       

        //创建菜单项

        MenuItem mi1 = new MenuItem("Open");

        //添加打开文件功能响应

        mi1.addActionListener(new ActionListener() {

          

           @Override

           public void actionPerformed(ActionEvent e) {

               FileDialog fd = new FileDialog(frame,"打开文件",FileDialog.LOAD);

               fd.setVisible(true);

              

               String fileName = fd.getDirectory()+fd.getFile();

               if(fileName != null)

               {

                  try {

                      FileInputStream fis = new FileInputStream(fileName);

                      byte[] buf = new byte[10*1024];

                     

                      try {

                         int len = fis.read(buf);

                         ta.append(new String(buf,0,len));

                         fis.close();

                      } catch (IOException e1) {

                          e1.printStackTrace();

                      }

                     

                  } catch (FileNotFoundException e1) {

                      e1.printStackTrace();

                  }

                 

               }

           }

        });

        MenuItem mi2 = new MenuItem("Save");

        MenuItem mi3 = new MenuItem("Other Save");

        MenuItem mi4 = new MenuItem("Close");

        //添加 关闭响应

        mi4.addActionListener(new ActionListener() {

          

           @Override

           public void actionPerformed(ActionEvent arg0) {

               System.exit(0);

           }

        });

        MenuItem mi5 = new MenuItem("Cope");

        MenuItem mi6 = new MenuItem("Paste");

       

        file.add(mi1);

        file.add(mi2);

        file.add(mi3);

        file.add(mi4);

        edit.add(mi5);

        edit.add(mi6);

       

        mb.add(file);

        mb.add(edit);

       

        frame.setMenuBar(mb);

       

        frame.setVisible(true);

    }

    }

    执行结果:

    java图形界面设计(AWT)

    4.       最后Swing的简单测试

     

    package com.guan.visualTest.frameTest;

     

    import java.awt.BorderLayout;

     

    import javax.swing.JButton;

    import javax.swing.JFrame;

     

    public class SwingFrame {

    public static void main(String[] args) {

        JFrame frame = new JFrame();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton button = new JButton("ok");

        frame.getContentPane().add(button,BorderLayout.WEST);

        frame.setSize(800,800);

        frame.setLocation(100, 100);

        frame.setVisible(true);

    }

    }

  • 相关阅读:
    属性可以通过下拉菜单选择的WebPart示例
    排序规则害死人,问个问题
    编写一个Flash播放器的WebPart
    在我的网站上开通了WebPart演示和下载列表。
    如何编写一个自动投票程序
    如何在url中引用域的内容呢?
    创建一个自定义ToolPart
    Linux下JNI的使用
    Android学习——ActivityManager与Proxy模式的运用
    Android中Adapter和Bridge模式理解和应用
  • 原文地址:https://www.cnblogs.com/zhzhang/p/5720116.html
Copyright © 2011-2022 走看看