- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如所附屏幕截图所示 - 黄线来自底层 BoxLayout
面向的 JPanel
。更改为 BorderLayout
删除黄线:
代码示例如下:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class FrameDemo {
private String[] suits = {"hearts", "spades", "diamonds", "clubs", "joker"};
public static void main(String[] args) {
new FrameDemo();
}
public FrameDemo() {
JFrame mainframe = new JFrame();
mainframe.setPreferredSize(new Dimension(800, 600));
mainframe.setLocationByPlatform(true);
mainframe.setTitle("Playing Card Game! v0.1");
mainframe.getContentPane().setLayout(new BorderLayout());
JPanel top = new JPanel();
top.setBackground(Color.BLUE);
top.setPreferredSize(new Dimension(800, 50));
JPanel left = new JPanel();
left.setBackground(Color.RED);
left.setPreferredSize(new Dimension(50, 500));
JPanel centre = new JPanel();
centre.setBackground(Color.YELLOW);
centre.setLayout(new BoxLayout(centre, BoxLayout.Y_AXIS));
JPanel right = new JPanel();
right.setBackground(Color.GREEN);
right.setPreferredSize(new Dimension(50, 500));
JPanel bot = new JPanel();
bot.setBackground(Color.GRAY);
bot.setPreferredSize(new Dimension(800, 50));
for(String suit : suits) {
if(!(suit.equals("joker"))) {
JPanel layer = new JPanel();
layer.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
layer.setBackground(Color.BLACK);
centre.add(layer);
}
}
mainframe.add(top, BorderLayout.NORTH);
mainframe.add(left, BorderLayout.WEST);
mainframe.add(centre, BorderLayout.CENTER);
mainframe.add(right, BorderLayout.EAST);
mainframe.add(bot, BorderLayout.SOUTH);
mainframe.setDefaultCloseOperation(
WindowConstants.DO_NOTHING_ON_CLOSE);
mainframe.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
mainframe.pack();
mainframe.setVisible(true);
}
}
我尝试过在 Color.BLACK
JPanel 上设置边框,但似乎没有多大作用。 Swing 为什么要这样做?可以修复吗?
注意:无论我是否使用setPreferredSize()
,都会显示此信息。
最佳答案
Why is Swing doing this?
添加到中心面板的每个面板的首选高度为 20。BoxLayout 需要缩放每个面板都填充整个可用空间。完成此缩放后,看起来似乎存在某种舍入问题。
Can it be fixed?
不确定您的具体要求是什么?
如果您尝试使每个组件具有相同的高度,则可以使用 Relative Layout 。它有一个属性,允许您指定如何处理舍入结果的剩余像素。
编辑:
仔细查看您的代码,问题是:
//mainframe.setPreferredSize(new Dimension(800, 600));
切勿设置框架的大小。这是布局管理器的工作。您手动设置的大小包括标题栏和边框,因此所有面板都比您希望的小,并且不能被 10 整除。
//mainframe.setPreferredSize(new Dimension(800, 600));
关于java - BoxLayout 在底部留下像素线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21289569/
以下用于布局组件的代码返回错误 public class MainPanel extends JPanel { private JTextField txt1, txt2; priva
我试图将 4 个水平盒子布局以垂直方式堆叠在 BoxLayout 中。 我的 KV 文件: : BoxLayout: size: root.size pos:
我知道这里有很多 BoxLayout 问题,但是我找不到可以解决我的问题的问题。我需要我的 ScoreDescPanel 来在彼此正下方显示每个标签(如列表),但是我在使用 BoxLayout 时遇到
我正在使用 BoxLayout 来定位服务器 GUI 的 GUI 元素。我的 JLabels 发生了一件奇怪的事情。我想知道为什么当我将字符串附加到 JTextArea 时,JLabels 会从其原始
我正在尝试制作一个简单的计算器,以熟悉我刚刚在计算机科学入门类(class)中学到的 GUI 的创建。我在按照我想要的方式排列 GUI 时遇到问题。我认为 BoxLayout 是完美的,但每当我尝试使
我有一个非常简单的自定义组件。它的首选最大尺寸为 100x100,并且只是一个红色矩形。我将其添加到使用框布局的 JPanel 中。我预计自定义组件的大小为 100x100,但实际上它是 50x100
我刚刚开始摆弄 BoxLayout 管理器。 我制作了两个相邻的按钮,第三个按钮应该转到下一行(前两个按钮下方),前两个按钮应该位于框架的顶部。 我怎样才能做到这一点? 这是我当前的代码 Bo
我的这段代码似乎抛出了 IllegalComponentException,但我不确定为什么。归结为以下几行代码: JRadioButton setRed = new JRadioButton(“Re
查看了一些以前与我的主题相关的帖子,但也无济于事。 尝试使用 BoxLayout 对齐组件但我无法让它发挥作用。我已经修改了一段时间,得到了不同的结果,但我无法弄清楚。我用的是默认的FlowLayou
我有两个 JPanel,我想将它们排列在一个更大的 JPanel 内部,一个叠在另一个之上。 “panel_controls”覆盖.getPreferredSize()。代码: public fina
你好,我正在尝试以与声明 BorderLayout 相同的方式声明 BoxLayout 声明borderlayout的方式 JPanel textFiled = new JPanel(new Bord
如所附屏幕截图所示 - 黄线来自底层 BoxLayout 面向的 JPanel。更改为 BorderLayout 删除黄线: 代码示例如下: import java.awt.BorderLayout;
是的,我用谷歌搜索了大约 30 分钟。是的,stackoverflow 中有 2 个关于该主题的不同帖子,但这些帖子并没有为我的问题提供任何解决方案。 我正在使用相当多的带有 BoxLayout 的面
我正在尝试在我的程序中制作 BoxLayout 布局。我使用 Eclipse,所以当我输入这些行时, JTextArea fntxt = new JTextArea(1, 20); JTextArea
我正在创建一个具有多个聊天的消息传递程序。在聊天窗口的一侧有一个 JPanel,其中包含用于切换聊天的所有按钮的列表数组。我无法使侧面板中的所有按钮具有相同的宽度,无论它们包含什么。但无论我尝试什么似
我似乎无法在网上找到解决方案来解释为什么我在尝试运行时遇到此错误 我正在为不同的程序制作一个简单的测试系统,当按下按钮时将在文本框中产生值。我希望它们位于不同的行以使其更清晰,所以我研究了布局。我认为
我试图弄清楚如何在运行时将项目插入到 BoxLayout...或使用不同的小部件/面板/布局实现类似的效果。 应用程序窗口的设计是垂直堆叠多个面板(BoxLayout.Y_AXIS)。然后,用户可以插
我正在尝试使用 BoxLayout 而不是我的 GridLayout 代码: setLayout (new GridLayout (5, 2)); 除了我注意到,当你做 BoxLayout 时,你需要
谁能帮帮我。为什么标签“当前”没有在面板/框架中左对齐? public static void main(String[] args) { JFrame TFrame =
我正在尝试沿面板中心的垂直轴对齐多个元素,而 BoxLayout 似乎正是我所需要的。但是,当添加的所有元素都具有奇数宽度时,它似乎会做一些奇怪的事情。 这是演示这种古怪行为的 SSCCE: impo
我是一名优秀的程序员,十分优秀!