[英]Set minimum width in side panel of Swing BorderLayout
I understand some parts of BorderLayout -- eg, the EAST/WEST (or BEGINNING_OF_LINE/END_OF_LINE) panel component stays one width and its length is stretched with the length of the window.我了解 BorderLayout 的某些部分——例如,EAST/WEST(或 BEGINNING_OF_LINE/END_OF_LINE)面板组件保持一个宽度,并且它的长度随着 window 的长度而拉伸。
I want to put a panel on the WEST side that itself has multiple components - a panel of buttons and a JList of things the buttons control, in this case.我想在 WEST 侧放置一个面板,该面板本身具有多个组件 - 在这种情况下,一个按钮面板和一个按钮控制的 JList。 I would like to allocate a minimum width for the strings in that JList, but something (probably BorderLayout) prevents me from setting a minimum or preferred width.我想为该 JList 中的字符串分配最小宽度,但某些东西(可能是 BorderLayout)阻止我设置最小或首选宽度。
When I run the code below, the list in the left panel is wide enough for "LongNameGame 3", but only because I added the string before rendering the list.当我运行下面的代码时,左侧面板中的列表对于“LongNameGame 3”来说足够宽,但这只是因为我在渲染列表之前添加了字符串。 I would like to set the width of that JList to accommodate strings of the width of my choice.我想设置该 JList 的宽度以容纳我选择的宽度的字符串。 Later I'll put it in a ScrollPane for strings wider than that, but that's a different problem.稍后我会将它放在一个 ScrollPane 中,用于比这更宽的字符串,但这是一个不同的问题。
My question is not answered by referring me to other layout managers -- I want to know how to do this with BorderLayout, if possible.将我推荐给其他布局管理器并没有回答我的问题——如果可能的话,我想知道如何使用 BorderLayout 来解决这个问题。
package comm;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
public class BLPlay
{
public static void main(String ... arguments)
{
JFrame frame = buildLoungeFrame();
frame.setVisible(true);
}
private static JFrame buildLoungeFrame()
{
JFrame loungeFrame = new JFrame("BLPlay");
loungeFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loungeFrame.setLayout(new BorderLayout(10,10));
// left panel is another BorderLayout panel with buttons and a list of games
JPanel gameListControlPanel = new JPanel(new BorderLayout());
Border innerBorder = BorderFactory.createLineBorder(Color.BLACK, 2);
Border outerBorder = BorderFactory.createEmptyBorder(3,3,3,3);
gameListControlPanel.setBorder(BorderFactory.createCompoundBorder(outerBorder, innerBorder));
String[] gamePanelButtonLabels = { "New", "Join", "Leave", "End" };
JPanel gamePanelButtons = new JPanel(new GridLayout(gamePanelButtonLabels.length,1));
addButtons(gamePanelButtons, gamePanelButtonLabels);
JPanel gamePanelButtonsContainerPanel = new JPanel();
gamePanelButtonsContainerPanel.add(gamePanelButtons);
gameListControlPanel.add(gamePanelButtonsContainerPanel, BorderLayout.WEST);
Vector<String> gameList = new Vector<>();
gameList.add("Game 1");
gameList.add("Game 2");
gameList.add("LongNameGame 3");
JList<String> gameJList = new JList<>(gameList);
JPanel gameListPanel = new JPanel(new FlowLayout());
gameListPanel.setMinimumSize(new Dimension(600,600)); // <-- has no effect
gameListPanel.add(gameJList);
gameListControlPanel.add(gameListPanel, BorderLayout.EAST);
loungeFrame.add(gameListControlPanel, BorderLayout.WEST);
// center panel in the lounge is for chat messages; it has a separate border layout,
// center for accumulated messages, bottom for entering messages
JPanel chatMessagePanel = new JPanel(new BorderLayout());
// Border chatMessagePanelBorder = BorderFactory.createEmptyBorder(7,7,7,7);
// chatMessagePanel.setBorder(chatMessagePanelBorder);
JTextArea chatMessages = new JTextArea();
chatMessagePanel.add(chatMessages, BorderLayout.CENTER);
// debug
chatMessages.append("message one\n");
chatMessages.append("message two\n");
chatMessages.append("message three\n");
// and lower panel is for entering one's own chat messages
JPanel chatMessageEntryPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JTextField chatMessageEntryField = new JTextField(35);
JButton chatMessageEntryButton = new JButton("Enter");
chatMessageEntryPanel.add(chatMessageEntryField);
chatMessageEntryPanel.add(chatMessageEntryButton);
chatMessagePanel.add(chatMessageEntryPanel, BorderLayout.SOUTH);
loungeFrame.add(chatMessagePanel, BorderLayout.CENTER);
loungeFrame.pack();
return loungeFrame;
}
private static void addButtons(JPanel panel, String ... labels)
{
for (String label : labels)
{
JButton button = new JButton(label);
panel.add(button);
}
}
}
Give the JList a prototype cell value that is wide enough to display what you need.为 JList 提供一个足够宽的原型单元格值,以显示您需要的内容。 eg,例如,
gameJList.setPrototypeCellValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
The prototype value (here a String because the list has been declared as a JList<String>
) is used to set the list's preferred size, but is not displayed in the JList.原型值(此处为 String,因为列表已声明为JList<String>
)用于设置列表的首选大小,但不会显示在 JList 中。 You can use as large or small a list as you need.您可以根据需要使用尽可能大或小的列表。 Also be sure to set visible row count for the same purpose in the horizontal dimension:还要确保在水平维度中为相同目的设置可见行数:
gameJList.setVisibleRowCount(20); // for example
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.