2009-08-13 59 views
2

開放當我在我的項目,我的一切工作使用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); 
    } 
} 

回答

3

很簡單,因爲你每重繪添加新的動作偵聽器。 畫圖方法僅適用於噴漆,沒有別的。你必須重新考慮你的策略。

+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

相關問題