的Java JFileChooser的重新开放点击在选择对话框

问题描述:

开放当我在我的项目,我的一切工作使用JFileChooser后工作就像它应该有除了一个完全没有问题的工作。的Java JFileChooser的重新开放点击在选择对话框

当你点击它改变了我的背景,然后JFileChooser对话框再次打开的对话框中的“打开”。任何人都可以告诉我我需要做些什么才能避免这种情况发生?

这里的一切我下面的源..


import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 
import java.net.*; 
import java.util.*; 
import org.w3c.dom.*; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 

public class COS extends JPanel implements ActionListener{ 
    static JFrame f=new JFrame(); 
    static Image bgImage=null; 
    static String message=""; 
    JButton chbg=new JButton("change background"); 
    public COS(){ 
    } 
    public void paintComponent(Graphics g){ 
     if(bgImage!=null){ 
      g.drawImage(bgImage,0,0,this); 
      chbg.setBounds(10,10,150,25); 
      chbg.addActionListener(this); 
      add(chbg); 
     } 
     else{ 
      g.drawString(message,40,40); 
     } 
    } 
    public static void loadbg(){ 
     try{ 
      String xmlpath="background.xml"; 
      DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); 
      try{ 
       String fimg=""; 
       DocumentBuilder db=dbf.newDocumentBuilder(); 
       Document dom=db.parse(xmlpath); 
       dom.getDocumentElement().normalize(); 
       NodeList ndlst=dom.getElementsByTagName("background"); 
       Node firstnd=ndlst.item(0); 
       if(firstnd.getNodeType()==Node.ELEMENT_NODE){ 
        Element firstele=(Element)firstnd; 
        NodeList firstnamenodelist=firstele.getElementsByTagName("bgimage"); 
        Element firstnamele=(Element)firstnamenodelist.item(0); 
        NodeList firstname=firstnamele.getChildNodes(); 
        fimg=((Node) firstname.item(0)).getNodeValue(); 
       } 
       getFileImage(fimg); 
      } catch(Exception e){ 
      } 
     } catch(Exception e){ 
      message="File load failed: "+e.getMessage(); 
     } 
    } 
    public static void getFileImage(String filein) throws IOException, InterruptedException{ 
     FileInputStream in=new FileInputStream(filein); 
     byte[] b=new byte[in.available()]; 
     in.read(b); 
     in.close(); 
     bgImage=Toolkit.getDefaultToolkit().createImage(b); 
    } 
    public void actionPerformed(ActionEvent e){ 
     Object source=e.getSource(); 
     JFileChooser jfc=new JFileChooser(); 
     if(source==chbg){ 
      int returnVal=jfc.showOpenDialog(null); 
      if(returnVal==JFileChooser.APPROVE_OPTION){ 
       File file=jfc.getSelectedFile(); 
       String fileone=file.getName(); 
       changebg(fileone); 
      } 
     } 
    } 
    public void changebg(String filein){ 
     try{ 
      getFileImage(filein); 
      saveDefaultImage(filein); 
      repaint(); 

     } catch(IOException e){ 
     } catch(InterruptedException ie){ 
     } 
    } 
    public void saveDefaultImage(String filein){ 
     String newdefbg=filein; 
     //don't mind this method, i am still working on it... 
    } 
    public static void main(String[] args){ 
     COS newcos=new COS(); 
     loadbg(); 
     f.setSize(825,640); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.getContentPane().setLayout(null); 
     newcos.setBounds(5,5,800,600); 
     f.setLocation(10,5); 
     f.getContentPane().add(newcos); 
     f.setVisible(true); 
    } 
} 

很简单,因为你每重绘添加新的动作侦听器。 画图方法仅适用于喷漆,没有别的。你必须重新考虑你的策略。

+0

没有帮助,当我需要重绘()出从changebg方法JFileChooser中仍然起到完全相​​同的路程。 – 2009-08-13 16:34:31

+2

不,他表示移动.addActionListener()调用构造函数,而不是的paintComponent()方法。您不断添加的“这”越来越多的情况下,该按钮的动作监听器列表,所以他们被称为顺序。每个实例只能添加一次监听器。 – 2009-08-13 16:40:08

+0

oooh,我明白了,thankyou =] – 2009-08-13 16:42:41