0

I have a JFrame with a JLabel on top called "coloredLabel", an instance of the class it's in is running on both of them. a random amount of objects move around on the frame and label and don't directly interact with them. The only problem is that there is a bit of the frame visable above the label, what I want is that the label fully alligns with the frame, without pasting over the objects (which are painted in with an override paint method and mentioned as "game.newBall" and "game.moveBall". "test" is the name of the class. Here is how my main thread looks, the frame and the label are declared within it:

public static void main(String[] args) throws InterruptedException {
    JFrame frame = new JFrame("Bounce 2.0");
    JLabel coloredLabel = new JLabel("");
    test game = new test();
    frame.add(game);
    frame.setSize(300, 400);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    coloredLabel.setOpaque(true);
    coloredLabel.setBackground(game.backgroundColor);
    coloredLabel.setPreferredSize(new Dimension(1000000,1000000));
    frame.getContentPane().add(coloredLabel,BorderLayout.LINE_START);
    game.add(coloredLabel);

    for(int a = randInt(0,9); a<10; a++)
        game.newBall(randInt(0,300),randInt(0,400));

    while (true) {
        double height = frame.getContentPane().getSize().getHeight();
        double width = frame.getContentPane().getSize().getWidth();
        int n = 0;
        while(game.exists(n)==true){
            game.moveBall(n,width,height);
            n++;
        }
        game.repaint();
        Thread.sleep(10);
    }
}

So my question is: How do I allign the JLabel with the JFrame? so there is no space in between the JLabel and the frame.

I searched for this on this site, but couldn't find the same problem or something similar enough so I could fix this.

  • solved - game.setBackground(...);
  • Could you reproduce this problem with just a `JLabel`, and a `JFrame`? – matt May 26 '17 at 15:30
  • 2
    1) *"which are painted in with an override paint method"* You should override `paintComponent(...)`, not `paint(...)`. 2) `while(true) {` that line's going to harm you more than help you along with `Thread.sleep(10);`, instead use a [Swing Timer](https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) or you could be blocking the EDT (which you could because you haven't initialized your application correctly) (do it as shown at the end of this [answer](https://stackoverflow.com/questions/41559357/weird-white-space-in-a-java-jframe/41559787#41559787)) – Frakcool May 26 '17 at 15:31
  • 3) You're making your `JFrame` visible much before you've finished adding all your elements to it. It could cause weird behaviors too, this line `frame.setVisible(true);` should be the last one in your program. 4) Why in the world would you create such big `JLabel`??? `coloredLabel.setPreferredSize(new Dimension(1000000,1000000));` 5) Related to the last point, see [Should I avoid the use of setPreferred|Maximum|MinimumSize()?](https://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) the general consensus says yes... – Frakcool May 26 '17 at 15:35
  • ...and to override `getPreferred|Maximum|MinimumSize()` methods instead. 6) Why calculcate this variables everytime? Why not have them as member variables? `double height = frame.getContentPane().getSize().getHeight(); double width = frame.getContentPane().getSize().getWidth();`, 7) This `while(game.exists(n)==true){` could be simplified as: `while(game.exists(n))`. For better help sooner post a valid [mcve] that demonstrates your problem. – Frakcool May 26 '17 at 15:37
  • And you're adding your `coloredLabel` to 2 places: `frame.getContentPane().add(coloredLabel,BorderLayout.LINE_START);` and `game.add(coloredLabel);` which is not correct, Swing will add it only to the last one added, and thus, the 1st line is not needed then... – Frakcool May 26 '17 at 15:42
  • I will implement your tips in my program, thx. – Pascal Anema May 26 '17 at 16:43
  • 6) because the objects interact with the frame border, when the user changes the size of the frame, the program must know the new size of the frame – Pascal Anema May 26 '17 at 16:47
  • 3) because if I put it later on, I get an ugly white flash when I change the size of the frame – Pascal Anema May 26 '17 at 16:55

1 Answers1

3

The only problem is that there is a bit of the frame visable above the label,

game.add(coloredLabel);

I'm guessing "game" is a JPanel. By default a JPanel uses a FlowLayout and by default the FlowLayout has horizontal and vertical gaps of 5 pixels.

Get rid of the gap in the FlowLayout. Read the API for the constructors/methods of the FlowLayout to customize its behaviour.

But of course the bigger issue is the design of your app. I don't understand your point of using the label and attempting to take up all the space of the frame. Just set the background of the game panel by using:

game.setBackground(...);

Also class names should:

  1. start with an upper case character and
  2. be descriptive.

"test" is neither.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • test is the name of the class, just for testing, it's Original name is "Bounce2", and game.setBackground(...); worked, thanks! – Pascal Anema May 26 '17 at 16:30