關於部落格
雨--是天空的眼淚 風--是蝴蝶的痴醉 雲--是水珠的嫵媚 光--是溫暖的慰藉 夜--是孤單的滋味 星--是墜跌的心碎 蹲下,是為了躍起。 後退,是為了向前跳得更遠。
  • 171475

    累積人氣

  • 4

    今日人氣

    0

    訂閱人氣

Java 卡片切換(cardlayout)



檔名:card.java


習慣這樣貼程式碼
因為複製下來後
才會自己縮排
邊縮排邊看
這樣才比較好懂


 

import java.awt.*;
import java.awt.event.*;
public class card extends Frame
{
	Button b1=new Button("card1");
	Button b2=new Button("card2");
	Button b3=new Button("card3");
	Button b4=new Button("card4");

	Panel buttons = new Panel();
	Panel cards = new Panel();
	Panel c1 = new Panel();
	Panel c2 = new Panel();
	Panel c3 = new Panel();
	Panel c4 = new Panel();

	CardLayout c=new CardLayout();

	Label l1 =new Label("Tis is the 1st card panel");
	Label l2 =new Label("Tis is the 2st card panel");
	Label l3 =new Label("Tis is the 3st card panel");
	Label l4 =new Label("Tis is the 4st card panel");

	public card()
	{
		this.setResizable(false);
		this.setLayout(new FlowLayout(FlowLayout.LEFT));

		this.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
		b1.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				c.show(cards,"1");
			}
		});
		b2.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				c.show(cards,"2");
			}
		});
		b3.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				c.show(cards,"3");
			}
		});
		b4.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				c.show(cards,"4");
			}
		});

		buttons.setLayout(new GridLayout(2,2));
		buttons.add(b1);
		buttons.add(b2);
		buttons.add(b3);
		buttons.add(b4);

		cards.setLayout(c);
		c1.add(l1);
		c2.add(l2);
		c3.add(l3);
		c4.add(l4);
		c1.setBackground(Color.yellow);
		c2.setBackground(Color.red);
		c3.setBackground(Color.blue);
		c4.setBackground(Color.green);
		cards.add("1",c1);
		cards.add("2",c2);
		cards.add("3",c3);
		cards.add("4",c4);

		this.add(buttons);
		this.add(cards);
		this.setSize(300,100);
		this.setVisible(true);
	}

	public static void main(String[] para)
	{
		new card();
	}
}




文:葉小誠

 

相簿設定
標籤設定
相簿狀態