java - BorderLayout.CENTER 和 BorderLayout.EAST 之间的 JSeparator?

标签 java swing user-interface jframe

我想制作一个带有 BorderLayout 的程序,其中中心和东部在图形上是分开的。我尝试了以下操作,但不幸的是分隔符位于 JFrame 的左侧,而不是在 centerPanel 和 rightPanel 之间。

setLayout(new BorderLayout());

JPanel centerPanel = new JPanel();
JPanel rightPanel = new JPanel();

centerPanel.add(new JLabel("center"));
rightPanel.add(new JLabel("right"));

getContentPane().add(centerPanel, BorderLayout.CENTER);
getContentPane().add(rightPanel, BorderLayout.EAST);

getContentPane().add(new JSeparator(JSeparator.VERTICAL), BorderLayout.LINE_START);

有人可以帮帮我吗?

最佳答案

您可以使用 Nested Layout为您的中心面板提供 BorderLayout 并将垂直分隔符添加到该面板而不是内容面板的方法:

JPanel centerPanel = new JPanel(new BorderLayout());
centerPanel.add(new JLabel("center"), BorderLayout.CENTER);
centerPanel.add(new JSeparator(JSeparator.VERTICAL), BorderLayout.LINE_END);

JPanel rightPanel = new JPanel();
rightPanel.add(new JLabel("right"));

getContentPane().add(centerPanel, BorderLayout.CENTER);
getContentPane().add(rightPanel, BorderLayout.LINE_END);

其他说明

1) 从 Java 1.4 开始,当使用 BorderLayout 时,强烈鼓励使用新常量:

PAGE_START
PAGE_END
LINE_START
LINE_END
CENTER

来自 How to Use BorderLayout教程(我的粗体文本):

Before JDK release 1.4, the preferred names for the various areas were different, ranging from points of the compass (for example, BorderLayout.NORTH for the top area) to wordier versions of the constants we use in our examples. The constants our examples use are preferred because they are standard and enable programs to adjust to languages that have different orientations.

2) 如果您不想添加任何与 Swing 相关的功能,请避免扩展 Swing 组件。见:

关于java - BorderLayout.CENTER 和 BorderLayout.EAST 之间的 JSeparator?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26633876/

相关文章:

c++ - 使用 qt 为预先构建的 c 程序构建 GUI

java - 检查电源实现中的溢出

java - 速度:$display.list() 和对象的集合

Java Swing 按钮保持按下状态

java - 如何将文件中的多行读取到字符串中以放入 jTextArea 中?

任意数量 Pane 的 CSS 动画过渡效果

WPF UI 建议?

Java移动矩形更快?

java - 如何在android中使用GSON解析JSON

java - 将自定义组件添加到 NetBeans GUI 构建器! (世界风)