我在3个独立的类中有3个窗口,我想使用cardLayout,以便当您单击下一个按钮时,将出现下一个窗口。如何将包含不同元素的JPanel添加到一个cardLayout?这是第一个窗口:(唯一的区别是背景虽然 - 但是它代表了我是如何得到它实际的想法)将其他类的JPanels添加到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);
}
}
}
退出multiposting问题:http://stackoverflow.com/questions/9322474/adding-jpanels-from-other-classes-to-the-cardlayout。你已经从Swing教程中得到了一个有效的例子,并且已经解释了如何将面板与同一个类分开,那么为什么你会重新发布与之前相同的代码?你没有听过你上一篇文章的任何建议。 – camickr 2012-02-17 04:43:45
只是因为我仍然无法得到它的工作。还有一个问题在于将它连接在一起,我猜,因为我得到'线程中的异常“AWT-EventQueue-0”java.lang.IllegalArgumentException:CardLayout的父级错误“ – Hurdler 2012-02-17 11:34:30
正如我所说,你没有发布你的SSCCE显示你的尝试。所以重新发布这个问题并没有给我们任何新的信息。它只会浪费我们的时间。 – camickr 2012-02-17 15:48:52