布局管理器

发布时间:2016-12-9 23:26:02 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"布局管理器",主要涉及到布局管理器方面的内容,对于布局管理器感兴趣的同学可以参考一下。

import java.awt.*; import java.awt.event.*; public class TestLayoutFrame extends Frame {                CardLayout cl = new CardLayout();      public TestLayoutFrame() {                         MenuBar menuBar = new MenuBar();         Menu menuFile = new Menu();         MenuItem menuFileExit = new MenuItem();                                                                 Panel p1 = new Panel();         p1.setLayout(new GridLayout(7,1));         Button p1b1 = new Button("前进");         Button p1b2 = new Button("后退");         Button p1b3 = new Button("壹");         Button p1b4 = new Button("貳");         Button p1b5 = new Button("叁");         Button p1b6 = new Button("肆");         Button p1b7 = new Button("伍");      p1.add(p1b1);      p1.add(p1b2);      p1.add(p1b3);      p1.add(p1b4);      p1.add(p1b5);      p1.add(p1b6);      p1.add(p1b7);                  final Panel p2 = new Panel();         p2.setLayout(cl);      p2.add(new Button("壹"),"1");      p2.add(new Button("貳"),"2");      p2.add(new Button("叁"),"3");      p2.add(new Button("肆"),"4");      p2.add(new Button("伍"),"5");                  add(p1,"West");         add(p2,"Center");                 menuFile.setLabel("菜单");         menuFileExit.setLabel("退出");                         // Add action listener.for the menu button         menuFileExit.addActionListener         (             new ActionListener() {                 public void actionPerformed(ActionEvent e) {                     TestLayoutFrame.this.windowClosed();                 }             }         );         menuFile.add(menuFileExit);         menuBar.add(menuFile);                 setTitle("TestLayout");         setMenuBar(menuBar);         setSize(new Dimension(200, 200));                 // Add window listener.         this.addWindowListener         (             new WindowAdapter() {                 public void windowClosing(WindowEvent e) {                     TestLayoutFrame.this.windowClosed();                 }             }         );      class MyButtonListener implements ActionListener         {          public void actionPerformed(ActionEvent e)          {           if(e.getActionCommand().equals("前进"))           {            cl.previous(p2);           }           else if(e.getActionCommand().equals("后退"))           {            cl.next(p2);           }           else if(e.getActionCommand().equals("壹"))           {            cl.first(p2);           }           else if(e.getActionCommand().equals("貳"))           {            cl.show(p2,"2");           }           else if(e.getActionCommand().equals("叁"))           {            cl.show(p2,"3");           }           else if(e.getActionCommand().equals("肆"))           {            cl.show(p2,"4");           }           else if(e.getActionCommand().equals("伍"))           {            cl.last(p2);           }          }         }           MyButtonListener mbl = new MyButtonListener();         p1b1.addActionListener(mbl);      p1b2.addActionListener(mbl);      p1b3.addActionListener(mbl);      p1b4.addActionListener(mbl);      p1b5.addActionListener(mbl);      p1b6.addActionListener(mbl);      p1b7.addActionListener(mbl);         }             protected void windowClosed() {         System.exit(0);     } }       public class TestLayout {         public static void main(String[] args) {         // Create application frame.         TestLayoutFrame frame = new TestLayoutFrame();                 // Show frame         frame.setVisible(true);     } }

上一篇:タッチイベントをとりえする「onTouchEventとMotionEvent」
下一篇:第三十二天    2011年4月25日

相关文章

关键词: 布局管理器

相关评论