0

I've this Actionlistener that change my previous panel into a second one, now I want to put a "Back" button in panel2 that take me back to panel1 maintaining its graphic setting.

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JFrame {

private static final long serialVersionUID = 1L;
    public static void main(String[] args){
        final JFrame Main = new JFrame("TEST");
        Main.setVisible(true);
        Main.setSize(600, 600);
        Main.setLocationRelativeTo(null);
        Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    

//Adding JPanel     
        JPanel panel = new JPanel();
        Main.add(panel);

//JPanel settings
        panel.setLayout(null);
        panel.setBackground(Color.GREEN);

//Adding JButton
        JButton button = new JButton("Button 1");
        JButton button2 = new JButton("Button2");
        panel.add(button);
        panel.add(button2);

//JButton settings
        button.setBounds(70, 160, 200, 200);
        button2.setBounds(320, 160, 200, 200);

//Button action
button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {

        JPanel panel2 = new JPanel();

        //Panel2 settings
        JButton button3 = new JButton("Back");
        panel2.add(button3);
        panel2.setBackground(Color.RED);

        Main.getContentPane().removeAll();
        Main.getContentPane().add(panel2);
        Main.getContentPane().validate();

    }
});

//Button action
button2.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {

        JPanel panel3 = new JPanel();

        //Panel2 settings
        JButton button3 = new JButton("Back");
        panel3.add(button3);
        panel3.setBackground(Color.YELLOW);

        //Button action
        button3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {

                JPanel panel = new JPanel();

                Main.getContentPane().removeAll();
                Main.getContentPane().add(panel);
                Main.getContentPane().validate();

                    }

                });

        Main.getContentPane().removeAll();
        Main.getContentPane().add(panel3);
        Main.getContentPane().validate();

            }

        });

    }
}
user2592584
  • 29
  • 1
  • 1
  • 9
  • 3
    1) You've described a problem, but have so far not asked a question (let alone a specific, answerable question). What *is* your question? 2) For many components in one space, use a [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) as seen in this [short example](http://stackoverflow.com/a/5786005/418556). 3) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jul 22 '13 at 14:37
  • 1
    dont fully understand but take a look at the Command pattern. It might help you implements back and forward commands – RNJ Jul 22 '13 at 14:41
  • What do you mean by __"maintaining its graphics settings"__ ? You are not keeping the previous reference of your `JPanel` intact, instead you reinitializing the previous reference, you already have, with a new object using `JPanel panel = new JPanel();`. Hence its state is bound to change. `CardLayout` is exactly what you need for this situation, as already suggested. One more [example](http://stackoverflow.com/a/9349137/1057230), for your help :-) – nIcE cOw Jul 22 '13 at 16:19
  • Related [example](http://stackoverflow.com/a/13110320/1057230), for what you asking for :-), though I would still strongly urge you to have a look at `CardLayout` :-) – nIcE cOw Jul 22 '13 at 16:29
  • i can't use cardlayout cause i want to move my buttons and made the graphic manually – user2592584 Jul 22 '13 at 16:40
  • "maintaining its graphics settings": i mean that in my code i put a green background and two buttons into my first panel, but, when i'm in the panel2 and click the "back" button, the first panel appear blank. – user2592584 Jul 22 '13 at 16:43
  • i will post all my code ;) – user2592584 Jul 22 '13 at 17:47
  • Thanks guys, i'm trying to learn Java for the past five days, so i'm a noob ;-) i'm going to check the tutorials – user2592584 Jul 23 '13 at 07:30

0 Answers0