java - Java 容器内的容器

标签 java swing user-interface containers

我有一个具有 BorderLayout 的 Java 程序,目前运行良好。 我想在其下方添加一个 JTextArea,以便在我点击运行按钮后显示输出。

有人可以向我展示完成我想做的事情的最佳方式的例子吗? 我是否应该定义一个 2X1 的 GridLayout,我的 BorderLayout 在上面,然后我的 JTextArea 在下面?

我愿意接受建议...

这是我目前所拥有的图像: enter image description here

在此之下——我想要一个文本区域,在我单击运行按钮后捕获输出。

最佳答案

正如我在评论中所说的那样......像这样的事情怎么样:

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);
            }
        });

    }

}

enter image description here

通过在 JSplitPane 上添加您的 JPanelJTextArea,您的 JTextArea 将扮演某种角色控制台,如果我理解正确的话,就是你的问题的目标。

关于java - Java 容器内的容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13292964/

相关文章:

C# (WPF) : Database Relationship Viewer

c# - 没有 Visual Studio 的 C# 中的 GUI

java - Maven 阴影 : include only specific artifacts and exclude all the rest

java - 获取 JLabel 中具有特定坐标的像素的颜色

java - 如何在Java中创建通用数组?

java - 为什么按下 JButton 后没有任何反应

java - 如何使 JFrame 处于 Activity 状态(聚焦)?

python-2.7 - 向TabWidget添加加号按钮pyqt4

java - Constructor.newInstance() 在 JUnit 中与实际运行时的行为不同

java - 将 Iterable<T> 转换为 T[] 的可重用方法?