我正在使用Java SE开发库存管理系统。我希望系统在库存中的商品数量低于设定的阈值时显示通知。比如,当数据库中的项目数量低于50时。系统应向用户显示通知。 我已经研究过了,但结果并不符合我的需要。
答案 0 :(得分:1)
如果我正确理解了您的问题,您正在寻找可能告知用户发生问题的可能性。 Swing有两种标准可能性:弹出对话框消息(例如here)和system tray messages。
您也可以使用自己的消息服务。例如,您可以为应用程序实现状态栏,并使用它来显示您的消息。 Here是一个简单的例子,如何做到这一点。
答案 1 :(得分:1)
用于通知用户应用程序状态的方法高度依赖于应用程序的可视结构及其运行的平台。如果您的应用程序针对的是桌面环境,并且您正在预测的消息并未提醒用户注意某些重要事项,那么一个小状态栏可能是一个不错的选择。
如果您的目标是屏幕上的空间有限并且需要确保用户看到该消息,那么可能会有一个覆盖主应用程序的对话窗口。下面是覆盖主应用程序框架的对话框的快速而肮脏的示例。
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DialogExample extends JFrame
{
private final static String DIALOG_TITLE = "Warning Dialog";
private final static int DIALOG_ICON = JOptionPane.WARNING_MESSAGE;
private final JButton openPopupBtn;
public DialogExample()
{
this.openPopupBtn = new JButton("Open Dialog");
this.openPopupBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(DialogExample.this, "You just opened a dialog.", DIALOG_TITLE, DIALOG_ICON);
}
});
this.setTitle("Dialog Example");
this.setSize(640,480);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.add(this.openPopupBtn);
this.setResizable(false);
}
public static void main(String[] args) {
JFrame dialogExample = new DialogExample();
dialogExample.setVisible(true);
}
}
如果您喜欢第二种方法,推荐读数为How to Make Dialogs。此页面显示了为这些对话框添加一点丰富功能的能力。