要使用对话框首先需要导入对话框的依赖库.不然找不到相应API。
主要通过NotifyDescriptor提供的Confirmation, Message, InputLine 3个内部类来实现确认框,消息框,简单的输入框。
1.Confirmation
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
NotifyDescriptor nd = new NotifyDescriptor.Confirmation("测试", NotifyDescriptor.YES_NO_OPTION);
Object result = DialogDisplayer.getDefault().notify(nd);
if (NotifyDescriptor.OK_OPTION == result) {
} else {
}
2.Message
String msg = "test...";
NotifyDescriptor nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.INFORMATION_MESSAGE);
DialogDisplayer.getDefault().notify(nd);
3.InputLine
NotifyDescriptor.InputLine input = new NotifyDescriptor.InputLine(txt, title);
input.setInputText("John Doe"); // specify a default name
Object result = DialogDisplayer.getDefault().notify(input);
if (result != NotifyDescriptor.OK_OPTION) {
return;
}
String userInput = input.getInputText();