在 Swing BorderLayout 的侧面板中设置最小宽度
Set minimum width in side panel of Swing BorderLayout
我了解 BorderLayout 的某些部分 -- 例如,EAST/WEST(或 BEGINNING_OF_LINE/END_OF_LINE)面板组件保持一个宽度,并且其长度随着 window 的长度而拉伸。
我想在 WEST 侧放置一个面板,它本身有多个组件 - 在这种情况下,是一个按钮面板和按钮控制的 JList。我想为该 JList 中的字符串分配一个最小宽度,但某些东西(可能是 BorderLayout)阻止我设置最小或首选宽度。
当我 运行 下面的代码时,左侧面板中的列表对于 "LongNameGame 3" 来说足够宽,但这只是因为我在呈现列表之前添加了字符串。我想设置该 JList 的宽度以适应我选择的宽度的字符串。稍后我会将它放在 ScrollPane 中,用于比这更宽的字符串,但这是一个不同的问题。
将我介绍给其他布局管理器并没有回答我的问题——如果可能的话,我想知道如何使用 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);
}
}
}
为 JList 提供足够宽的原型单元格值以显示您需要的内容。例如,
gameJList.setPrototypeCellValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
原型值(此处为String,因为列表已声明为JList<String>
)用于设置列表的首选大小,但不显示在JList 中。您可以根据需要使用大列表或小列表。还要确保在水平维度中出于相同目的设置可见行数:
gameJList.setVisibleRowCount(20); // for example
我了解 BorderLayout 的某些部分 -- 例如,EAST/WEST(或 BEGINNING_OF_LINE/END_OF_LINE)面板组件保持一个宽度,并且其长度随着 window 的长度而拉伸。
我想在 WEST 侧放置一个面板,它本身有多个组件 - 在这种情况下,是一个按钮面板和按钮控制的 JList。我想为该 JList 中的字符串分配一个最小宽度,但某些东西(可能是 BorderLayout)阻止我设置最小或首选宽度。
当我 运行 下面的代码时,左侧面板中的列表对于 "LongNameGame 3" 来说足够宽,但这只是因为我在呈现列表之前添加了字符串。我想设置该 JList 的宽度以适应我选择的宽度的字符串。稍后我会将它放在 ScrollPane 中,用于比这更宽的字符串,但这是一个不同的问题。
将我介绍给其他布局管理器并没有回答我的问题——如果可能的话,我想知道如何使用 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);
}
}
}
为 JList 提供足够宽的原型单元格值以显示您需要的内容。例如,
gameJList.setPrototypeCellValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
原型值(此处为String,因为列表已声明为JList<String>
)用于设置列表的首选大小,但不显示在JList 中。您可以根据需要使用大列表或小列表。还要确保在水平维度中出于相同目的设置可见行数:
gameJList.setVisibleRowCount(20); // for example