0

I'm a beginner with Graphic User Interface and I'm working on a a game project. The class I'm writing GameMenu is a JComponent and the purpose of it is that within my game I will be making a GameMenu object and it will be handling the loading of the game, just a visual representation. However, instead of using a layout manager I decided to do the GameMenu loading with absolute positioning in which I have a ProgressBar and a Frame together. I set the size of the Frame to be 500 by 500 and the ProgressBar also to be the dimension of 500 by 500, what I have thought the result to be is that the Frame and ProgressBar should be the same size, but when I run the code the ProgressBar appears to be a little bigger than my Frame. Thus I have to resize the Frame in order to see the entire ProgressBar. I don't know whether my code is wrong or is it that ProgressBar by nature appears to be a little bigger in size if you positioning it by yourself. How do I set the Frame and ProgressBar to be the same size without resizing?

public class GameMenu extends JComponent
{
    private JProgressBar loadbar;
    private JFrame gui;

    public GameMenu()
    {
        loadbar=new JProgressBar();

        gui=new JFrame();
    }
    public void init()
    {
        gui.setLayout(null);

        loadbar.setStringPainted(true);

        gui.setBounds(0,0,500,500);

        loadbar.setBounds(0,0,500,500);

        loadbar.setVisible(true);

        gui.getContentPane().add(loadbar);

        gui.setVisible(true);

        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        gui.setLocationRelativeTo(null);


        for(int i=0;i<=100;i++)
        {
            loadbar.setValue(i);

            try
            {
                Thread.sleep(15);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    One problem is that you're forgetting about the decorations that the JFrame holds, that also takes up size. While null layouts and `setBounds()` might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a very difficult to enhance or maintain, they fail completely when placed in scrollpanes, they look awful when viewed on all platforms or screen resolutions that are different from the original one. – Hovercraft Full Of Eels Apr 22 '18 at 22:52
  • 1
    So learn and use the layout managers. Here if you want a JProgressBar to fill a JFrame, then simply add it to the JFrame, since it will be then added to the JFrame's contentPane in the BorderLayout.CENTER position and will fill the JFrame. Side note: you really don't want to use a JFrame here but rather a dialog window such as a JDialog. – Hovercraft Full Of Eels Apr 22 '18 at 22:53
  • Alright thank you I will try out the layout manager and JDialog, but with using layout manager is it still possible to overlap two components together? – FrozenToxic Apr 22 '18 at 23:12
  • 1
    Yes, you can do this with a JLayeredPane or other layouts, but often when this is asked, it's in truth an [XY Problem](http://xyproblem.info/). – Hovercraft Full Of Eels Apr 22 '18 at 23:33
  • Alright thank you! – FrozenToxic Apr 22 '18 at 23:47

0 Answers0