?? cardtest.java
字號(hào):
package cardtest;
import java.awt.*;
import java.awt.event.*;
//(0)參見教材P-118 程序8-4:
//CardTest是繼承鼠標(biāo)適配器(既:MouseAdapter)的類
//CardTest類內(nèi)定義了三個(gè)函數(shù)
//main( )、init( )、mouseClicked ( MouseEvent e )
public class CardTest extends MouseAdapter{
int i=1 ;
public CardTest() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
Panel p1,p2,p3,p4,p5,p6,p7;
Label l1,l2,l3,l4,l5,l6,l7;
//(1)聲明一個(gè)CardLayout類對(duì)象
CardLayout myCard;
Frame f;
public static void main(String args[]){
CardTest ct=new CardTest();
ct.init();
}
public void init() {
f=new Frame("Card Test");
myCard=new CardLayout();
f.setLayout(myCard);
p1=new Panel();
p2=new Panel();
p3=new Panel();
p4=new Panel();
p5=new Panel();
p6=new Panel();
p7=new Panel();
//(2)為每個(gè)Panel創(chuàng)建一個(gè)標(biāo)簽并設(shè)定不同的背景顏色,以便區(qū)分
l1=new Label("This is the first Panel");
p1.add(l1);
p1.setBackground(Color.yellow);
l2=new Label("This is the second Panel");
p2.add(l2);
p2.setBackground(Color.green);
l3=new Label("This is the third Panel");
p3.add(l3);
p3.setBackground(Color.magenta);
l4=new Label("This is the forth Panel");
p4.add(l4);
p4.setBackground(Color.white);
l5=new Label("This is the fifth Panel");
p5.add(l5);
p5.setBackground(Color.cyan);
l6=new Label("This is the sixth Panel");
p6.add(l6);
p1.setBackground(Color.red);
l7=new Label("This is the seventh Panel");
p7.add(l7);
p1.setBackground(Color.red);
//(3)設(shè)定(即:注冊(cè))鼠標(biāo)事件的監(jiān)聽程序
p1.addMouseListener(this);
p2.addMouseListener(this);
p3.addMouseListener(this);
p4.addMouseListener(this);
p5.addMouseListener(this);
p6.addMouseListener(this);
p7.addMouseListener(this);
keyclick kc=new keyclick() ; //****(關(guān)閉方法-1-(1))
WindowsClose wc=new WindowsClose() ;//****(關(guān)閉方法-3-(1))
//(4)將每個(gè)Panel作為一張卡片加入Frame
f.add(p1,"First");
f.add(p2,"Second");
f.add(p3,"Third");
f.add(p4,"Forth");
f.add(p5,"Fifth");
f.add(p6,"sixth");
f.add(p7,"seventh");
f.addKeyListener(kc);//***
this.f.addWindowListener(wc);
//(5)顯示第一張卡片
myCard.show(f,"First");
f.setSize(300,200);
f.show();
}
//(6)處理鼠標(biāo)事件:
//若單擊鼠標(biāo)鍵,則顯示下一張卡片.
//若已經(jīng)顯示到最后一張,則重新顯示第一張.
public void mouseClicked(MouseEvent e){
myCard.next(f);
i++;
if (i==7){//(關(guān)閉方法-2-(1))
System.exit(0);
}
}
private void jbInit() throws Exception {
}
}
class keyclick extends KeyAdapter{ //****(關(guān)閉方法-1-(2))
public void keyPressed(KeyEvent e){
System.exit(0); //****(關(guān)閉方法-1-(3))
}
}
class WindowsClose extends WindowAdapter{ //****(關(guān)閉方法-3-(2))
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
//編譯源代碼:Javac CardTest.java
//運(yùn)行字節(jié)碼:Java CardTest
//運(yùn)行結(jié)果:若單擊鼠標(biāo)鍵,則顯示下一張卡片
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -