awt布局管理器例子

import java.awt.*;
public class ExGui{
    private Frame f;
    private Button b1;
    private Button b2;
    public static void main(String args[]){
        ExGui that = new ExGui();
        that.go();
    }

    public void go(){
        f = new Frame("GUI example");
        f.setLayout(new FlowLayout());
        //设置布局管理器为FlowLayout
        b1 = new Button("Press Me");
        //按钮上显示字符"Press Me"
        b2 = new Button("Don't Press Me");
        f.add(b1);
        f.add(b2);
        f.pack();
        //紧凑排列,其作用相当于setSize(),即让窗口尽量小,小到刚刚能够包容住b1、b2两个按钮
        f.setVisible(true);
    }
}

 

 

多按钮

 

 

import java.awt.*;
 import java.awt.event.*; //事件处理机制,下一节的内容
 public class ThreePages implements MousListener {
    CardLayout layout=new CardLayout(); //实例化一个牌布局管理器对象
    Frame f=new Frame("CardLayout");
    Button page1Button;
    Label page2Label; //Label是标签,实际上是一行字符串
    TextArea page3Text; //多行多列的文本区域
    Button page3Top;
    Button page3Bottom;

 public static void main(String args[])
 { new ThreePages().go(); }


 Public void go()
 {   f.setLayout(layout); //设置为牌布局管理器layout
    f.add(page1Button=new Button("Button page"),"page1Button"); /*第二个参数"page1Button"表示的是你对这层牌所取的名字*/
    page1Button.addMouseListener(this); //注册监听器
    f.add(page2Label=new Label("Label page"),"page2Label");
    page2Label.addMouseLisener(this); //注册监听器
    Panel panel=new Panel();
    panel.setLayout(new BorderLayout());
    panel.add(page3Text=new TextArea("Composite page"),"Center");
    page3Text.addMouseListener(this);
    panel.add(page3Top=new Button("Top button") , "North");
    page3Top.addMouseListener(this);
    panel.add(page3Bottom=new Button("Bottom button") ,"South");
    page3Bottom.addMouseListener(this);
    f.add(panel,"panel");
    f.setSize(200,200);
    f.setVisible(true);
 }

 

 

四个按钮的例子

 

 

    import java.awt.*;
    public class ExGui3{
    private Frame f;
    private Panel p;
    private Button bw,bc;
    private Button bfile,bhelp;
       public static void main(String args[])
       {
         ExGui3 gui = new ExGui3();
         gui.go();
       }

    public void go(){
       f = new Frame("GUI example 3");
       bw=new Button("West");
       bc=new Button("Work space region");
       f.add(bw,"West");
       f.add(bc,"Center");
       p = new Panel();
       f.add(p,"North");
       bfile= new Button("File");

       bhelp= new Button("Help");
       p.add(bfile);
       p.add(bhelp);
       f.pack();
       f.setVisible(true);
    }
    }

 

 

按钮响应的例子

    import java.awt.*;
    import java.awt.event.*;
    public class TestButton {
    public static void main(String args[])
    {
      Frame f = new Frame("Test");
      Button b = new Button("Press Me!");
      b.addActionListener(new ButtonHandler()); /*注册监听器进行授权,该方法的参数是事件处理者对象,要处理的事件类型可以从方法名中看出,例如本方法要授权处理的是ActionEvent,因为方法名是addActionListener。*/
      f.setLayout(new FlowLayout()); //设置布局管理器
      f.add(b);
      f.setSize(200,100);
      f.setVisible(true);
      }
    }
    class ButtonHandler implements ActionListener {
    //实现接口ActionListener才能做事件ActionEvent的处理者

    public void actionPerformed(ActionEvent e)
    //系统产生的ActionEvent事件对象被当作参数传递给该方法

    {

                   Frame f = new Frame("new Test");
                   f.setSize(300,60);
                   f.setVisible(true);
                  System.out.println("Action occurred");

      System.out.println("Action occurred");
    //本接口只有一个方法,因此事件发生时,系统会自动调用本方法,需要做的操作就把代码写在则个方法里。

    }
    }

 

 

监听及响应:

import java.awt.*;
  import java.awt.event.*;
    public class ThreeListener implements MouseMotionListener,MouseListener,WindowListener {
    //实现了三个接口
    private Frame f;
    private TextField tf;
    public static void main(String args[])
    {
     ThreeListener two = new ThreeListener();
     two.go(); }

    public void go() {
    f = new Frame("Three listeners example");
    f.add(new Label("Click and drag the mouse"),"North");
    tf = new TextField(30);
    f.add(tf,"South"); //使用缺省的布局管理器
    f.addMouseMotionListener(this); //注册监听器MouseMotionListener
    f.addMouseListener(this); //注册监听器MouseListener
    f.addWindowListener(this); //注册监听器WindowListener
    f.setSize(300,200);
    f.setVisible(true);
      }

    public void mouseDragged (MouseEvent e) {
    //实现mouseDragged方法
    String s = "Mouse dragging : X="+e.getX()+"Y = "+e.getY();
    tf.setText(s);
      }
    public void mouseMoved(MouseEvent e){}
    //对其不感兴趣的方法可以方法体为空
    public void mouseClicked(MouseEvent e){}
    public void mouseEntered(MouseEvent e){
      String s = "The mouse entered";
      tf.setText(s);
        }

    public void mouseExited(MouseEvent e){
      String s = "The mouse has left the building";
      tf.setText(s);
        }

    public void mousePressed(MouseEvent e){}
    public void mouseReleased(MouseEvent e){ }
    public void windowClosing(WindowEvent e) {
    //为了使窗口能正常关闭,程序正常退出,需要实现windowClosing方法
      System.exit(1);
        }

    public void windowOpened(WindowEvent e) {}
    //对其不感兴趣的方法可以方法体为空
    public void windowIconified(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowClosed(WindowEvent e) {}
    public void windowActivated(WindowEvent e) { }
    public void windowDeactivated(WindowEvent e) {}

    }

 

添加菜单

 

import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TestAWT implements KeyListener, MouseListener ,WindowListener{
    Canvas c;
    String s =new String("");
    public static void main(String args[]) {
        Frame fr = new Frame("MenuBar");
        MenuBar mb = new MenuBar();
        fr.setMenuBar(mb);
        Menu m1 = new Menu("File");
        Menu m2 = new Menu("Edit");
        Menu m3 = new Menu("Help");
        mb.add(m1);
        mb.add(m2);
        mb.setHelpMenu(m3);
        MenuItem mi1 = new MenuItem("Save");
        MenuItem mi2 = new MenuItem("Load");
        MenuItem mi3 = new MenuItem("Quit");

        m1.add(mi1);
        m1.add(mi2);
        m1.addSeparator();
        m1.add(mi3);

        fr.setSize(200,200);
        fr.setVisible(true);
    }

    public void mouseClicked(MouseEvent ev){
        System.out.println("MouseClicked");
        c.requestFocus();
    }

    public void keyTyped(KeyEvent ev) {

        String val=  new String("");
        System.out.println("KeyTyped");
        val +=ev.getKeyChar();
        s =val;   
        //s = "value is : "+String.valueOf( Integer.parseInt(val));
        c.getGraphics().drawString(s,0,20);
    }

    public void keyPressed(KeyEvent ev) {

        System.out.println("KeyPressed");
    }   
    public void keyReleased(KeyEvent ev) {
        System.out.println("KeyReleased");
    }

    public void mousePressed(MouseEvent ev) {
        System.out.println("MousePressed");
    }

    public void mouseReleased(MouseEvent ev) {
        System.out.println("MouseReleased");
    }

    public void mouseEntered(MouseEvent ev) {
        System.out.println("MouseEntered");
    }

    public void mouseExited(MouseEvent ev) {
        System.out.println("MouseExited");
    }

    public void windowClosing(WindowEvent e) {
        System.exit(1);
    }

    public void windowOpened(WindowEvent e) {}
    //对其不感兴趣的方法可以方法体为空
    public void windowIconified(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowClosed(WindowEvent e) {}
    public void windowActivated(WindowEvent e) { }
    public void windowDeactivated(WindowEvent e) {}
}

你可能感兴趣的:(awt布局管理器例子)