java卡片布局 show没用_[showjava怎么用]java卡片布局中show的正确使用方法

[showjava怎么用]java卡片布局中show的正确使用方法

问题:创建了四个按钮,分别执行上一页、下一页及两个直接跳转指定页。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("第二个面板"));}}已修改,复制粘贴即可,panelMain

=new-Panel(cl);//被设定为卡片布局的面板修改如上即可import-java.awt.BorderLayout;import-java.awt.Button;import-java.awt.CardLayout;import-java.awt.FlowLayout;import-java.awt.Frame;import-java.awt.Label;import-java.awt.Panel;import-java.awt.event.ActionEvent;import-java.awt.event.ActionListener;import-java.awt.event.WindowAdapter;import-java.awt.event.WindowEvent;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(cl);//被设定为卡片布局的面板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);btPrevious.addActionListener

(new-ActionListener(){@Overridepublic-void-actionPerformed

(ActionEvent-e){cl.previous(panelMain);}});btNext.addActionListener

(new-ActionListener(){//下一页的按钮public-void-actionPerformed

(ActionEvent-e){cl.next(panelMain);}});bt1.addActionListener

(new-ActionListener(){//跳转到第二个面板的按钮public-void-actionPerformed

(ActionEvent-e){cl.show(panelMain,"pf");}});bt2.addActionListener

(new-ActionListener(){//跳转到第二个面板的按钮public-void-actionPerformed

(ActionEvent-e){cl.show(panelMain,"ps");}});addWindowListener

(new-WindowAdapter(){public-void-windowClosing(WindowEvent-wevent

){if(w)dispose();elseSystem.exit(0);}});panelXZ.setLayout

(new-FlowLayout());setTitle("主界面");setSize(800,600);setLocationRelativeTo

(null);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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值