如何摆脱Jframe底部和右侧的白色1PX线?

发布于 2025-01-21 20:53:11 字数 1595 浏览 2 评论 0原文

我想要一个jpanel,该目前是黑色的,但会在jframe内更改颜色。我只设置了面板的大小,然后在我的框架上调用.pack,但是我框架的底部和右侧出现了一条丑陋的白色1PX线。为什么那是这样,我该如何修复? 我目前正在运行Windows 11。

import javax.swing.JFrame;

public class Main {
    
    private static GamePanel gamePanel;

    public static void main(String[] args) {
        
        createWindow();
        
        gamePanel = new GamePanel();
        window.add(gamePanel);
        
        window.pack();
        window.setLocationRelativeTo(null);
        window.setVisible(true);
        
    }
    
    private static JFrame window;
    
    private static void createWindow() {
        window = new JFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setTitle("My Game");
        window.setResizable(false);
    }
    
}
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;

public class GamePanel extends JPanel {
    
    // Screen settings
    final int originalTileSize = 16;
    final int scaleFactor = 3;
    
    final int tileSize = originalTileSize * scaleFactor;
    final int maxScreenCol = 16;
    final int maxScreenRow = 12;
    final int screenWidth = maxScreenCol * tileSize;
    final int screenHeight = maxScreenRow * tileSize;
    
    // --- Constructor ---
    GamePanel () {
        this.setPreferredSize(new Dimension(screenWidth, screenHeight));
        this.setBackground(Color.black);
        this.setDoubleBuffered(true);
    }
    
    public int getScreenWidth () {
        return screenWidth;
    }
}

I want a JPanel which is currently black but will change color, inside of a JFrame. I only set the size of the panel and then call .pack on my frame, but an ugly white 1px line appears on the bottom and right side of my frame. Why is that and how can I fix it?
I am currently running Windows 11.

import javax.swing.JFrame;

public class Main {
    
    private static GamePanel gamePanel;

    public static void main(String[] args) {
        
        createWindow();
        
        gamePanel = new GamePanel();
        window.add(gamePanel);
        
        window.pack();
        window.setLocationRelativeTo(null);
        window.setVisible(true);
        
    }
    
    private static JFrame window;
    
    private static void createWindow() {
        window = new JFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setTitle("My Game");
        window.setResizable(false);
    }
    
}
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;

public class GamePanel extends JPanel {
    
    // Screen settings
    final int originalTileSize = 16;
    final int scaleFactor = 3;
    
    final int tileSize = originalTileSize * scaleFactor;
    final int maxScreenCol = 16;
    final int maxScreenRow = 12;
    final int screenWidth = maxScreenCol * tileSize;
    final int screenHeight = maxScreenRow * tileSize;
    
    // --- Constructor ---
    GamePanel () {
        this.setPreferredSize(new Dimension(screenWidth, screenHeight));
        this.setBackground(Color.black);
        this.setDoubleBuffered(true);
    }
    
    public int getScreenWidth () {
        return screenWidth;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文