0

I have tried in many ways to add an internal frame to my existing one. I have tried with and without JPanel. But nothing worked and I don´t have a clue why. Anyone?

public class Menu_new extends JFrame{


private BufferedImage background = null;


public Menu_new() {

    try {
        background = ImageIO.read(new File("pics_1/hallo.jpg"));
    } catch (IOException e) {
        e.printStackTrace();
    }

    JDesktopPane desktop = new JDesktopPane();
    JInternalFrame inside = new JInternalFrame(("Data"), true, true, true, true);
    desktop.add(inside);



    inside.setBounds(50, 50, 300, 500);
    inside.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    inside.setVisible(true);
    JLabel label = new JLabel("Your name");
    inside.add(label);

    JTextField text = new JTextField(10);
    inside.add(text);
    Icon icon = new ImageIcon("pics_1/Button.png");
    JButton start = new JButton(icon);
    start.setBorder(BorderFactory.createEmptyBorder());

    inside.add(start);

    inside.moveToFront();
    this.add(desktop);
 }


 public void paintComponent(Graphics g) {

        g.drawImage(background, 0, 0, this);
 }

}
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43

1 Answers1

1

Your method of adding the components to the internal frame is wrong.

The default layout manager (for internal frame as well as for JFrame) is BorderLayout and therefore requires that you specify where you want to place your components. (As a special case if you only add a single component it seems to work without specifying a constraint).

Your code to add the components should look like this:

inside.add(label, BorderLayout.PAGE_START);
...
inside.add(text, BorderLayout.CENTER);
...
inside.add(start, BorderLayout.PAGE_END);

As an additional note, this inside.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); has no effect, since JFrame.EXIT_ON_CLOSE is not valid for internal frames.

Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34