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);
}
}
그게 도움이되지 않았을 때, 나는 changeBg 메서드에서 repaint()를 취할 때 JFileChooser가 여전히 똑같은 방식으로 작동합니다. –
아니요, 그는 .addActionListener() 호출을 paintComponent() 메서드가 아닌 생성자로 옮기는 것을 의미합니다. 버튼의 액션 리스너 목록에 'this'의 인스턴스가 점점 더 많이 추가되므로 계속 호출되고 있습니다. 인스턴스 당 한 번만 리스너를 추가하십시오. –
우우, 알았어, 고마워.] –