- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个非常简单的自定义组件。它的首选最大尺寸为 100x100,并且只是一个红色矩形。我将其添加到使用框布局的 JPanel 中。我预计自定义组件的大小为 100x100,但实际上它是 50x100(始终是最大宽度的一半)。
这是代码:
public class Testing extends JFrame {
class TestComponent extends JComponent {
@Override
public Dimension getMaximumSize() {
return new Dimension(100, 100);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setPaint(Color.RED);
g2d.fillRect(getX(), getY(), getWidth(), getHeight());
System.out.println(getWidth());
}
}
private JPanel panel;
private TestComponent testComponent;
public Testing() {
super("Testing");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(1280, 720);
setLocationRelativeTo(null);
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.setBackground(Color.CYAN);
testComponent = new TestComponent();
panel.add(testComponent);
add(panel);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(()->{
new Testing().setVisible(true);
});
}
}
如果我添加一个设置了首选尺寸和最大尺寸的 JPanel,而不是我的自定义组件,则不会发生这种情况。
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.setBackground(Color.CYAN);
// testComponent = new TestComponent();
// panel.add(testComponent);
JPanel test = new JPanel();
test.setPreferredSize(new Dimension(100,100));
test.setMaximumSize(new Dimension(100,100));
test.setBackground(Color.RED);
panel.add(test);
add(panel);
我不确定我错过了什么。谢谢!
最佳答案
I have a custom component that is extremely simple.
自定义组件负责确定其:
您只需实现首选尺寸和最大尺寸。
But it still doesn't make sense why the component isn't centered and is half the maximum width.
BoxLayout 尝试遵守组件的最小和最大尺寸。
默认的最小尺寸是 (0, 0),这似乎会导致 BoxLayout 的困惑。我不知道为什么。我想说这是 BoxLayout 的一个错误/功能。
您需要实现getMinimumSize()
方法。这似乎有效:
@Override
public Dimension getMinimumSize() {
return new Dimension(2, 2);
}
注意我尝试了 (1, 1) 但没有成功。再次,不知道为什么。
我建议,对于您的情况,您可能需要使用:
@Override
public Dimension getMinimumSize() {
return getPreferredSize();
}
对于最小值和最大值方法,因为您似乎希望大小保持不变。
关于java - BoxLayout 为自定义组件提供了意外的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61426414/
以下用于布局组件的代码返回错误 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
我是一名优秀的程序员,十分优秀!