创建了四个按钮,分别执行上一页、下一页及两个直接跳转指定页。previous和next都没问题,但是show却始终无效,求高手帮忙看下代码。importjava.awt.*;importjava.awt.event.*;public...

创建了四个按钮,分别执行上一页、下一页及两个直接跳转指定页。previous和next都没问题,但是show却始终无效,求高手帮忙看下代码。

import java.awt.*;

import java.awt.event.*;

public class Ceshi extends Frame

{

PanelF pf = new PanelF();

PanelS ps = new PanelS();

Panel panelMain,panelXZ;

Button btNext,btPrevious,bt1,bt2,bt3,bt4,bt5;

CardLayout cl;

Boolean w = false;

public Ceshi()

{

cl = new CardLayout();

panelMain = new Panel();//被设定为卡片布局的面板

panelXZ = new Panel();//构造放按钮的面板

btPrevious = new Button("上一页");

btNext = new Button("下一页");

bt1 = new Button("1");

bt2 = new Button("2");

//给定义为卡片布局的面板添加面板

panelMain.add(pf,"pf");

panelMain.add(ps,"ps");

panelXZ.add(btPrevious);

panelXZ.add(btNext);

panelXZ.add(bt1);

panelXZ.add(bt2);

btNext.addActionListener(new ActionListener(){//下一页的按钮

public void actionPerformed(ActionEvent e)

{

cl.next(panelMain);

}

});

bt2.addActionListener(new ActionListener(){//跳转到第二个面板的按钮

public void actionPerformed(ActionEvent e)

{

cl.show(panelMain, "ps");

}

});

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent wevent)

{

if(w)

dispose();

else

System.exit(0);

}

});

panelMain.setLayout(cl);

panelXZ.setLayout(new FlowLayout());

setTitle("主界面");

setSize(800, 600);

setLocation(500,250);

add(panelMain,BorderLayout.CENTER);//把设为卡片布局的面板放置在窗体中间

add(panelXZ,BorderLayout.SOUTH);//把带按钮的面板放置在底部

setVisible(true);

}

public static void main(String args[])

{

new Ceshi();

}

}

//第一个面板

class PanelF extends Panel

{

public PanelF()

{

add(new Label("第一个面板"));

}

}

//第二个面板

class PanelS extends Panel

{

public PanelS()

{

add(new Label("第二个面板"));

}

}

展开

更多推荐

show java使用教程_java卡片布局中show的正确使用方法