JOptionPane不会关闭

时间:2011-09-04 18:18:22

标签: java swing joptionpane

好吧,我不知道为什么会发生这种情况但是当我按下关闭按钮时,joptionpane将不会关闭。它不断弹出,我需要多次点击才能关闭它。

这里代码snaphot

Point p;
    p   = onScreenLocation(0.134,0.019, eastlake);
    btn.setBounds(p.x,p.y,128,96);
    btn.setContentAreaFilled(false);
    btn.setBorderPainted(false);
    add(btn);

    btn.addMouseListener(new MouseAdapter(){
        public void mouseClicked(MouseEvent me){
        Object[] options = {"View Info","View Place","Close"};
        Object[] choice ={"Close"};
        int response = JOptionPane.showOptionDialog(null,"Apartment Area","Message",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,ResidentImage,options,"Close");
        if(response == 0 ){
            JOptionPane.showOptionDialog(null, "Apartment Eastlake \n" +
                    "provides students with conducive room and reasonable prices ", "Message", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,ResidentImage,choice,"Close");
        }
        else if(response == 1){
            JFrame ImageBox = new JFrame();
            ImageBox.setSize(300,400);
            ImageBox.add(new JLabel(ResidentImageView,SwingConstants.CENTER));
            ImageBox.setVisible(true);
        }
        else{

        }   

        }

    });

完整代码

package environment;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;


public class Map extends JPanel{

    String time = null;
    serializedObject[] humans2 = null;
    serializedDatas input = new serializedDatas();
    Image eastlake,otherImage, buddyImage, clientImage, EventImage;
    String[] buddyList;
    String clientName;
    JFrame frame;

    ClassLoader cl = this.getClass().getClassLoader();
    ImageIcon TransparentImage = new ImageIcon(cl.getResource("image1/bnt1.png"));

    ImageIcon TescoImageView = new ImageIcon(cl.getResource("image1/tesco.jpg"));
    ImageIcon TescoImage = new ImageIcon(cl.getResource("image1/Tesco.png"));



    JButton btn = new JButton(TransparentImage);

    public Map(Image map, Image agent, Image buddy, Image other, String clientName){

        eastlake = map;
        clientImage = agent;
        otherImage = other;
        buddyImage = buddy;
        this.clientName = clientName;
    }

    protected void  paintComponent(Graphics g)
    {
        super.paintComponents(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(eastlake,0,0,null);
        this.setPreferredSize(new Dimension(2624,1696));


        Font font = new Font("Helvetica", Font.BOLD,  12);

        g2d.setFont(font);
        g2d.setColor(Color.red);
        if(time != null)
            g2d.drawString(time, 10, 10);

        Point p;
        p   = onScreenLocation(0.134,0.019, eastlake);
        btn.setBounds(p.x,p.y,128,96);
        btn.setContentAreaFilled(false);
        btn.setBorderPainted(false);
        add(btn);

        btn.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent me){
            Object[] options = {"View Info","View Place","Close"};
            Object[] choice ={"Close"};
            int response = JOptionPane.showOptionDialog(null,"Apartment Area","Message",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,ResidentImage,options,"Close");
            if(response == 0 ){
                JOptionPane.showOptionDialog(null, "Apartment Eastlake \n" +
                        "provides students with conducive room and reasonable prices ", "Message", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,ResidentImage,choice,"Close");
            }
            else if(response == 1){
                JFrame ImageBox = new JFrame();
                ImageBox.setSize(300,400);
                ImageBox.add(new JLabel(ResidentImageView,SwingConstants.CENTER));
                ImageBox.setVisible(true);
            }
            else{

            }   

            }

        });

    }

    public void setTime(String time2) {
        // TODO Auto-generated method stub
        this.time = time2;
    }

    public void setBuddyList(String[] buddyList2) {
        // TODO Auto-generated method stub
        this.buddyList = buddyList2;
    }

    protected Point onScreenLocation(double x, double y, Image img)
    {
        return new Point((int)(img.getWidth(null)*x),(int)(img.getHeight(null)*y));
    }
}

已编辑:之前发布了错误的代码。

1 个答案:

答案 0 :(得分:7)

首先,发布一个最小的代码示例总是一个好主意 - 特别是因为在很多情况下你会自己发现错误。

但这一次似乎相当明显:

您应该在构造函数中添加mouseListener,而不是每次调用paintComponent时都是如此。否则,每次调用paintComponent方法时都会添加一个新的侦听器,这对于swing组件来说可能相当多(因为JOptionPane可能隐藏了面板的一部分,单击ok将生成一个新的paintComponent调用,这意味着你有一个无尽的循环)。