GridBagLayout in CardLayout....Problems

Discussion in 'Java' started by domenik.brugger@googlemail.com, Sep 24, 2012.

  1. Guest

    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);
    }
    });
    }
    }
    
    
    I have no idea what to do and hope you can help me.
    Thanks.
    , Sep 24, 2012
    #1
    1. Advertising

  2. markspace Guest

    On 9/23/2012 4:49 PM, wrote:

    > I have no idea what to do and hope you can help me.


    I have no idea what to do either:

    >
    > My Code:
    >
    Code:
    > package EP.GUI;
    > import static EP.UTIL.Sprache_DE.stRegeln;[/color]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^[color=blue]
    >
    > public class CardLayoutExample1 extends MyGridBagContraints{[/color]
    ^^^^^^^^^^^^^^^^^^^
    
    
    This doesn't help us.  Do you really think we can guess what is
    happening when there are several methods in your code that you didn't
    show us?  Please post something compiles, i.e., an SSCCE:
    
    http://sscce.org/
    markspace, Sep 24, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lee Fesperman

    CardLayout on JRE 1.4.2

    Lee Fesperman, Jul 4, 2003, in forum: Java
    Replies:
    0
    Views:
    363
    Lee Fesperman
    Jul 4, 2003
  2. BG

    cardlayout problem

    BG, Jul 30, 2003, in forum: Java
    Replies:
    0
    Views:
    386
  3. AJ
    Replies:
    2
    Views:
    327
    Wilhelm Stockhammer
    Jan 10, 2004
  4. steve

    problems with a cardlayout.

    steve, Nov 21, 2004, in forum: Java
    Replies:
    1
    Views:
    354
    steve
    Nov 21, 2004
  5. roadkill
    Replies:
    7
    Views:
    617
    roadkill
    Jan 9, 2005
Loading...

Share This Page