import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CardLatoutFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton btPrevious = new JButton ("前一张");
JButton btNext = new JButton ("前一张");
JPanel flowPanel = new JPanel(new FlowLayout());
JPanel cardPanel = new JPanel(new FlowLayout());
int currentIndex = 0;
@SuppressWarnings("deprecation")
public CardLayoutFrame (){
this.getContentPane().add(flowPanel,BorderLayout.SOUTH);
this.getContentPane().add(flowPanel,BorderLayout.CENTER);
cardPanel.add(getCard(1),"Card1");
cardPanel.add(getCard(2),"Card2");
flowPanel.add(btPrevious);
flowPanel.add(btNext);
ActionListener listener = new ActionListener (){
public void actionPerformed(ActionEvent e){
switchCard();
}
};
btPrevious.addActionListener (listener);
btNext.addActionListener (listener);
this.setSize(300,200);
this.setTitle("GardLayoutDemo");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.show();
}
JPanel getCard(int index){
JPanel panel = new JPanel (new BorderLayout());
JLabel label = new JLabel ("<HTML><h1 style = color:red>"+"这是第"+index+"张卡片"+"<h1></HTML>");
label.setHorizontalAlignment(JLabel.CENTER);
panel.add(label);
return panel;
}
void switchCard(){
CardLayout cl = (CardLayout)cardPanel.getLayout();
if(currentIndex == 0){
currentIndex++;
cl.show(cardPanel,"card2");
}else{
currentIndex--;
cl.show(cardPanel,"card1");
}
}
public static void main(String[] args){
CardLatoutFrame frame = new CardLatoutFrame();
}
}
在eclipse中运行 没有任何结果,好像中间this.show()语句有问题,还有最后主程序没法实例化。
还有这句 private static final long serialVersionUID = 1L; 是什么意思,eclipse加的
import javax.swing.*;
import java.awt.event.*;
public class CardLatoutFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton btPrevious = new JButton ("前一张");
JButton btNext = new JButton ("前一张");
JPanel flowPanel = new JPanel(new FlowLayout());
JPanel cardPanel = new JPanel(new FlowLayout());
int currentIndex = 0;
@SuppressWarnings("deprecation")
public CardLayoutFrame (){
this.getContentPane().add(flowPanel,BorderLayout.SOUTH);
this.getContentPane().add(flowPanel,BorderLayout.CENTER);
cardPanel.add(getCard(1),"Card1");
cardPanel.add(getCard(2),"Card2");
flowPanel.add(btPrevious);
flowPanel.add(btNext);
ActionListener listener = new ActionListener (){
public void actionPerformed(ActionEvent e){
switchCard();
}
};
btPrevious.addActionListener (listener);
btNext.addActionListener (listener);
this.setSize(300,200);
this.setTitle("GardLayoutDemo");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.show();
}
JPanel getCard(int index){
JPanel panel = new JPanel (new BorderLayout());
JLabel label = new JLabel ("<HTML><h1 style = color:red>"+"这是第"+index+"张卡片"+"<h1></HTML>");
label.setHorizontalAlignment(JLabel.CENTER);
panel.add(label);
return panel;
}
void switchCard(){
CardLayout cl = (CardLayout)cardPanel.getLayout();
if(currentIndex == 0){
currentIndex++;
cl.show(cardPanel,"card2");
}else{
currentIndex--;
cl.show(cardPanel,"card1");
}
}
public static void main(String[] args){
CardLatoutFrame frame = new CardLatoutFrame();
}
}
在eclipse中运行 没有任何结果,好像中间this.show()语句有问题,还有最后主程序没法实例化。
还有这句 private static final long serialVersionUID = 1L; 是什么意思,eclipse加的
是要实现点了上一张下一张显示不同的字还是干什么
点的时候会抛个异常
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.awt.FlowLayout cannot be cast to java.awt.CardLayoutthis.show()过时了,最好用setVisible(true)
serialVersionUID 是实现序列化的一个标识,相当于序列号,不要也可以
这里转型可能有问题,把LayoutManager强转成CardLayout了
CardLayout cl = (CardLayout) cardPanel.getLayout();this.show(),这里show方法是过时的方法,但还是可以用的private static final long serialVersionUID = 1L; 这句是序列化用的
JPanel cardPanel = new JPanel(new FlowLayout() ); 改为
JPanel cardPanel = new JPanel(new CardLayout() );
this.getContentPane().add(flowPanel,BorderLayout.SOUTH);
this.getContentPane().add(flowPanel,BorderLayout.CENTER);
把同一个panel加到south又加到center?
是
this.getContentPane().add(flowPanel, BorderLayout.SOUTH);
this.getContentPane().add(cardPanel, BorderLayout.CENTER);
还是怎么的
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;public class Test extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton btPrevious = new JButton("前一张");
JButton btNext = new JButton("前一张");
JPanel flowPanel = new JPanel(new FlowLayout());
JPanel cardPanel = new JPanel(new CardLayout() );
int currentIndex = 0; public Test() {
this.getContentPane().add(flowPanel, BorderLayout.SOUTH);
this.getContentPane().add(cardPanel, BorderLayout.CENTER);
cardPanel.add(getCard(1), "Card1");
cardPanel.add(getCard(2), "Card2");
flowPanel.add(btPrevious);
flowPanel.add(btNext);
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
switchCard();
}
};
btPrevious.addActionListener(listener);
btNext.addActionListener(listener);
this.setSize(300, 200);
this.setTitle("GardLayoutDemo");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
} JPanel getCard(int index) {
JPanel panel = new JPanel(new BorderLayout());
JLabel label = new JLabel(" <HTML> <h1 style = color:red>" + "这是第"
+ index + "张卡片" + " <h1> </HTML>");
label.setHorizontalAlignment(JLabel.CENTER); panel.add(label);
return panel;
} void switchCard() {
CardLayout cl = (CardLayout) cardPanel.getLayout();
if (currentIndex == 0) {
currentIndex++;
cl.show(cardPanel, "Card2");
} else {
currentIndex--;
cl.show(cardPanel, "Card1");
}
} public static void main(String[] args) {
Test frame = new Test();
}}顺便说一下,你创建的时候
cardPanel.add(getCard(1), "Card1");
cardPanel.add(getCard(2), "Card2");
下面调用的时候用的是
cl.show(cardPanel,"card2");
cl.show(cardPanel,"card1");
Card1和card1啊。。