如何在JAVA消息弹出框中添加图片的问题

我在用JAVA进行编程的时候遇到了一个问题:使用一下代码时可产生一个消息框:JOptionPane.showMessageDialog(null,"您获得了一等奖!");... 我在用JAVA进行编程的时候遇到了一个问题:使用一下代码时可产生一个消息框:JOptionPane.showMessageDialog(null,"您获得了一等奖!");
但是如何在这个弹出窗口中添加图片或者随机添加一些图片并显示出来呢,请高手指教一下,我会把积分全送上
展开
 我来答
psychic0111
2008-05-27 · TA获得超过999个赞
知道小有建树答主
回答量:463
采纳率:100%
帮助的人:220万
展开全部
这是我做的一网吧管理系统中的一个类,我所有的弹出对话框都用的这个
是初学JAVA编的,希望对你有所帮助

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.border.*;

public class WarningDialog extends MainFrame {
JDialog warning;
Font f=new Font("Frame",Font.PLAIN,16);
Font fb=new Font("Frame",Font.PLAIN,12);
JFrame frame=null;
Container c;
Cursor cursor=new Cursor(Cursor.HAND_CURSOR);
Border low=BorderFactory.createLoweredBevelBorder();
//JButton btn_normal;
//JButton btn_card;
JButton btn;
JButton btn_cancel;
JLabel lbl_message;

final JLabel lbl_icon=new JLabel();
final JTextField _id=new JTextField();
final JComboBox box_type=new JComboBox();
final StringBuffer cptid=new StringBuffer("");

public WarningDialog(JFrame frm,String str) {
this.frame=frm;
warning=new JDialog(frm,"警告信息",true);

warning.setUndecorated(true);
warning.getRootPane().setWindowDecorationStyle(JRootPane.WARNING_DIALOG);
c=warning.getContentPane();
c.setLayout(null);
ImageIcon icon_lbl=new ImageIcon("e:\\netbar\\icon\\0008.gif");
ImageIcon icon_btn=new ImageIcon("e:\\netbar\\icon\\a0007.gif");
lbl_message=new JLabel(str);
lbl_icon.setIcon(icon_lbl);
btn=new JButton("");
btn.setFont(new Font("Frame",Font.PLAIN,12));
btn.setIcon(icon_btn);
btn.setCursor(cursor);
btn.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
warning.dispose();
}
}
);

lbl_message.setFont(f);
lbl_message.setBounds(new Rectangle(105,25,160,40));
lbl_icon.setBounds(50,25,40,40);
btn.setBounds(new Rectangle(115,75,70,33));
c.add(lbl_message);
c.add(lbl_icon);
c.add(btn);
warning.setBounds((1024-300)/2,150,300,150);
warning.show();
}
public WarningDialog(String str){
//this.frame=frm;
warning=new JDialog(this,"确认信息",true);

warning.setUndecorated(true);
warning.getRootPane().setWindowDecorationStyle(JRootPane.QUESTION_DIALOG);
c=warning.getContentPane();
c.setLayout(null);
ImageIcon icon_lbl=new ImageIcon("e:\\netbar\\icon\\qq0026.gif");
ImageIcon icon_btn=new ImageIcon("e:\\netbar\\icon\\0005.gif");
ImageIcon icon_cancel=new ImageIcon("e:\\netbar\\icon\\0007.gif");
lbl_message=new JLabel(str);
lbl_icon.setIcon(icon_lbl);

btn=new JButton("");
btn.setIcon(icon_btn);
btn.setCursor(cursor);

btn.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
);
btn_cancel=new JButton("");
btn_cancel.setIcon(icon_cancel);
btn_cancel.setCursor(cursor);

btn_cancel.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent f){
warning.dispose();
}
}
);
lbl_message.setFont(f);
lbl_message.setBounds(new Rectangle(105,25,120,40));
lbl_icon.setBounds(50,25,40,40);
btn.setBounds(new Rectangle(85,75,70,30));
btn_cancel.setBounds(new Rectangle(160,75,70,30));
c.add(lbl_message);
c.add(lbl_icon);
c.add(btn);
c.add(btn_cancel);
warning.setBounds((1024-300)/2,150,300,150);
warning.show();
}
public WarningDialog(JFrame frm,String cardid,String cpt_id,String []type){
this.frame=frm;
warning = new JDialog(this, "上机对话框", true);
cptid.append(cpt_id);

String []tp=new String[2];
tp=type;

JLabel lbl_type = new JLabel("上机类型:");
box_type.addItem(tp[0]);
box_type.addItem(tp[1]);
JLabel lbl_card=new JLabel("输入卡号");
_id.setText(cardid);
_id.setBorder(low);

warning.setUndecorated(true);
warning.getRootPane().setWindowDecorationStyle(JRootPane.COLOR_CHOOSER_DIALOG);
c = warning.getContentPane();
this.getContentPane().setLayout(null);
Icon icon_lbl = new ImageIcon("e:\\netbar\\icon\\0004.gif");
Icon icon_normal = new ImageIcon("e:\\netbar\\icon\\073115413830268.gif");
Icon icon_cancel=new ImageIcon("e:\\netbar\\icon\\073115414187388.gif");

lbl_icon.setIcon(icon_lbl);
box_type.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(box_type.getSelectedItem().equals("会员上机")){
_id.setEnabled(true);
_id.setText("");
_id.requestFocus(true);
lbl_icon.setVisible(true);
}else{
_id.setEnabled(false);
_id.setText("noCard");
lbl_icon.setVisible(false);
}
}
}
);

