我在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