【发布时间】:2016-07-17 18:00:45
【问题描述】:
我正在尝试为我的班级做一个项目。我应该构建 3 列数据。这是一个 GUI 类的代码:
public void displayArray(String[] wordArray) {
Container myContentPane = project1JFrame.getContentPane();
TextArea arrayArea = new TextArea();
for (int i = 0; i < wordArray.length; i++) {
if (wordArray[i] != null) {
arrayArea.append(wordArray[i] + "\n"); // add the words of the array into the TextArea
}
} //for
myContentPane.add(arrayArea, BorderLayout.WEST);
project1JFrame.setVisible(true);
} //displayArray
public void displaySortedArray(String[] wordArray) {
Container myContentPane = project1JFrame.getContentPane();
TextArea arrayArea = new TextArea();
for (int i = 0; i < wordArray.length; i++) {
if (wordArray[i] != null) {
arrayArea.append(wordArray[i] + "\n"); // add the words of the array into the TextArea
}
} //for
myContentPane.add(arrayArea, BorderLayout.CENTER);
project1JFrame.setVisible(true);
} //displaySortedArray
public void displaySortedList(WordList myList) {
Container myContentPane = project1JFrame.getContentPane();
TextArea listArea = new TextArea();
WordListIterator myIt;
listArea.setText("");
myIt = myList.reset();
while (myIt.hasNext()) {
myList.append(myIt.next() + "\n");
}
myContentPane.add(listArea, BorderLayout.EAST);
project1JFrame.setVisible(true);
}
当我尝试将此代码与我的主程序一起运行时,我只得到 2 列。我想要 3 列。我猜它与边框布局有关,但我似乎做不到。请帮忙!
【问题讨论】:
-
首先,您应该使用
javax.swing.JTextArea而不是java.awt.TextArea,可能无法解决您当前的问题,但可能会解决其他一些问题 -
考虑提供一个runnable example 来证明您的问题。这不是代码转储,而是您正在做的事情的一个例子,它突出了您遇到的问题。这将导致更少的混乱和更好的响应
标签: java swing layout-manager