lbl_type.setFont(fb);
lbl_type.setBounds(new Rectangle(30,20,100,30));

box_type.setFont(fb);
box_type.setBounds(new Rectangle(110,20,100,30));

lbl_card.setFont(fb);
lbl_card.setBounds(new Rectangle(30,60,100,30));

_id.setText(cardid);
_id.selectAll();
_id.setBounds(new Rectangle(110,60,100,30));

btn=new JButton("确认上机");
btn.setFont(fb);
btn.setIcon(icon_normal);
btn.setCursor(cursor);

//btn_card=new JButton("会员上机");
//btn_card.setFont(fb);
//btn_card.setIcon(icon_card);
//btn_card.setCursor(cursor);

btn_cancel=new JButton("取消");
btn_cancel.setFont(fb);
btn_cancel.setIcon(icon_cancel);
btn_cancel.setCursor(cursor);

lbl_icon.setBounds(new Rectangle(215,60,50,30));

btn.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
Connecter con = new Connecter();
if(box_type.getSelectedItem().equals("普通上机")){
con.updateRecord(cptid.toString());
dispose();
}else if(con.isCard(_id.getText())){
con.updateRecord(cptid.toString(),_id.getText());
dispose();
}else{
new WarningDialog(frame,"该会员卡不存在");
}
}
}
);

btn_cancel.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent f) {
dispose();
}
}
);

btn.setBounds(new Rectangle(30,110,110,30));
btn_cancel.setBounds(new Rectangle(155,110,110,30));
this.getContentPane().add(btn);
this.getContentPane().add(btn_cancel);
this.getContentPane().add(lbl_type);
this.getContentPane().add(box_type);
this.getContentPane().add(lbl_card);
this.getContentPane().add(_id);
this.getContentPane().add(lbl_icon);
_id.setEnabled(false);
lbl_icon.setVisible(false);

this.setBounds(200,150, 295, 200);
this.show();
}
public WarningDialog(JFrame frm,String cardid,String cpt_id,float[][]pay){
this.frame=frm;
warning = new JDialog(this, "下机对话框", true);
final Connecter con1 = new Connecter();
final String cid=cardid;
final String cptid=cpt_id;
final float [][]payinfo=pay;

warning.setUndecorated(true);
warning.getRootPane().setWindowDecorationStyle(JRootPane.COLOR_CHOOSER_DIALOG);
c = warning.getContentPane();
this.getContentPane().setLayout(null);

Icon icon_lbl = new ImageIcon("e:\\netbar\\icon\\0004.gif");
Icon icon_normal = new ImageIcon("e:\\netbar\\icon\\073115413830268.gif");
Icon icon_card = new ImageIcon("e:\\netbar\\icon\\073115413944451.gif");
Icon icon_cancel=new ImageIcon("e:\\netbar\\icon\\073115414187388.gif");

btn=new JButton("确认下机");
btn.setFont(fb);
btn.setIcon(icon_normal);
btn.setCursor(cursor);

btn_cancel=new JButton("取消");
btn_cancel.setFont(fb);
btn_cancel.setIcon(icon_cancel);
btn_cancel.setCursor(cursor);

JLabel lbl_type = new JLabel("请选择您的结帐方式:");

final JRadioButton rb_cash=new JRadioButton("现金支付",true);
final JRadioButton rb_card=new JRadioButton("会员卡支付",false);
ButtonGroup group=new ButtonGroup();
group.add(rb_cash);
group.add(rb_card);

lbl_type.setFont(fb);

lbl_type.setBounds(new Rectangle(30,55,150,30));
btn.setBounds(new Rectangle(30,120,110,30));
btn_cancel.setBounds(new Rectangle(155,120,110,30));
rb_cash.setBounds(30,85,110,30);
rb_card.setBounds(145,85,110,30);
rb_cash.setFont(fb);
rb_card.setFont(fb);

float sum[] = new float[4];
if(cid.equals("noCard")){
rb_card.setEnabled(false);
sum=con1.handInTwo(payinfo,cptid);
String str1=String.valueOf(sum[1]);
String str2=str1.substring(0,str1.indexOf(".")+2);
lbl_message=new JLabel("您的上机费用为:"+str2+"元,"+"其中附加费为:"+String.valueOf(sum[3])+"元.");
}else{
sum=con1.handInCard(payinfo,cptid,cid);
String str1=String.valueOf(sum[1]);
String str2=str1.substring(0,str1.indexOf(".")+2);
lbl_message=new JLabel("您的上机费用为:"+str2+"元,"+"其中附加费为:"+String.valueOf(sum[3])+"元.");
}
lbl_message.setFont(fb);
lbl_message.setBounds(new Rectangle(30,20,235,30));

btn.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent h){
float rst[] = new float[4];
if(rb_cash.isSelected()){
rst=con1.handInTwo(payinfo,cptid);
dispose();
}else if(rb_card.isSelected()){
rst=con1.handInCard(payinfo,cptid,cid);
dispose();
}
}
}
);

