我创建了一个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)
任何帮助将不胜感激,谢谢.
最简单的可能是使用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)
我希望这有帮助 :)
归档时间: |
|
查看次数: |
385 次 |
最近记录: |