建立一个输出信息的java类窗口
时间: 2024-03-09 18:44:57 浏览: 26
可以使用Java的Swing库来建立一个输出信息的窗口。下面是一个简单的示例代码:
```java
import javax.swing.*;
public class OutputWindow {
private JFrame frame;
private JTextArea textArea;
public OutputWindow() {
frame = new JFrame("Output Window");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textArea = new JTextArea();
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
frame.getContentPane().add(scrollPane);
frame.setVisible(true);
}
public void appendText(String text) {
textArea.append(text + "\n");
}
public static void main(String[] args) {
OutputWindow window = new OutputWindow();
// example usage
window.appendText("Hello, world!");
}
}
```
这个类建立了一个窗口,其中包含一个只读的文本区域,可以通过`appendText`方法向其中添加新的文本行。在`main`方法中,可以看到如何使用这个类来输出一行文本。