简体   繁体   中英

Adding JPanel to JFrame?

I'm trying to add a JLabel to a JPanel to a JFrame. I set the border for the JPanel, but all I see on the JFrame is a small black square in the center of my frame. Whatever I do I can't change the size or location of it. Please help.

    Start main = new Start();
    Random random = new Random();

    JFrame mainFrame = new JFrame("MainFrame");
    JPanel mainPanel = new JPanel();
    JLabel welcomeLabel = new JLabel();

    mainFrame.add(main);
    mainFrame.setLayout(new GridBagLayout());
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.setTitle(names[random.nextInt(names.length)]);
    mainFrame.pack();
    mainFrame.setVisible(true);
    mainFrame.setSize(mainFrameX, mainFrameY);
    mainFrame.setResizable(false);
    mainFrame.setLocationRelativeTo(null);
    mainFrame.add(mainPanel);

    mainPanel.add(welcomeLabel);
    mainPanel.setBorder(new LineBorder(Color.BLACK));
    mainPanel.setSize(new Dimension(200, 200));

    welcomeLabel.setFont(new Font("Verdana", 1, 20));
    welcomeLabel.setLocation(100, 100);

    main.start();

Suggestions:

  • You will want to read the tutorial, Laying out Components , as it will explain how to code with the Swing layout managers, and this information is essential to solve your current problem.
  • One caveat: I urge you to avoid the temptation to use the null layout as use of it will lead to creation of code that is very hard to maintain or upgrade.
  • Your JLabel, welcomeLabel, will of course need some text to be visible.
  • Don't set it's location via setLocation(...) but again use the layout managers to do the dirty work of placing and sizing your components.
  • You will also want to call pack() and setVisible(true) on your JFrame after adding all initial components.

Hovercraft is right (+1), make sure you understand how the layout managers are working.

The order in which you do things are important, especially when dealing with the top level containers...

Start main = new Start();
Random random = new Random();

JFrame mainFrame = new JFrame("MainFrame");
JPanel mainPanel = new JPanel();
JLabel welcomeLabel = new JLabel();    

welcomeLabel.setFont(new Font("Verdana", 1, 20));

mainPanel.add(welcomeLabel);
mainPanel.setBorder(new LineBorder(Color.BLACK));

// Do this first
mainFrame.setLayout(new GridBagLayout());
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setTitle(names[random.nextInt(names.length)]);

// Add your components
mainFrame.add(main);
mainFrame.add(mainPanel);

// Prepare the window for showing, now you have some content.
mainFrame.setResizable(false);
mainFrame.pack();
mainFrame.setVisible(true);
mainFrame.setLocationRelativeTo(null);

main.start();

This will still only produce a small black square in the window, because the JLabel has no content and therefore it's preferred size is going to be (something like) 2x2 (because of the border).

Try adding some text to...

welcomeLabel.setText("Welcome");

And then see the difference

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