1

わかりました、なぜこれが起こっているのかわかりませんが、閉じるボタンを押しても joptionpane は閉じません。ポップアップが繰り返し表示され、閉じるには複数回クリックする必要があります。

ここでコードスナップショット

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));
    }
}

編集:以前に間違ったコードを投稿しました。

4

1 に答える 1

7

まず第一に、最小限のコード サンプルを投稿することは常に良い考えです。特に、多くの場合、自分で間違いを見つけることになるためです。

しかし、今回はかなり明白に見えます:

コンストラクターに mouseListener を追加する必要があり、呼び出されるたびに追加する必要はありませんpaintComponent。それ以外の場合は、paintComponent メソッドが呼び出されるたびに新しいリスナーを追加します。これは、swing コンポーネントにはかなりの量になる可能性があります (また、JOptionPane はおそらくパネルの一部を非表示にするため、[OK] をクリックすると、新しい paintComponent 呼び出しが生成されます。そこに無限ループ)。

于 2011-09-04T18:26:55.257 に答える