我看着那个使用该代码的代码示例:
cl.show(cardPanel, "" + (currentCard));
但是当我使用show
我得到Eclipse的一个消息,它的过时,我不知道是否有另一种方式展现在CardLayout不同的卡,当我点击按钮? 下面是我的CardLayout类的代码。 建议也欢迎,如果代码的某些部分是不好的做法。 谢谢!
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class CardLayoutTest extends JFrame implements ActionListener {
// Ref
private JPanel cardPanel, jp1, jp2, buttonPanel;
private JLabel jl1, jl2;
private JButton btn1, btn2;
private CardLayout cardLayout;
// Konstruktor
public CardLayoutTest()
{
setTitle("Test med CardLayout");
setSize(600,400);
cardPanel = new JPanel();
buttonPanel = new JPanel();
cardPanel.setLayout(cardLayout);
jp1 = new JPanel();
jp2 = new JPanel();
jl1 = new JLabel("Card 1");
jl2 = new JLabel("Card 2");
jp1.add(jl1);
jp2.add(jl2);
cardPanel.add(jp1, "1");
cardPanel.add(jp2, "2");
btn1 = new JButton("Show Card 1");
btn2 = new JButton("Show Card 2");
buttonPanel.add(btn1);
buttonPanel.add(btn2);
getContentPane().add(cardPanel, BorderLayout.NORTH);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
btn1.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
// ??? Show card 1 ???
// ??? Show card 2 ???
}
public static void main(String[] args) {
CardLayoutTest frame = new CardLayoutTest();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
我看不到Java7秀(集装箱父,字符串名称)或的Java6秀(集装箱父,字符串名称)折旧
取决于如果currentCard
返回String
从cl.show(cardPanel, "" + (currentCard));
编辑(我想你的代码示例)
1.you忘了初始化最重要的变量
private CardLayout cardLayout = new CardLayout();
2.然后SSCCE可能是
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class CardLayoutTest extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel cardPanel, jp1, jp2, buttonPanel;
private JLabel jl1, jl2;
private JButton btn1, btn2;
private CardLayout cardLayout = new CardLayout();
public CardLayoutTest() {
setTitle("Test med CardLayout");
setSize(400, 300);
cardPanel = new JPanel();
buttonPanel = new JPanel();
cardPanel.setLayout(cardLayout);
jp1 = new JPanel();
jp2 = new JPanel();
jl1 = new JLabel("Card 1");
jl2 = new JLabel("Card 2");
jp1.add(jl1);
jp2.add(jl2);
cardPanel.add(jp1, "1");
cardPanel.add(jp2, "2");
btn1 = new JButton("Show Card 1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.show(cardPanel, "1");
}
});
btn2 = new JButton("Show Card 2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.show(cardPanel, "2");
}
});
buttonPanel.add(btn1);
buttonPanel.add(btn2);
add(cardPanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.SOUTH);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
CardLayoutTest frame = new CardLayoutTest();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
这是最有可能的,你正在呼吁的JPanel显示(),而不是CardLayout。
该方法显示()应用于的JPanel存在(更具体成分)和已被由setVisible()取代。 这是从CardLayout的show()方法完全不同。
确保你正在做的事情,如下面在动作监听器
CardLayout cardLayout = (CardLayout)(cardPanel.getLayout());
cardLayout.show(cardPanel, "CardToShow");
作为一个侧面说明,它不出现你是“newing”你CardLayout。 请确保你做到这一点。