package awt;
import java.awt.*;
import java.awt.event.*;
public class CardTest2 extends Frame implements ActionListener, ItemListener
{
@Override
public void itemStateChanged(ItemEvent e)
{
int idx=ch.getSelectedIndex();
String item=ch.getSelectedItem();
this.setTitle(idx+1+"번째 있는"+item+" 선택");
}
Button btn1,btn2,btn3;
CardLayout card=new CardLayout();
Panel pCenter=new Panel();
Panel p1,p2,p3;
Label label1,label2,label3;
Choice ch; //스크롤 다운 박스
public CardTest2(String title)
{
super(title);
this.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
this.setDesign();
this.setLocation(300, 100);
this.setSize(300, 300);
this.setVisible(true);
}
public void setDesign()
{
Panel pTop=new Panel();
pTop.setBackground(Color.pink);
btn1=new Button("card1");
btn2=new Button("card2");
btn3=new Button("card3");
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
pTop.add(btn1);
pTop.add(btn2);
pTop.add(btn3);
this.add("North",pTop);
pCenter.setLayout(card);
p1=new Panel();
label1=new Label("==== Card 1 =====");
label1.setForeground(Color.red);
label1.setFont(new Font("Impact",Font.BOLD,20));
p1.add(label1);
p2=new Panel();
label2=new Label("==== Card 2 =====");
label2.setForeground(Color.green);
label2.setFont(new Font("맑은고딕코딩",Font.BOLD,20));
p2.add(label2);
p3=new Panel();
label3=new Label("==== Card 3 =====");
label3.setForeground(Color.pink);
label3.setFont(new Font("Arial",Font.BOLD,20));
p3.add(label3);
pCenter.add("one", p1);
pCenter.add("two", p2);
pCenter.add("three", p3);
card.show(pCenter, "one");
this.add("Center",pCenter);
Panel pBottom=new Panel();
pBottom.setBackground(Color.yellow);
ch=new Choice();
ch.addItemListener(this);
ch.add("red");
ch.add("green");
ch.add("blue");
ch.add("white");
ch.add("yellow");
pBottom.add(new Label("좋아하는 색은?"));
pBottom.add(ch);
this.add("South",pBottom);
setBackground(Color.darkGray);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object ob=e.getSource();
if(ob==btn1)
card.show(pCenter, "one");
else if(ob==btn2)
card.show(pCenter, "two");
else
card.show(pCenter, "three");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new CardTest2("카드 레이아웃 문제");
}
}
본 블로그는 페이스북 댓글을 지원합니다.