布局管理器—CardLayout、GridLayout(Panel組件)
/**?* @(#)TestCardLayout.java
?*
?* TestCardLayout application
?*
?* @author
?* @version 1.00 2007/1/21
?*/
?import java.awt.*;
?import java.awt.event.*;
?
public class TestCardLayout extends Frame
{?
?CardLayout cl=new CardLayout();
??? Panel plCenter=new Panel(cl);
??? public TestCardLayout()
??? {
??? ?GridLayout gl=new GridLayout(5,1);
??? ?Panel plWest=new Panel(gl);
??? ?Button first=new Button("first");
??? ?Button prev=new Button("prev");
??? ?Button next=new Button("next");
??? ?Button three=new Button("three");
??? ?Button last=new Button("last");
??? ?plWest.add(first);
??? ?plWest.add(prev);
??? ?plWest.add(next);
??? ?plWest.add(three);
??? ?plWest.add(last);
??
??? ?plCenter.add(new Button("one"),"1");
??? ?plCenter.add(new Button("two"),"2");
??? ?plCenter.add(new Button("three"),"3");
??? ?plCenter.add(new Button("four"),"4");
??? ?plCenter.add(new Button("five"),"5");
??? ?add(plWest,"West");
??? ?add(plCenter);
??? ?class MyactionListener implements ActionListener
??? ?{
??? ??public void actionPerformed(ActionEvent e)
??? ??{
??? ???if(e.getActionCommand().equals("first"))
??? ???{
??? ????cl.first(plCenter);
??? ???}
??? ???else if(e.getActionCommand().equals("prev"))
??? ???{
??? ????cl.previous(plCenter);
??? ???}
??? ???else if(e.getActionCommand().equals("next"))
??? ???{
??? ????cl.next(plCenter);
??? ???}
??? ???else if(e.getActionCommand().equals("three"))
??? ???{
??? ????cl.show(plCenter,"3");
??? ???}
??? ???else
??? ???{
??? ????cl.last(plCenter);
??? ???}
??? ??}
??? ?}
??? ?MyactionListener ml=new MyactionListener();
??? ?first.addActionListener(ml);
??? ?prev.addActionListener(ml);
??? ?next.addActionListener(ml);
??? ?three.addActionListener(ml);
??? ?last.addActionListener(ml);
??? ?addWindowListener(new WindowAdapter()
??? ?{
??? ??public void windowClosing(WindowEvent e)
??? ??{
??? ???e.getWindow().dispose();
??? ??}
??? ?});
??? }
??? public static void main(String[] args)
??? {
??? ?
??? ?// TODO, add your application code
??? ?System.out.println("Hello World!");
??? ?TestCardLayout mainFrame=new TestCardLayout();
??? ?mainFrame.setTitle("TestCardLayout");
??? ?mainFrame.setBounds(300,200,400,400);
??? ?mainFrame.setVisible(true);
??? ?
??? }
}
posted on 2007-01-21 13:01 大頭劍客 閱讀(615) 評論(0) 編輯 收藏 所屬分類: 學(xué)習(xí)筆記