gpt4 book ai didi

java - JSeparator 产生一个长空格

转载 作者:行者123 更新时间:2023-11-30 06:24:24 24 4
gpt4 key购买 nike

我想将 JSeparator 放在工具栏的各个部分之间。当我添加以下内容时,分隔符可见:

toolbar.add(radioButtonDDA, BorderLayout.NORTH);
toolbar.add(radioButtonPolygon, BorderLayout.NORTH);
toolbar.add(new JSeparator(1));
toolbar.add(radioButtonseedFill, BorderLayout.NORTH);
toolbar.add(radioButtonScanLine, BorderLayout.NORTH);
toolbar.add(patternCheckBox, BorderLayout.NORTH);
toolbar.add(buttonClear, BorderLayout.NORTH);

但是,它还会在分隔符后面创建一个空格。如何去掉空格?

toolbar space

最佳答案

不要直接使用 JSeparator,而是使用 JToolBar 内置支持

来自JavaDocs

JSeparator provides a general purpose component for implementing divider lines - most commonly used as a divider between menu items that breaks them up into logical groupings. Instead of using JSeparator directly, you can use the JMenu or JPopupMenu addSeparator method to create and add a separator.

Separator

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JToolBar;
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();
}

JToolBar toolbar = new JToolBar();
toolbar.add(new JRadioButton("One"));
toolbar.add(new JRadioButton("Two"));
toolbar.addSeparator();
//toolbar.add(new JSeparator(JSeparator.VERTICAL));
toolbar.add(new JRadioButton("Three"));
toolbar.add(new JRadioButton("Four"));
toolbar.add(new JRadioButton("Five"));

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(toolbar, BorderLayout.NORTH);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
add(new JLabel("Just here to fill space"));
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

}

}

But why (does it do this)?

我没有深入研究这个问题(甚至没有深入研究),但我猜测布局管理器正在使用分隔符的最大尺寸属性来确定如何最好地布局组件,这为它提供了剩余空间...这可能就是为什么 JToolBar 支持它自己的分隔符

关于java - JSeparator 产生一个长空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47490954/

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