3つのクラスに3つのウィンドウがあり、次のボタンをクリックすると次のウィンドウが表示されるようにcardLayoutを使いたいと思います。 1つのcardLayoutに異なる要素を含むJPanelを追加するにはどうすればよいですか?これは最初のウィンドウです:(唯一の違いは、しかし、背景である - しかし、それは私が実際にそれを得た方法のアイデアを表します)他のクラスから他のクラスの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);
}
}
}
そして最後に1:
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
私はまだそれを働かせることができないので単純に。それでも、問題をスレッド間で結びつけるのは、AWT-EventQueue-0「スレッド例外」です。java.lang.IllegalArgumentException:CardLayoutの親が間違っています。 – Hurdler
あなたのSSCCEにあなたの試みる。だから、質問を再投稿しても、私たちに新しい情報はありません。それは私たちの時間を無駄にするだけです。 – camickr