2013년 12월 29일 일요일

자바 AWT 카드레이아웃 (cardlayout) 예제[재직자무료교육/프로그래머교육/구로디지털IT교육,오라클/자바/닷넷/C#/iOS/안드로이드/아이폰교육]

자바 AWT 카드레이아웃 (cardlayout) 예제[재직자무료교육/프로그래머교육/구로디지털IT교육,오라클/자바/닷넷/C#/iOS/안드로이드/아이폰교육] 



자바 AWT 카드레이아웃 (cardlayout) 예제
 
import java.awt.*;
import java.awt.event.*;
public class CardLayoutTest1 extends Frame {
 public static void main(String[] args) {
   Frame f = new Frame("CardLayoutTest");

   final Panel tabs = new Panel();
 
   tabs.add(new Button("<<"));
     tabs.add(new Button("<"));
   tabs.add(new Button("Options"));
   tabs.add(new Button("Settings"));
   tabs.add(new Button("Preferences"));
   tabs.add(new Button(">"));
   tabs.add(new Button(">>"));
   f.add(tabs, "North");
  
   final CardLayout layout = new CardLayout();
   final Panel cards = new Panel(layout);
   cards.add(new CardPanel("Options"), "Options");
   cards.add(new CardPanel("Settings"), "Settings");
   cards.add(new CardPanel("Preferences"), "Preferences");
   f.add(cards, "Center");
  
  
   ActionListener al = new ActionListener() {
    public void actionPerformed(ActionEvent ev) {
     String cmd = ev.getActionCommand();
     if (cmd.equals("<<")) layout.first(cards);
     else if (cmd.equals("<")) layout.previous(cards);
     else if (cmd.equals(">")) layout.next(cards);
   else if (cmd.equals(">>")) layout.last(cards);
   else layout.show(cards, cmd);
       }
   };
  
   for(int i=0; i<tabs.getComponentCount();i++) {
    ((Button)tabs.getComponent(i)).addActionListener(al);
   }
   f.setSize(400, 300);
   f.setVisible(true);  
   f.addWindowListener(new WindowListenerProcessing() );
 }
}
class CardPanel extends Panel {
 CardPanel(String name) {
  setLayout(new BorderLayout());
  add(new Label("CardPanel : " + name, Label.CENTER),"Center");
  Panel p = new Panel();
  add(p, "South");
  Button btn = new Button("Close");
  ActionListener a1 = new ActionListener() {
   public void actionPerformed(ActionEvent ev) {
    String cmd = ev.getActionCommand();
    if (cmd.equals("Close")) {
     System.exit(0);
    }
   }
  };
  btn.addActionListener(a1);
  p.add(btn); 
 }
}
class WindowListenerProcessing extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
             System.exit(0);
    }
}
 

댓글 없음:

댓글 쓰기