java中的CardLayout的运用
我编了个窗口界面想实现点击按钮切换页面的功能,就是各个面板(装有相关的组件)的切换,帮忙修改下,一实现功能,若是有别的好方法也可以帮助写下,小弟感激了啊!!!import...
我编了个窗口界面想实现点击按钮切换页面的功能,就是各个面板(装有相关的组件)的切换,帮忙修改下,一实现功能,若是有别的好方法也可以帮助写下,小弟感激了啊!!!
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 自动生成方法存根
}
} 展开
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 自动生成方法存根
}
} 展开
3个回答
展开全部
//大致的解决方案是这样滴....
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import java.awt.*;
public class MyFrame extends JFrame{
private static final long serialVersionUID = 1L;
private int width = 400;
private int height = 300;
private static Toolkit kit = Toolkit.getDefaultToolkit();
JTabbedPane jpan = new JTabbedPane();
int pnum = 4;
JPanel[] pans = new JPanel[pnum];
String [] titles = {"时间管理", "课程提醒", "定时关机", "帮助"};
public MyFrame() {
this.defaultInit();
}
public MyFrame(String title) {
super(title);
this.defaultInit();
}
public MyFrame(String title, int width, int height) {
this(title);
this.width = width;
this.height = height;
this.defaultInit();
}
private void defaultInit() {
for(int i = 0; i < pnum; i ++) {
pans[i] = new JPanel();
jpan.addTab(titles[i], pans[i]);
}
Dimension d = kit.getScreenSize();
this.setLayout(new BorderLayout());
this.add(jpan, BorderLayout.CENTER);
this.setSize(width, height);
this.setLocation((int)(d.getWidth() - width) / 2, (int)(d.getHeight() - height) / 2);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new MyFrame();
}
public static Toolkit getKit() {
return kit;
}
}
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import java.awt.*;
public class MyFrame extends JFrame{
private static final long serialVersionUID = 1L;
private int width = 400;
private int height = 300;
private static Toolkit kit = Toolkit.getDefaultToolkit();
JTabbedPane jpan = new JTabbedPane();
int pnum = 4;
JPanel[] pans = new JPanel[pnum];
String [] titles = {"时间管理", "课程提醒", "定时关机", "帮助"};
public MyFrame() {
this.defaultInit();
}
public MyFrame(String title) {
super(title);
this.defaultInit();
}
public MyFrame(String title, int width, int height) {
this(title);
this.width = width;
this.height = height;
this.defaultInit();
}
private void defaultInit() {
for(int i = 0; i < pnum; i ++) {
pans[i] = new JPanel();
jpan.addTab(titles[i], pans[i]);
}
Dimension d = kit.getScreenSize();
this.setLayout(new BorderLayout());
this.add(jpan, BorderLayout.CENTER);
this.setSize(width, height);
this.setLocation((int)(d.getWidth() - width) / 2, (int)(d.getHeight() - height) / 2);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new MyFrame();
}
public static Toolkit getKit() {
return kit;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询