2009-08-13 20 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メソッドからrepaint()を取るとき、JFileChooserは同じ正確に離れて動作します。 –

+2

いいえ、paintComponent()メソッドではなくコンストラクタへの.addActionListener()呼び出しの移動を意味します。ボタンのアクションリスナーリストに「this」のインスタンスを追加し続けるので、それらは順番に呼び出されます。インスタンスごとに一度だけリスナーを追加します。 –

+0

よ、わかりました、ありがとうございました=] –

関連する問題