JFrame application has gaps

Asked 2 years ago, Updated 2 years ago, 130 views

I'm using JFrame to create a 2D game in Java, but when I start it, sometimes there is a mysterious gap between the right end and the bottom as shown in the picture.

Tinkering with Layout didn't really work.

Depending on the start-up time, there are times when there is a gap and times.What should I do to keep this gap from coming out?Please let me know if anyone knows.
There is a gap between the right and the bottom

add

The operating system is Windows 7 The Java version is "1.8.0_91"
The same symptom was observed in the source code below.

public class Test extensions JFrame {
    private static final long serialVersionUID = 1L;

    public Test(){
        Container cnt=getContentPane();
        MainPanel panel = new MainPanel();
        setResizable (false);
        cnt.add(panel);
        pack();
    }

    public static void main(String args[]){
        Test test = new Test();
        test.setVisible(true);
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

public class MainPanel extensions JPanel {
    private static final long serialVersionUID = 1L;
    private static final int WIDTH = 500;
    private static final int HEIGHT=500;

    publicMainPanel(){
        setPreferredSize (new Dimension (WIDTH, HEIGHT));
    }

    public void paintComponent (Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.blue);
        g.fillRect(0,0,WIDTH,HEIGHT);
    }
}

source code below

java swing

2022-09-30 10:32

1 Answers

Ignoring the rule of calling the method of the Swing object in the event dispatch thread (EDT), as described in the Swing tutorial below, sometimes causes strange behavior (JFrame size, misalignment, etc.).

Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

The Event Dispatch Thread

Swing event handling code runs on a special thread known as the event dispatch thread. Most code that invokes Swing methods also runs on this thread. This is necessary because most Swing object methods are not "thread safe": invoking them from multiple threads risks thread interference or memory consistency errors. Some Swing component methods are labelled "thread safe" in the API specification; these can be safety invoked from any thread. All other Swing component methods must be invoked from the event dispatch thread.Programs that ignore this rule may function correctly most of the time, but are subject to unreliable errors that are different to repeat.

This is likely to be the cause of this phenomenon, so you can use http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html"rel="nofollow">SwingUtilities#invokeLater(Runnable) in etc.I think I can avoid it by scheduling a task to call the Swing method, such as e#pack() or JFrame#setVisible(), and running it within EDT.

import java.awt.*;
import javax.swing.*;

public class Test2 {
  public static void main(String[]args) {
    SwingUtilities.invokeLater(newRunnable(){
      @ Override
      public void run() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.getContentPane().add(new MainPanel());
        f.setResizable (false);
        f.pack();
        f.setVisible(true);
      }
    });
  }
}

class MainPanel extensions JPanel {
  private static final long serialVersionUID = 1L;
  private static final int WIDTH = 500;
  private static final int HEIGHT=500;

  publicMainPanel(){
    super();
    // setPreferredSize (new Dimension (WIDTH, HEIGHT));
  }
  @ Override
  public Dimension getPreferredSize(){
    return new Dimension (WIDTH, HEIGHT);
  }
  @ Override
  public void paintComponent (Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.blue);
    g.fillRect(0,0,WIDTH,HEIGHT);
  }
}


2022-09-30 10:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.