好的,我不知道为什么会这样,但是当我按下关闭按钮时,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));
}
}
已编辑:之前发布了错误的代码。