我试图在 GridBagLayout
中的 Panel
中添加一个 JSeparator
,但它没有显示。
我试过类似的选项
panel.add(new JSeparator());
和
panel.add(new JSeparator(SwingConstants.HORIZONTAL));
他们都没有提供可见的分隔符。
最佳答案
阅读 How to Use Separators 上的 Swing 教程部分你会在哪里找到声明:
Separators have almost no API and are extremely easy to use as long as you keep one thing in mind: In most implementations, a vertical separator has a preferred height of 0, and a horizontal separator has a preferred width of 0. This means a separator is not visible unless you either set its preferred size or put it in under the control of a layout manager such as BorderLayout or BoxLayout that stretches it to fill its available display area.
因此,如果您将它与 GridBagLayout
一起使用,那么您将需要指定适当的约束,以便分隔符填充给定行的所有列。阅读 How to Use GridBagLayout 上的 Swing 教程部分有关约束和工作示例的更多信息。本教程演示了如何让按钮填充 3 列,因此请尝试用分隔符替换按钮。
关于java - 如何在面板上绘制分隔符 (GridBagLayout),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36880920/