java - 如何在面板上绘制分隔符 (GridBagLayout)

标签 java swing layout-manager gridbaglayout jseparator

我试图在 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/

相关文章:

java - 我想将 java 中的数组中的值存储在我自己类型的 arrayList 中。我该怎么做

java - 俄罗斯方 block : Turning the pieces?

Java Swing 下拉列表

java - 获取面板的 gridbag 约束

java - Swing 中展开的 float 复选框

java - 向图像添加 Action 监听器

java - 如何参数化 Maven surefire 插件以便我可以选择要运行的 TestNG 套件

java - 拖动对象时更改其颜色

java - 为什么在Java中必须使用paintComponent方法来绘制?

java - Jframe 内的 JPanel