java - 联系人列表 Swing

标签 java swing jscrollpane

我需要为联系人列表实现 ui,就像在 Skype 中一样。由派生自 JPanel 的自定义类(JContact)表示的联系人。我尝试使用不同的布局但没有收到预期的结果。主框架具有如下结构。

JFrame -> JPanel(contactsPanel)-> JScrollPane(scrollContacts)->JPanel(contactPanel)

scrollContacts.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_​​ALWAYS); scrollContacts.setHorizo​​ntalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_​​NEVER);

如果将 FlowLayout 用于 contactPanel(请参见图中的 FlowLayout)

我认为奇怪的行为,因为预期每个联系人都会一比一,因为为滚动联系人指示 HORIZONTAL_SCROLLBAR_​​NEVER。

如果使用网格或框布局。布局自动调整我的面板大小,它看起来很丑。请参阅 Grid&Box 布局图像。

预期结果见“预期”图片

----SEE IMAGE----

我不是母语人士,所以请原谅我的英语不好。感谢您的关注!

最佳答案

引用Swing tutorial :

The FlowLayout class puts components in a row, sized at their preferred size. If the horizontal space in the container is too small to put all the components in one row, the FlowLayout class uses multiple rows

所以你得到的结果是预期的。

A GridLayout object places components in a grid of cells. Each component takes all the available space within its cell, and each cell is exactly the same size. If the GridLayoutDemo window is resized, the GridLayout object changes the cell size so that the cells are as large as possible, given the space available to the container.

所以你得到的结果也是预期的。

When a BoxLayout lays out components from top to bottom, it tries to size each component at the component's preferred height. If the vertical space of the layout does not match the sum of the preferred heights, then BoxLayout tries to resize the components to fill the space.

所以你得到的结果也是预期的。

但是,盒子布局可以包含粘合组件以避免这种情况。

因此,我将使用垂直框布局,并添加垂直粘合作为最后一个组件。阅读 the tutorial .

关于java - 联系人列表 Swing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13777226/

相关文章:

Java swing Jlist 创建

java - 在 Java 8 中是否有更简洁的方法来执行此操作 - map 操作

Java程序以字母数字排序对象

java - 创建一个可以被 Eclipse 和 Netbeans 理解的 Java 桌面用户界面

java - 如何外部化 MouseListener?

java - 使 JFrame 中的 JTextArea 或 JEditorPane 可滚动

java - JPanel 在 JScrollPane 中不可见

java - 如何判断 Java 是否作为 Windows 服务运行以及是否使用了 -Xrs 标志

java - sftp 服务器中的连接被拒绝

java - 无法编辑 JTextField - Java Swing