java消息对话框用什么做_Java如何创建消息对话框?
本示例演示如何使用JOptionPane類方法創建消息對話框。在下面的代碼,你會看到使用的JOptionPane.showMessageDialog(),JOptionPane.showInputDialog()和JOptionPane.showConfirmDialog()。package?org.nhooo.example.swing;
import?javax.swing.*;
import?java.awt.*;
import?java.awt.event.ActionListener;
import?java.awt.event.ActionEvent;
public?class?MessageDialogDemo?extends?JFrame?{
public?MessageDialogDemo()?throws?HeadlessException?{
initialize();
}
private?void?initialize()?{
setSize(200,?200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JButton?button1?=?new?JButton("Click?Me!");
button1.addActionListener(new?ActionListener()?{
public?void?actionPerformed(ActionEvent?e)?{
// 顯示帶有文本消息的消息對話框
JOptionPane.showMessageDialog((Component)?e.getSource(),
"Thank?you!");
}
});
JButton?button2?=?new?JButton("What?is?your?name?");
button2.addActionListener(new?ActionListener()?{
public?void?actionPerformed(ActionEvent?e)?{
// 顯示一個輸入對話框,要求您輸入一些文本
String?text?=?JOptionPane.showInputDialog((Component)?e.getSource(),
"What?is?your?name?");
if?(text?!=?null?&&?!text.equals(""))?{
JOptionPane.showMessageDialog((Component)?e.getSource(),
"Hello?"?+?text);
}
}
});
JButton?button3?=?new?JButton("Close?Application");
button3.addActionListener(new?ActionListener()?{
public?void?actionPerformed(ActionEvent?e)?{
// 顯示一個確認對話框,要求輸入是或否
// 按鈕。
int?result?=?JOptionPane.showConfirmDialog((Component)?e.getSource(),
"Are?you?sure?want?to?close?this?application?");
if?(result?==?JOptionPane.YES_OPTION)?{
System.exit(0);
}?else?if?(result?==?JOptionPane.NO_OPTION)?{
// 不執行任何操作,繼續運行應用程序
}
}
});
setLayout(new?FlowLayout(FlowLayout.CENTER));
getContentPane().add(button1);
getContentPane().add(button2);
getContentPane().add(button3);
}
public?static?void?main(String[]?args)?{
SwingUtilities.invokeLater(new?Runnable()?{
public?void?run()?{
new?MessageDialogDemo().setVisible(true);
}
});
}
}
總結
以上是生活随笔為你收集整理的java消息对话框用什么做_Java如何创建消息对话框?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于STM32的高精度频率计设计
- 下一篇: python封装exe如何返回上一步_如