有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java中容器内的swing容器

我有一个Java程序,它有一个BorderLayout,到目前为止运行良好。 我想在下面添加一个JTextArea,在我点击run按钮后显示out put

有人能给我举个例子,说明实现我想做的事情的最佳方法吗? 我应该定义一个2X1的GridLayout,上面是我的BorderLayout,下面是我的JTextArea

我愿意接受建议

以下是我到目前为止的照片: enter image description here

下面--我想有一个文本区域,一旦我点击run按钮,它就会捕捉输出


共 (3) 个答案

  1. # 1 楼答案

    使用border layout
    将现有GUI添加到panel并将其添加到framecenterborder layoutTextArea

    代码片段:

    frame.setLayout(new BorderLayout());
    frame.add(BorderLayout.CENTER, panel);  //panel== your existing GUI
    frame.add(BorderLayout.SOUTH, textArea);  //new TextArea
    
  2. # 2 楼答案

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

    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

    通过在{}上添加{}和{},你的{}将扮演某种控制台的角色,如果我理解正确的话,这就是你问题的目标

  3. # 3 楼答案

    如果我有这个想法,当你点击跑步按钮时,你想在整个东西下面添加一个文本区域

    我建议使用两个面板。把你所有的东西都放在一个面板中(这个面板会有borderLayout),然后把另一个面板放在下面,当你点击Run按钮时,你可以通过编程的方式添加JtextArea

    编辑
    比如:

    Window
    |
    | - Panel  (borderLayout)
    |      |  - (things you already have)
    |
    | - Panel 2
         |    (JTextArea, added later by code)