gpt4 book ai didi

Java GUI 3 列边框布局

转载 作者:行者123 更新时间:2023-12-02 03:45:55 25 4
gpt4 key购买 nike

我正在尝试为我的类(class)做一个项目。我应该构建 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 列。我猜这与边框布局有关,但我似乎无法做到这一点。请帮忙!

最佳答案

似乎对我来说工作正常......

BorderLayout

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
setLayout(new BorderLayout());
add(new JScrollPane(makeTextArea("On the left")), BorderLayout.WEST);
add(new JScrollPane(makeTextArea("Jam in the middle")), BorderLayout.CENTER);
add(new JScrollPane(makeTextArea("On the right")), BorderLayout.EAST);
}

protected JTextArea makeTextArea(String text) {
JTextArea ta = new JTextArea(10, 20);
ta.setText(text);
return ta;
}

}

}

考虑提供runnable example这说明了你的问题。这不是代码转储,而是您正在执行的操作的示例,它突出显示了您遇到的问题。这将减少困惑并获得更好的响应

不过,我会考虑使用 GridLayout 代替...

GridLayout

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
setLayout(new GridLayout(1, 3));
add(new JScrollPane(makeTextArea("On the left")), BorderLayout.WEST);
add(new JScrollPane(makeTextArea("Jam in the middle")), BorderLayout.CENTER);
add(new JScrollPane(makeTextArea("On the right")), BorderLayout.EAST);
}

protected JTextArea makeTextArea(String text) {
JTextArea ta = new JTextArea(10, 20);
ta.setText(text);
return ta;
}

}

}

看看How to Use GridLayout了解更多详情

关于Java GUI 3 列边框布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36296870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com