gpt4 book ai didi

java - 如何在 GridBagLayout 中的两个 JLabel 之间添加 JSeparator

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:18:03 26 4
gpt4 key购买 nike

我想在 GridBagLayout 中的每个 JLabel 之间有一个 JSeparator。目前它看起来像这样:

enter image description here

现在我想在图标和 JLabel 之间的每个 TESTSTEP 标签之后添加一个 JSeparator。以下约束仅适用于 JLabel 和图标。我必须添加什么才能在整个垂直线上获得 JSeparator?

  GridBagConstraints lastConstraints = new GridBagConstraints();
GridBagConstraints labelConstraints = new GridBagConstraints();

lastConstraints.fill = GridBagConstraints.NONE;
lastConstraints.anchor = GridBagConstraints.EAST;
lastConstraints.weightx = 0.0;
lastConstraints.gridwidth = GridBagConstraints.REMAINDER;
lastConstraints.insets = new Insets(8, 8, 8, 8);

labelConstraints = (GridBagConstraints) lastConstraints.clone();
labelConstraints.weightx = 0.0;
labelConstraints.fill = GridBagConstraints.NONE;
labelConstraints.anchor = GridBagConstraints.WEST;
labelConstraints.gridwidth = 1;

最佳答案

使用:

  • 填充 = 水平;
  • 权重x = 1.0;
  • 网格宽度 = REMAINDER;

小例子:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class TestJSeparator {

public TestJSeparator() {
JFrame frame = new JFrame(TestJSeparator.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel bigPanel = new JPanel(new GridBagLayout());
GridBagConstraints lastConstraints = new GridBagConstraints();
GridBagConstraints labelConstraints = new GridBagConstraints();
GridBagConstraints separatorConstraint = new GridBagConstraints();
lastConstraints.fill = GridBagConstraints.NONE;
lastConstraints.anchor = GridBagConstraints.EAST;
lastConstraints.weightx = 0.0;
lastConstraints.gridwidth = GridBagConstraints.REMAINDER;
lastConstraints.insets = new Insets(8, 8, 8, 8);

labelConstraints = (GridBagConstraints) lastConstraints.clone();
labelConstraints.weightx = 0.0;
labelConstraints.fill = GridBagConstraints.NONE;
labelConstraints.anchor = GridBagConstraints.WEST;
labelConstraints.gridwidth = 1;

separatorConstraint.weightx = 1.0;
separatorConstraint.fill = GridBagConstraints.HORIZONTAL;
separatorConstraint.gridwidth = GridBagConstraints.REMAINDER;

JLabel label1 = new JLabel("1. TESTSTEP 0 TEST 0 DE");
JLabel result1 = new JLabel(UIManager.getIcon("OptionPane.informationIcon"));
JLabel label2 = new JLabel("2. TESTSTEP 0 TEST 1 DE");
JLabel result2 = new JLabel(UIManager.getIcon("OptionPane.errorIcon"));
bigPanel.add(label1, labelConstraints);
bigPanel.add(result1, lastConstraints);
bigPanel.add(new JSeparator(JSeparator.HORIZONTAL), separatorConstraint);
bigPanel.add(label2, labelConstraints);
bigPanel.add(result2, lastConstraints);
bigPanel.add(new JSeparator(JSeparator.HORIZONTAL), separatorConstraint);
frame.add(bigPanel);
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TestJSeparator gui = new TestJSeparator();
}
});
}
}

关于java - 如何在 GridBagLayout 中的两个 JLabel 之间添加 JSeparator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16812538/

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