GridBagLayout in CardLayout....Problems
Hello,
i have a probleme with the cardLayout.
i have a jpanel with cardlayout and two cards in it with GridBagLayout.
In each card is a JButton. At a click there should be shown the next card with the other button.
But the buttons will be shown together when i start the program.
My Code:
[code]
package EP.GUI;
import static EP.UTIL.Sprache_DE.stRegeln;
import static java.awt.GridBagConstraints.BOTH;
import static java.awt.GridBagConstraints.LINE_START;
import static java.awt.GridBagConstraints.REMAINDER;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutExample1 extends MyGridBagContraints{
private CardLayout cardLayout;
public JButton button1, button2;
private JPanel panel1, panel2, jPanelEigen, jPanelGridBag;
public CardLayoutExample1(){
super();
this.setSize(500,500);
this.setLayout(new GridBagLayout());
this.setVisible(true);
this.setLocation(50,50);
this.setTitle(stRegeln);
this.addWindowListener(new WindowListener() {
public void windowOpened(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public void windowClosing(WindowEvent arg0) {
}
public void windowClosed(WindowEvent arg0) {}
public void windowActivated(WindowEvent arg0) {}
});
cardLayout = new CardLayout();
jPanelEigen = new JPanel(cardLayout);
panel1 = new JPanel(new GridBagLayout());
panel2 = new JPanel(new GridBagLayout());
button1 = new JButton("Button1");
button2 = new JButton("Button2");
jPanelEigen.add(getPanel1(), "card1");
jPanelEigen.add(getPanel2(), "card2");
cardLayout.show(jPanelEigen, "card1");
this.add(jPanelEigen);
pack();
createListener();
setVisible(true);
}
private JPanel getPanel1(){
panel1.setBackground(Color.gray);
setDefaultGB();
addGB(button1, gridx, gridy,
gridwidth = REMAINDER, gridheight, fill,
weightx, weighty, anchor,
insets);
return panel1;
}
private JPanel getPanel2(){
panel2.setBackground(Color.yellow);
setDefaultGB();
addGB(button2, gridx, gridy,
gridwidth = REMAINDER, gridheight, fill,
weightx, weighty, anchor,
insets);
return panel2;
}
private void createListener(){
ActionListener al = new ActionListener(){
public void actionPerformed(ActionEvent e){
cardLayout.next(jPanelEigen);
}
};
button1.addActionListener(al);
button2.addActionListener(al);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
[/code]
I have no idea what to do and hope you can help me.
Thanks.