public class MessageWindow { private static JFrame instance = null; public static void showMessage(String message) { if (instance == null) { instance = new JFrame("消息窗口"); instance.add(new JLabel(message)); instance.pack(); instance.setVisible(true); instance.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } else { instance.setVisible(true); } } } 如何用main函数运行
时间: 2024-02-14 17:04:05 浏览: 32
可以在 MessageWindow 类中添加一个 main 函数,通过调用 showMessage 方法来展示消息窗口。代码如下:
```
import javax.swing.*;
public class MessageWindow {
private static JFrame instance = null;
public static void showMessage(String message) {
if (instance == null) {
instance = new JFrame("消息窗口");
instance.add(new JLabel(message));
instance.pack();
instance.setVisible(true);
instance.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
} else {
instance.setVisible(true);
}
}
public static void main(String[] args) {
showMessage("Hello, world!");
}
}
```
在上面的代码中,我们添加了一个 main 函数,并在其中调用了 showMessage 方法来展示消息窗口。当运行该程序时,会弹出一个消息窗口,显示 "Hello, world!"。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![m](https://img-home.csdnimg.cn/images/20210720083646.png)