我有一个具有 BorderLayout
的 Java 程序,目前运行良好。
我想在其下方添加一个 JTextArea
,以便在我点击运行按钮后显示输出。
有人可以向我展示完成我想做的事情的最佳方式的例子吗?
我是否应该定义一个 2X1 的 GridLayout
,我的 BorderLayout
在上面,然后我的 JTextArea
在下面?
我愿意接受建议...
这是我目前所拥有的图像:
在此之下——我想要一个文本区域,在我单击运行按钮后捕获输出。
最佳答案
正如我在评论中所说的那样......像这样的事情怎么样:
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Ironmantis extends JFrame{
JTextArea textArea = new JTextArea();
JPanel upperPanel = new JPanel();
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,upperPanel,new JScrollPane(textArea));
public Ironmantis(){
textArea.setEditable(false);
textArea.append("Operating system: "+System.getProperty("os.name")+"\n");
textArea.append("Operating System version: " +System.getProperty("os.version")+"\n");
textArea.append("Operating system architecture: "+ System.getProperty("os.arch")+"\n");
textArea.append("Java version: "+ System.getProperty("java.version")+"\n");
textArea.append("Java vendor: "+ System.getProperty("java.vendor")+"\n");
textArea.append("Java vendor URL: "+ System.getProperty("java.vendor.url")+"\n");
upperPanel.setPreferredSize(new Dimension(800,400));
upperPanel.setBackground(Color.CYAN);
splitPane.setPreferredSize(new Dimension(800,600));
add(splitPane);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable(){
public void run(){
Ironmantis i = new Ironmantis();
i.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
i.pack();
i.setVisible(true);
}
});
}
}
通过在 JSplitPane
上添加您的 JPanel
和 JTextArea
,您的 JTextArea
将扮演某种角色控制台,如果我理解正确的话,就是你的问题的目标。
关于java - Java 容器内的容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13292964/