gpt4 book ai didi

java - Swing在GridBagLayout组件中绘制1px的组件间边框线

转载 作者:行者123 更新时间:2023-11-30 09:49:48 28 4
gpt4 key购买 nike

我在 GridBagLayout 中布置了组件。我想要在所有组件之间以及 JPanel 本身周围有一条 1px 的黑线。

目前,我正在使用 MatteBorder 来执行此操作。父组件在顶部和左侧边缘有一个 1px 的 MatteBorder。每个子组件的右侧和底部边缘都有一个 1px 的 MatteBorder。 GridBagLayout 上的水平和垂直间隙为零。

这主要是有效的,除了我偶尔会在子边框与父边框相交的地方出现间隙。 Gaps in borders

我怀疑这是由于分配给子组件的额外空间的舍入/ float 不准确所致。

有没有更好的方法来实现这种外观?

附上一个更简单的例子:

public class SSBGuiTest extends JDialog {
public SSBGuiTest(Frame owner) {
super(owner);
initComponents();
}

public SSBGuiTest(Dialog owner) {
super(owner);
initComponents();
}

private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
wrapperPanel = new JPanel();
panelWithTopLeftMatteBorder = new JPanel();
panel1 = new JPanel();
label1 = new JLabel();
panel2 = new JPanel();
label2 = new JLabel();
panel3 = new JPanel();
label3 = new JLabel();

//======== this ========
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());

//======== wrapperPanel ========
{
wrapperPanel.setBorder(new EmptyBorder(15, 15, 15, 15));
wrapperPanel.setLayout(new BorderLayout());

//======== panelWithTopLeftMatteBorder ========
{
panelWithTopLeftMatteBorder.setBorder(new MatteBorder(1, 1, 0, 0, Color.black));
panelWithTopLeftMatteBorder.setLayout(new GridBagLayout());
((GridBagLayout)panelWithTopLeftMatteBorder.getLayout()).columnWidths = new int[] {0, 0};
((GridBagLayout)panelWithTopLeftMatteBorder.getLayout()).rowHeights = new int[] {0, 0, 0, 0};
((GridBagLayout)panelWithTopLeftMatteBorder.getLayout()).columnWeights = new double[] {1.0, 1.0E-4};
((GridBagLayout)panelWithTopLeftMatteBorder.getLayout()).rowWeights = new double[] {1.0, 1.0, 1.0, 1.0E-4};

//======== panel1 ========
{
panel1.setBorder(new MatteBorder(0, 0, 1, 1, Color.black));
panel1.setLayout(new BorderLayout());

//---- label1 ----
label1.setHorizontalAlignment(SwingConstants.CENTER);
label1.setText("label1");
panel1.add(label1, BorderLayout.CENTER);
}
panelWithTopLeftMatteBorder.add(panel1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));

//======== panel2 ========
{
panel2.setBorder(new MatteBorder(0, 0, 1, 1, Color.black));
panel2.setLayout(new BorderLayout());

//---- label2 ----
label2.setHorizontalAlignment(SwingConstants.CENTER);
label2.setText("label2");
panel2.add(label2, BorderLayout.CENTER);
}
panelWithTopLeftMatteBorder.add(panel2, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));

//======== panel3 ========
{
panel3.setBorder(new MatteBorder(0, 0, 1, 1, Color.black));
panel3.setLayout(new BorderLayout());

//---- label3 ----
label3.setHorizontalAlignment(SwingConstants.CENTER);
label3.setText("label3");
panel3.add(label3, BorderLayout.CENTER);
}
panelWithTopLeftMatteBorder.add(panel3, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
}
wrapperPanel.add(panelWithTopLeftMatteBorder, BorderLayout.CENTER);
}
contentPane.add(wrapperPanel, BorderLayout.CENTER);
pack();
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents
}

// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
private JPanel wrapperPanel;
private JPanel panelWithTopLeftMatteBorder;
private JPanel panel1;
private JLabel label1;
private JPanel panel2;
private JLabel label2;
private JPanel panel3;
private JLabel label3;
// JFormDesigner - End of variables declaration //GEN-END:variables
}

看起来像这样:

enter image description here

最佳答案

我想我会使用 BorderFactory.createLineBorder(color, thickness) 而不是 ...matteBorder,因为 LineBorder 似乎是更接近你想要做的事情。为方便起见,您还可以使用 LineBorder.createBlackLineBorder()

如果某些组件的外部没有完全接触其容器的内部,请检查容器(即外部组件)是否设置了非零插图!


另一种解决方案可能是让您的背景容器具有黑色背景和 1 像素的插入,并在其上放置无边框组件,它们之间有 1 像素的间隙。这应该会在顶部没有组件的任何地方产生非常精确的黑线,并且还会消除多个边框相遇并导致双倍宽度边框的问题。


最后,在我看来,您正在使用组件来绘制表格。您是否需要这些组件具有某种行为,或者真的只是将表格放在您的组件上会好吗?您可以使用例如非常方便地做到这一点JLabelJTextPanel 并将它们的 text 属性设置为 HTML 以显示任何内容。 Java 结合了一个相当复杂的 HTML 布局引擎,它甚至可以很好地处理 CSS 的子集,无论是内联的还是来自 href 的文件。

关于java - Swing在GridBagLayout组件中绘制1px的组件间边框线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5586553/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com