JPanel底部的怪异白色边框

时间:2018-12-30 23:24:17

标签: java swing

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

enter image description here

这是我的ContentPane.java

Sub UpDateDDE()
 Dim Mylink As String
 Dim Procedure As String
  Mylink = "MT4|BID!EURJPY"
  Procedure = "msg"
    With ThisWorkbook.SetLinkOnData Mylink Procedure
End Sub

Sub msg()
  MsgBox "new value detected!"
End Sub

这是GameView.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);
}

}

这是TimeView.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));
}

}

这是PlayView.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));
}
}

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

如果有人可以向我解释是什么导致了这个问题,那就太好了。

编辑:

这是MainFrame.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));
}

}

}

1 个答案:

答案 0 :(得分:1)

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

在设置使窗口可见的大小之前,您应该致电setResize

此外,因为“可用”空间是“窗口大小”减去“框架装饰”,所以您不应该依赖setSize,而应该允许窗口和布局管理器API进行为您确定后,只需调用pack,它将把窗口包装在首选内容的周围,例如...

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并没有提供适当的方法来“管理”可能发生的任何变化