目前,我有一个嵌套在具有BorderLayout的Panel中的JScrollPane。更重要的是,在JScrollPane中,我打算垂直列出多个文本区域,其宽度不超过JScrollPane,并且需要尽可能多的高度(滚动窗格只有一个垂直滚动条)。
我的问题是文本区域看起来相对于最大的文本区域在JScrollPane中居中,并且它们都是一行。
任何人都可以告诉我,查看我的代码,如何在滚动窗格的右侧之前将文本区域包裹起来,以及如何将它们保持对齐?
private JPanel generateStateView(State state) {
//Create a new panel, and the info label.
JPanel container = new JPanel(new BorderLayout());
JLabel infoLabel = new JLabel("The state of " + state.getName() +
", with " + state.getElectoralVotes() + " Electoral Votes.");
container.add(infoLabel, BorderLayout.NORTH); //Put the label on top.
//Will scroll through polls.
JScrollPane pollViewer = new JScrollPane();
//This will be the scroll pane's "viewport".
JViewport pollViewport = new JViewport();
//And finally, this will actually hold the individual polls.
JPanel pollPanel = new JPanel(new GridLayout(
state.getPolls().size(), 1));
pollPanel.setAlignmentX(LEFT_ALIGNMENT);
//Iteratively add the polls to the container
for (Poll poll : state.getPolls()) {
//Holds individual polls
Box pollBox = Box.createHorizontalBox();
//Create the poll's panel and add it to the poll container.
pollBox.add(generatePollPanel(poll));
pollBox.add(Box.createHorizontalGlue()); //Fill to the right
pollBox.setAlignmentX(LEFT_ALIGNMENT);
pollPanel.add(pollBox); //Put the box into the pollPanel.
}
//Put the panel into the viewport.
pollViewport.add(pollPanel);
//Put the viewport "into" the scroll pane
pollViewer.setViewport(pollViewport);
//Put the pane into the state view.
container.add(pollViewer, BorderLayout.CENTER);
return container; //And give back the container.
}
/**
* Method: generatePollPanel
* Purpose: Generates a panel containing information on a particular poll.
* @param poll The poll to have information generated from.
* @return The JPanel.
*/
private JPanel generatePollPanel(Poll poll) {
//Create a new panel, then a text area to fill with the info.
JPanel pollPanel = new JPanel();
JTextArea pollInfo = new JTextArea("Conducted by " + poll.getAgency() +
" on day " + poll.getDate() + " for " + poll.getNumDays() +
" days, " + poll.getPercentVoteDem() +
"% voted Democrat, and " + poll.getPercentVoteRep() +
"% voted Republican.");
pollInfo.setEditable(false); //We don't want the user editing this.
pollPanel.add(pollInfo); //Throw the area in, and return the panel.
pollPanel.setAlignmentX(LEFT_ALIGNMENT);
return pollPanel;
}
/**
* Method: valueChanged
* Purpose: Handle the event of a different list item being selected.
* @param event The object containing information about this event.
*/
public void valueChanged(ListSelectionEvent event) {
//Instantiating JList with a type of ? seems to solve the issue of
//eclipse complaining about an unchecked cast (originally to
//JList<String>, so I should be able to cast child values directly
//to string later on anyways.
JList<?> stateList = (JList<?>) event.getSource();
//Account for keyboard and mouse actions
if (!event.getValueIsAdjusting()) {
State chosenState; //Keep track of the picked state.
try {
//Try to get the currently selected state
chosenState = db.findState((String) stateList.getSelectedValue());
}
catch (NoSuchElementException exception) {
//Somehow the picked state was not found, notify the user.
reportError("The selected state is not available.");
return;
}
//Find the container for the gui window.
Container container = getContentPane();
//Remove the empty state view container by generating a new one.
container.remove(currentStateView);
//Generate the state view and add that to the container.
currentStateView = generateStateView(chosenState);
container.add(currentStateView, BorderLayout.CENTER);
//Redraw everything.
revalidate();
}
}
答案 0 :(得分:4)
看起来像遗失了的严重情况: - )
虽然嵌套可以是实现布局要求的一种方法,但如果不能提供预期的输出,很难找出究竟出现了什么问题。此外,还有一些警告信号表明嵌套基本上是错误的
在您的情况下,除了顶级borderlayout之外的所有内容都有一个“netto”(也就是说,不打算围绕布局问题)孩子,其级别为:
BorderLayout (the state view)
GridLayout (the panel that's the scrollPane's view)
BoxLayout (the panel that contains a single pollBox)
FlowLayout (the panel that contains the textArea)
经验丰富的问题:
都归功于LayoutManagers的特性,无论是部分(第一)还是完全(第二):
时间退后一步并审查初始要求:
垂直列出的多个文本区域,宽度不是 超过JScrollPane,以及他们需要的高度
零订单解决方案是
在代码中:
JComponent overview = new JPanel(new BorderLayout());
overview.add(new JLabel("All polls for state XY"), BorderLayout.NORTH);
JComponent pollPanel = new JPanel();
pollPanel.setLayout(new BoxLayout(pollPanel, BoxLayout.PAGE_AXIS));
for (int i = 0; i < 10; i++) {
pollPanel.add(generatePollTextArea(i)); //Put the box into the pollPanel.
}
pollPanel.add(Box.createVerticalGlue());
JScrollPane pollViewer = new JScrollPane(pollPanel);
overview.add(pollViewer);
showInFrame(overview, "layout text only");
protected JComponent generatePollTextArea(int i) {
String agency = "agency ";
// simulates different size text
for (int j = 0; j < i; j++) {
agency += i;
}
String text = "Conducted by " + agency +
" on day " + "today" + " for " + 20 +
" days, " + 99 +
"% voted Democrat, and " + 0.2 +
"% voted Republican.";
JTextArea pollInfo = new JTextArea(text);
// give it some reasonable initial width
// in terms of content
pollInfo.setColumns(20);
pollInfo.setLineWrap(true);
pollInfo.setWrapStyleWord(true);
pollInfo.setEditable(false); //We don't want the user editing this.
return pollInfo;
}
“Das Wort zum Dienstag”:嵌套布局不是避免学习在嵌套布局的各个级别上使用的LayoutManagers的特征的手段。
答案 1 :(得分:3)
首先,我不认为Box
被认为与BoxLayout
以外的任何东西一起使用(我可能错了,但这就是我读它的方式)
就个人而言,我要么使用SwingLabs的GridBagLayout
或VerticalLayout
。
为了便于横向限制,您还需要查看Scrollable界面,尤其是getScrollableTracksViewportWidth