【发布时间】:2012-03-08 12:17:05
【问题描述】:
我在 3 个单独的类中有 3 个窗口,我想使用 cardLayout 以便当您单击下一个按钮时,会出现下一个窗口。如何将包含不同元素的 JPanel 添加到一个 cardLayout?这是第一个窗口:(虽然唯一的区别是背景 - 但它代表了我实际上是如何得到它的想法)
public class Window1 extends JPanel implements ActionListener {
static CardLayout cardLayout = new CardLayout();
public Window1() {
init();
}
private void init() {
JPanel jp = new JPanel(new BorderLayout());
JPanel jp2 = new Window2();
//JPanel jp3 = new Window3();
JLabel textLabel = new JLabel("Window1");
jp.setBackground(Color.GREEN);
jp.add(textLabel, BorderLayout.CENTER);
JButton nextButton = new JButton("NEXT");
nextButton.setActionCommand("next");
nextButton.addActionListener(this);
jp.add(nextButton, BorderLayout.EAST);
setLayout(cardLayout);
add(jp, "string");
add(jp2, "string");
//add(jp3, "string");
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("next")) {
// go to the next window
cardLayout.next(this);
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("test");
frame.getContentPane().setLayout(Window1.cardLayout);
frame.getContentPane().add(new Window1(), "Center");
frame.getContentPane().add(new Window2(), "Center");
frame.getContentPane().add(new Window3(), "Center");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(550, 450);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
第二个窗口:
public class Window2 extends JPanel implements ActionListener {
//static CardLayout cardLayout = new CardLayout();
public Window2() {
init();
}
private void init() {
setLayout(new BorderLayout());
JLabel textLabel = new JLabel("Window2");
setBackground(Color.RED);
add(textLabel, BorderLayout.CENTER);
JButton nextButton = new JButton("NEXT");
nextButton.setActionCommand("next");
nextButton.addActionListener(this);
add(nextButton, BorderLayout.EAST);
//setLayout(cardLayout);
//JPanel jp3 = new Window3();
//add(jp3, "string");
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("next")) {
// go to the next window??
System.out.println("window2");
Window1.cardLayout.next(this);
}
}
}
最后一个:
public class Window3 extends JPanel implements ActionListener {
public Window3() {
init();
}
private void init() {
setLayout(new BorderLayout());
JLabel textLabel = new JLabel("Window3");
setBackground(Color.BLUE);
add(textLabel, BorderLayout.CENTER);
JButton nextButton = new JButton("NEXT");
nextButton.setActionCommand("next");
nextButton.addActionListener(this);
add(nextButton, BorderLayout.EAST);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("next")) {
// go to the next window
// Window1.cardLayout.next(this);
}
}
}
【问题讨论】:
-
退出多发问题:stackoverflow.com/questions/9322474/…。您已经从 Swing 教程中获得了一个工作示例,并且已经解释了如何将面板与同一类分开,那么您为什么要重新发布与之前相同的代码?您没有听取您上次发布的任何建议。
-
只是因为我仍然无法让它工作。我想,当我得到
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: wrong parent for CardLayout时,问题仍然在于将它连接在一起 -
正如我所说,你没有发布你的 SSCCE 来展示你的尝试。所以重新发布这个问题并没有给我们任何新的信息。这只会浪费我们的时间。
-
我还在学习,对不起...我认为转发的问题比上一个问题更像 SSCCE。另外,我觉得我快到了,只需要了解类之间的链接应该是什么样子。
-
我建议您在上一个问题中应该如何进行。您从可行的东西开始(教程演示),然后从主类中删除“面板代码”以创建单独的源/类文件。我什至给了你使用的代码。您发布的代码与演示代码或我建议的代码完全不同。 SSCCE 的重点是从简单理解概念开始。一旦你理解了这个概念,你就可以将知识应用到你的实际应用中。
标签: java swing user-interface jpanel cardlayout