如何将类似窗口的"控制台"添加到GUI?

4 java user-interface swing

我创建了一个GUI.它有几个按钮.我想在它们下面添加一个像对象一样的控制台,我可以在其中编写消息,以便用户可以看到它们.我应该使用什么元素/对象/类?我希望它能够以不同的方式呈现消息.这是我创建GUI的代码:

public ssGUI() {
    setLayout(new BorderLayout());

    bRunNoAdj = new JButton("Run no adjustment");
    bRunNoAdj.setVerticalTextPosition(AbstractButton.CENTER);
    bRunNoAdj.setHorizontalTextPosition(AbstractButton.LEADING);
    bRunNoAdj.setMnemonic(KeyEvent.VK_E);
    bRunNoAdj.addActionListener(this);
    bRunNoAdj.setEnabled(false);
    bRunNoAdj.setBackground(Color.white);

    bRunAdj = new JButton("Run adjustment");
    bRunAdj.setVerticalTextPosition(AbstractButton.CENTER);
    bRunAdj.setHorizontalTextPosition(AbstractButton.LEADING);
    bRunAdj.setMnemonic(KeyEvent.VK_E);
    bRunAdj.addActionListener(this);
    bRunAdj.setEnabled(false);
    bRunAdj.setBackground(Color.white);

    bConnect = new JButton("Connect");
    bConnect.setMnemonic(KeyEvent.VK_E);
    bConnect.addActionListener(this);
    bConnect.setEnabled(true);
    bConnect.setBackground(Color.white);

    bDisconnect = new JButton("Disconnect");
    bDisconnect.setMnemonic(KeyEvent.VK_E);
    bDisconnect.addActionListener(this);
    bDisconnect.setEnabled(false);
    bDisconnect.setBackground(Color.white);

    bStationary = new JButton("Show Stationary");
    bStationary.setMnemonic(KeyEvent.VK_E);
    bStationary.addActionListener(this);
    bStationary.setEnabled(false);
    bStationary.setBackground(Color.white);

    bMoving = new JButton("Show Moving");
    bMoving.setMnemonic(KeyEvent.VK_E);
    bMoving.addActionListener(this);
    bMoving.setEnabled(false);
    bMoving.setBackground(Color.white);

    JPanel topPanel = new JPanel();
    topPanel.add(bConnect);
    topPanel.add(bDisconnect);
    add(topPanel, BorderLayout.NORTH);
    JPanel centerPanel = new JPanel();
    centerPanel.add(bRunNoAdj);
    centerPanel.add(bRunAdj);
    add(centerPanel, BorderLayout.CENTER);
    JPanel bottomPanel = new JPanel();
    bottomPanel.add(bStationary);
    bottomPanel.add(bMoving);
    add(bottomPanel, BorderLayout.SOUTH);
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激,谢谢.

Roa*_*oan 6

最简单的可能是使用JTextArea.

您当然会阻止用户编辑该区域,这可以这样做:

JTextArea area = new JTextArea();
area.setEditable(false);
Run Code Online (Sandbox Code Playgroud)

如果您希望用户能够滚动该区域,您可以将其添加到JScrollPane,如下所示:

JTextArea area = new JTextArea();
JScrollPane scrollableArea = new JScrollPane(area);
Run Code Online (Sandbox Code Playgroud)

最后,您可以通过执行以下操作向该区域添加一行:

area.setText(area.getText() + "\n" + newLine);
Run Code Online (Sandbox Code Playgroud)

或者最好:

area.append("\n" + newLine);
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助 :)

  • 也可以使用附加在setText上,除了更容易,它更有效 (3认同)
  • 是的你可以.除了使用标准的JTextArea构造函数,你还可以使用这个构造函数:`new JTextArea(rows,cols)`(其中rows和cols都是整数`.这样你就可以创建具有初始行数和列数的JTextArea.希望这可以帮助 :) (2认同)

AJN*_*eld 5

用一个JTextArea.请text_area.setEditable(false);它将其设为只读.

  • 您的"控制台"是仅输出窗口; `setEditable(false)`使控件成为只读.用户不应该更改程序显示的消息.当然,您的程序可以添加更多行,或者随意进行任何其他更改. (2认同)