btn_cancel.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent i){
dispose();
}
}
);

this.getContentPane().add(lbl_message);
this.getContentPane().add(lbl_type);
this.getContentPane().add(btn);
this.getContentPane().add(btn_cancel);
this.getContentPane().add(rb_cash);
this.getContentPane().add(rb_card);

this.setBounds(200,150, 295, 200);
this.show();
}
public WarningDialog(String str,boolean flag){
//this.frame=frm;
warning=new JDialog(this,"锁屏窗口",flag);

warning.setUndecorated(true);
warning.getRootPane().setWindowDecorationStyle(JRootPane.QUESTION_DIALOG);
c=warning.getContentPane();
c.setLayout(null);
ImageIcon icon_lbl=new ImageIcon("e:\\netbar\\icon\\0031.gif");
lbl_icon.setIcon(icon_lbl);

_id.setText(str);
_id.setBorder(low);

_id.setFont(f);
_id.setBounds(new Rectangle(110,25,120,40));
lbl_icon.setBounds(30,25,70,70);
c.add(_id);
c.add(lbl_icon);

_id.addKeyListener(
new KeyAdapter(){
public void keyPressed(KeyEvent evt) {
Connecter connect = new Connecter();

if (evt.getKeyCode() == evt.VK_ENTER) {

String str2 = _id.getText();
if(connect.whoLogin(str2)){
warning.dispose();
}else{
_id.setText("继续输入");
}
evt.setKeyCode(0);
}
}
}
);

warning.setBounds((1024-300)/2,(768-150)/2,300,150);
warning.show();
}
public WarningDialog(JFrame frm,Vector vcr,String cpt){
this.frame=frm;
final Vector v=vcr;
warning=new JDialog(this,"销售窗口",true);

warning.setUndecorated(true);
warning.getRootPane().setWindowDecorationStyle(JRootPane.FILE_CHOOSER_DIALOG);
c=this.getContentPane();
c.setLayout(null);

final JComboBox ware_name=new JComboBox();
for(int i=0;i<v.size();i++){
if(i%2==0){
ware_name.addItem(v.elementAt(i));
}
}
ware_name.addItem("选择商品");
ware_name.setSelectedItem("选择商品");

JLabel ware=new JLabel("选择商品");
ware.setFont(fb);
ware_name.setFont(fb);
ware.setBounds(new Rectangle(30,30,90,30));
ware_name.setBounds(new Rectangle(125,30,85,30));

lbl_message=new JLabel("");
lbl_message.setBounds(new Rectangle(215,30,55,30));

ware_name.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent e){
lbl_message.setText(v.elementAt(v.indexOf(e.getItem())+1).toString()+"元");
}
}
);

ImageIcon icon_btn=new ImageIcon("e:\\netbar\\icon\\073115413830268.gif");
ImageIcon icon_cancel=new ImageIcon("e:\\netbar\\icon\\073115413532761.gif");
btn=new JButton("确定");
btn_cancel=new JButton("取消");
btn.setFont(fb);
btn_cancel.setFont(fb);
btn.setIcon(icon_btn);
btn_cancel.setIcon(icon_cancel);
btn.setBounds(new Rectangle(30,75,85,30));
btn_cancel.setBounds(new Rectangle(125,75,85,30));

btn.setCursor(cursor);
btn_cancel.setCursor(cursor);

final String cptid=cpt;
btn.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
Connecter con=new Connecter();
String lbl=lbl_message.getText();
float cost=Float.parseFloat(lbl.substring(0,lbl.indexOf(".")+1));
con.costAdd(cost,cptid);
dispose();
}
}
);

btn_cancel.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
}
);

c.add(btn);
c.add(btn_cancel);
c.add(ware);
c.add(ware_name);
c.add(lbl_message);

this.setSize(310,180);
this.show();
}
}
seekfor_
2008-05-27 · TA获得超过371个赞
知道小有建树答主
回答量:170
采纳率:66%
帮助的人:181万
展开全部
你想要在什么地方添加图片呢?
是作为图标还是作为背景什么的?
如果是作为图标那你就在
JOptionPane的构造函数里的Icon参数里载入一个图标就行。
如果你想作为背景什么的,就得重新构造一个JoptionPane,挺麻烦的,有需要给我留言,我再给你写例子。
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式