简体   繁体   中英

How can I add new jpanel on already existing in this example?

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM