-1

Stupid question, but I cant seem to find answer, and when I do it doesn't work. So i want to add new JPanel on already existing JPanel. Sometimes when i add it it just opens a new window when I run it, other times nothing happens. Anyways here is the code:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Main extends JFrame {

private static final long serialVersionUID = 1L;

public static void main(String[] args)
{
    new Main().setVisible(true);
}

private Main()
{
    super("Vending machine");

    JPanel p = new JPanel();

      JLabel title = new JLabel("Vending machine: ");
      JButton button1 = new JButton("1");
      JButton button2 = new JButton("2");
      JButton button3 = new JButton("5");
      JLabel label1 = new JLabel("Enter code: ");
      JTextField text1= new JTextField(3);
      JButton ok= new JButton("OK");
      JButton button4 = new JButton("Return change");
      JLabel label2 = new JLabel("Result is: ");
      JTextField text2= new JTextField(3);
      JLabel label3 = new JLabel("Current: ");
      JTextField text3= new JTextField(3);

      title.setBounds(200,5,250,80);
      title.setFont (title.getFont ().deriveFont (22.0f));
      p.add(title);
      p.setLayout(null);

      button1.setBounds(530,46,120,60);
      p.add(button1);
      button2.setBounds(530,172,120,60);
      p.add(button2);
      button3.setBounds(530,298,120,60);
      p.add(button3);
      label1.setBounds(555,414,120,60);
      p.add(label1);
      text1.setBounds(530,454,120,30);
      p.add(text1);
      ok.setBounds(530,550,120,60);
      p.add(ok);
      button4.setBounds(360,550,120,60);
      p.add(button4);
      label2.setBounds(230,530,120,60);
      p.add(label2);
      text2.setBounds(200,575,120,30);
      p.add(text2);
      label3.setBounds(50,530,120,60);
      p.add(label3);
      text3.setBounds(38,575,120,30);
      p.add(text3);



      getContentPane().add(p);
      setSize(700,700);
      setVisible(true); 


}
}

I want to add new JPanel on this place: vending machine:

https://i.stack.imgur.com/nkrpp.png

Thank you!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Daffyo
  • 13
  • 3
  • 1
    Possibly relevant: [Why is it frowned upon to use a null layout in Swing?](https://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) – Joe C Nov 17 '18 at 14:44
  • 2
    `i want to add new JPanel on already existing JPanel` - where is the code that attempts to do this? I see you adding buttons, labels etc to the panel. I don't see where you add another panel to the panel. – camickr Nov 17 '18 at 16:26

1 Answers1

3

Even if you could do this, it will give you harm for every time you want another changes on this frame.

Instead of locating a JPanel into another JPanel, use layouts.

You shouldnt use static variables and a null layout.

Use appropriate layout managers. Maybe the main panel uses a BorderLayout. Then you add your main component to the CENTER and a second panel to the EAST. The second panel can also use a BorderLayout. You can then add the two components to the NORTH, CENTER or SOUTH as you require.

  • 1
    (1+). Yes, you should be using layout managers, not a null layout. Read the Swing tutorial on [Layout Managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) for working examples. – camickr Nov 17 '18 at 16:28
  • 1
    Thank you @camickr I'm new here. I've just found out that I need to share references for every answer, you've completed my answer. Thank you ! – somethingunusual Nov 17 '18 at 18:10