我编了个窗口界面想实现点击按钮切换页面的功能,就是各个面板(装有相关的组件)的切换,帮忙修改下,一实现功能,若是有别的好方法也可以帮助写下,小弟感激了啊!!!importjava.a...
我编了个窗口界面想实现点击按钮切换页面的功能,就是各个面板(装有相关的组件)的切换,帮忙修改下,一实现功能,若是有别的好方法也可以帮助写下,小弟感激了啊!!!
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Image;
import java.awt.Label;
import java.awt.event.*;
import javax.swing.*;
public class mainWind extends JFrame implements ActionListener
{
Container c;
CardLayout card;
JPanel p1,p2,p3,p4;
JMenuBar MenuB;
JButton textBut,shutDownBut,courseManBut,helpBut;
public mainWind()
{
super("贴心小助手");
ImageIcon imgIcon = new ImageIcon("100.gif");
Image img = imgIcon.getImage();
this.setIconImage(img);
setVisible(true) ;
setResizable(false);
setSize(400,300);
setLocation(getToolkit().getScreenSize().width/3
- this.getWidth()/3,
getToolkit().getScreenSize().height/3
- this.getHeight()/3);
//主菜单添加按钮
MenuB=new JMenuBar();
textBut= new JButton("时间管理");
courseManBut=new JButton("课程提醒");
shutDownBut=new JButton("定时关机");
helpBut=new JButton("帮助");
MenuB.add(textBut);
MenuB.add(courseManBut);
MenuB.add(shutDownBut);
MenuB.add(helpBut);
textBut.addActionListener(this);
courseManBut.addActionListener(this);
shutDownBut.addActionListener(this);
helpBut.addActionListener(this);
setJMenuBar(MenuB);
//定义父面板
c=this.getContentPane();
c.setLayout(card);
card = new CardLayout();
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();
p1.add(new JButton("第一面板"));
p2.add(new Label("第二面板"));
p3.add(new JButton("第三面板"));
p4.add(new Label("第四面板"));
p1.setBackground(Color.red);
p2.setBackground(Color.blue);
p3.setBackground(Color.yellow);
p4.setBackground(Color.lightGray);
c.add(p1,"1");
c.add(p2,"2");
c.add(p3,"3");
c.add(p4,"4");
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="时间管理")
{
chooseCard("1");
}
else if(e.getActionCommand()=="课程提醒")
{
chooseCard("2");
}
else if(e.getActionCommand()=="定时关机")
{
chooseCard("3");
}
else
{
chooseCard("4");
}
}
protected void chooseCard(String str)
{
card.show(c,str);
}
/**
* @param args
*/
public static void main(String[] args) {
mainWind mw = new mainWind();
mw.chooseCard("1");
// TODO 自动生成方法存根
}
}
展开