一、只弹出提示信息
JOptionPane.showMessageDialog(null, "问题不能为空","格式错误",JOptionPane.ERROR_MESSAGE);
二、可以选择确认或取消
程序:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class TestJoptionPane {
public static void main(String[] args)
{
TestJoptionPane main1 = new TestJoptionPane();
main1.go();
}
void go()
{
JFrame frame = new JFrame("你好世界");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("点我测试");
button.addActionListener(new buttonListener());
JTextArea textArea = new JTextArea(10,20);
JPanel mainpanel = new JPanel();
mainpanel.add(button);
frame.getContentPane().add(BorderLayout.CENTER,mainpanel);
frame.setSize(450,550);
frame.setVisible(true);
}
class buttonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
int res = JOptionPane.showConfirmDialog(null,"是否继续","我要提示你",
JOptionPane.YES_NO_OPTION);
if (res == JOptionPane.YES_OPTION)
{
System.out.println("点击了YES");
}
else
{
System.out.println("点击了NO");
}
}
}
}
对话框:
输出结果:
点击了YES