-1

I'm making a GUI, which allows me to choose one of few options, which have more options(buttons). I'm inexperienced at Java and have some problem with it.

Why this.dispose() doesn't open new JPanel after clicking Button11? How I can open new JPanel exactly Panel11? Could you give me some advice?

class Panel1 extends JPanel
{
private Frame frame;
private JButton backButton;
private JButton Button11;
private JButton Button12;
private JButton Button13;

public Panel1(Frame f)
{
frame=f;

setPreferredSize(new Dimension(500,500));
backButton=new JButton("Back");
Button11=new JButton("Z warunkami atmosferycznymi");
Button12=new JButton("Z kontrolą ruchu lotniczego");
Button13=new JButton("Z uszkodzeniem/awarią");

add(new JLabel("Podczas lotu"));
add(backButton);
add(Button11);
add(Button12);
add(Button13);

backButton.addActionListener( new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e){
    frame.back(); }
                              }
);

Button11.addActionListener( new ActionListener () {
    public void actionPerformed(ActionEvent e) {
    Panel11 panel = new Panel11();
    panel.setVisible(true);
    panel.setLocationRelativeTo(null);
    this.dispose();
       }
}
);
    }
}

class Panel11 extends JPanel
{

private Frame frame;
private JButton backButton;

public Panel11(Frame f)
{
frame=f;

setPreferredSize(new Dimension(200,500));
backButton=new JButton("Back");
add(new JLabel("Podczas rotacji"));
add(backButton);
backButton.addActionListener( new ActionListener()
                              {
                                @Override
                              public void actionPerformed(ActionEvent e)
                                {
                                  frame.back();
                                }
                              }
 );  
}
}

class Frame extends JFrame
{

private JPanel main;
private JButton button1;
private Panel1 panel1;


public Frame()
{
super("Algorytm przydziału kodów opóźnień");

main=new JPanel();
main.setPreferredSize(new Dimension(400,200));
panel1=new Panel1(this);

button1=new JButton("Podczas lotu");
button1.addActionListener(new ActionListener()
                          {
                            @Override
                            public void actionPerformed(ActionEvent e)
                            {
                              update(panel1);
                            }
                          }
);

main.add(button1);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(main);
pack();
setResizable(true);
setVisible(true);
}

public void back()
{
update(main);
}

private void update(JPanel panel)
{
remove(panel1);
remove(main);
add(panel);
pack();
panel.setVisible(true);
setVisible(true);
}
}


public class Main
{
public static void main(String[] args)
{
new Frame();
}
}    
Szymsss
  • 7
  • 1
  • please clarify what you want to happen: do you want that when you click button11 then panel 11 will show up in the Jframe? – SteelToe Dec 23 '16 at 18:47
  • @MarrieteCowby12 Yes – Szymsss Dec 23 '16 at 18:49
  • Java **is not** Javascript! Use the tags correctly!, Then please indent your code correctly and post a valid [mcve], (i.e. Not your whole code, but a simple example) which demonstrates your issue and as short as possible so we can copy-paste it and see it in our pc – Frakcool Dec 23 '16 at 19:01
  • 1
    Please don't revert the changes, you're using Java, not Javascript! You still forgot to indent your code correctly – Frakcool Dec 23 '16 at 19:12

1 Answers1

2

Your error is because you're calling:

Panel11 panel = new Panel11();

Now, let's see the constructor(s) of Panel11:

public Panel11(Frame f)
public Panel11()

Do you find any difference? Yes I know, you never wrote public Panel11(), but it's there by default, so, you're calling it and it's empty, so, you need to pass a Frame reference to it, also make sure to change that to JFrame instead of Frame, because Frame belongs to AWT while JFrame belongs to Swing.

You would have spotted that with proper code indenting, next thing is, you're using multiple JFrames, see The use of multiple JFrames, Good / Bad practice? (BAD!), instead you might want to use a Card Layout or Dialogs

If that doesn't help, please post a valid Minimal, Complete and Verfiable Example

Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89