目前,我有一个 JScrollPane 嵌套在具有 BorderLayout 的面板中。更重要的是,在 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();
}
}