JPanel 底部奇怪的白色边框 [英] Weird white border at bottom of JPanel

查看:27
本文介绍了JPanel 底部奇怪的白色边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的屏幕底部有一个奇怪的白色边框.它在左下角,也就是 TimeView.java

I have this weird white border at the bottom of my screen. It's on the bottom left corner, which is the TimeView.java

这是我的 ContentPane.java

This is my ContentPane.java

package views;

import java.awt.BorderLayout;

import javax.swing.BoxLayout;
import javax.swing.JPanel;

public class ContentPane extends JPanel {

private static final long serialVersionUID = 1L;

private GameView gameView;
private PlayView playView;
private TimeView timeView;

public ContentPane() {      
    this.setLayout(new BorderLayout());
}

public void setGameView(GameView gameView, PlayView playView, TimeView timeView) {
    this.gameView = gameView;
    this.playView = playView;
    this.timeView = timeView;

    JPanel subPanel = new JPanel();
    subPanel.setLayout(new BoxLayout(subPanel, BoxLayout.Y_AXIS));
    subPanel.add(gameView);
    subPanel.add(timeView);

    this.add(playView, BorderLayout.CENTER);
    this.add(subPanel, BorderLayout.WEST);
}

}

这是 GameView.java

This is GameView.java

package views;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

public class GameView extends JPanel {

private static final long serialVersionUID = 1L;

public GameView() {
    this.setBackground(Color.decode("#2A2828"));
    this.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, Color.GRAY));
    this.setPreferredSize(new Dimension(200, 300));
}

}

这是 TimeView.java

This is TimeView.java

package views;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class TimeView extends JPanel {

private static final long serialVersionUID = 1L;

public TimeView() {
    this.setBackground(Color.decode("#2A2828"));
    this.setPreferredSize(new Dimension(200, 500));
}
}

这是 PlayView.java

And this is PlayView.java

package views;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JPanel;

public class PlayView extends JPanel {

private static final long serialVersionUID = 1L;

public PlayView() {
    this.setBackground(Color.decode("#1F1F40"));
    this.setPreferredSize(new Dimension(1100, 800));
}

}

我已经尝试在 TimeView 上放置一个空边框,但仍然出现奇怪的白色边框.

I've already tried putting an empty border on the TimeView but still,​ I'm getting the weird white border.

如果有人能向我解释导致这个问题的原因,那就太好了.

If anyone could explain to me what causes this problem that'd be great.

这是 MainFrame.java

This is MainFrame.java

package views;

import javax.swing.JFrame;

public class MainFrame extends JFrame {

private static final long serialVersionUID = 1L;

public void setupGui(ContentPane contentPane) {
    this.setTitle("TagMan By Jesse");
    this.setContentPane(contentPane);
    this.setSize(1300, 800);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);
    this.setResizable(false);
}

}

推荐答案

在许多操作系统上,使窗口不可调整大小会更改框架装饰,从而影响可用的内容空间.不过,这往往不会使容器失效,这可能会导致少量额外空间未使用.

On many OS's making a window not resizable changes the frame decorations, which affects the available content space. This tends to NOT invalidate the container though, which can result in a small amount of additional space been unused.

您应该在设置大小或使窗口可见之前调用 setResizable.

You should call setResizable BEFORE setting the size or making the window visible.

另外,因为可用"space 是窗口大小".减去框架装饰",你不应该依赖 setSize,相反,你应该让窗口和布局管理器 API 为你做出这个决定,只需调用 pack,它将围绕内容首选大小打包窗口,例如...

Also, because the "available" space is the "window size" MINUS the "frame decorations", you shouldn't rely on setSize, instead, you should allow the window and the layout manager API to make this determination for you and simply call pack, which will pack the window around the contents preferred size, for example...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new ContentPane());
                frame.setResizable(false);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ContentPane extends JPanel {

        private static final long serialVersionUID = 1L;

        private GameView gameView;
        private PlayView playView;
        private TimeView timeView;

        public ContentPane() {
            this.setLayout(new BorderLayout());
            setGameView(new GameView(), new PlayView(), new TimeView());
        }

        public void setGameView(GameView gameView, PlayView playView, TimeView timeView) {
            this.gameView = gameView;
            this.playView = playView;
            this.timeView = timeView;

            JPanel subPanel = new JPanel();
            subPanel.setLayout(new BoxLayout(subPanel, BoxLayout.Y_AXIS));
            subPanel.add(gameView);
            subPanel.add(timeView);

            this.add(playView, BorderLayout.CENTER);
            this.add(subPanel, BorderLayout.WEST);
        }

    }

    public class GameView extends JPanel {

        private static final long serialVersionUID = 1L;

        public GameView() {
            this.setBackground(Color.decode("#2A2828"));
            this.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, Color.GRAY));
            this.setPreferredSize(new Dimension(200, 300));
        }

    }

    public class TimeView extends JPanel {

        private static final long serialVersionUID = 1L;

        public TimeView() {
            this.setBackground(Color.decode("#2A2828"));
            this.setPreferredSize(new Dimension(200, 500));
        }
    }

    public class PlayView extends JPanel {

        private static final long serialVersionUID = 1L;

        public PlayView() {
            this.setBackground(Color.decode("#1F1F40"));
            this.setPreferredSize(new Dimension(1100, 800));
        }

    }
}

我也不鼓励使用 setPreferredSize 以支持覆盖 getPreferredSize,因为 setPreferredSize 没有提供合适的方法来"管理"任何可能发生的变化

I'd also discourage the use of setPreferredSize in favour of overriding getPreferredSize, as setPreferredSize doesn't provide a suitable means to "manage" any changes which might occur

这篇关于JPanel 底部奇怪的白色边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