我必须在按钮上按两次才能启动Jchooser:
我有一个处理程序类:
package tekstapplicatie;
import java.awt.event.*;
import java.io.*;
public class Handler implements ActionListener {
public void actionPerformed(ActionEvent arg0)
{
Scherm.openen.addActionListener( new Lezen() );
System.out.println( "Fout bij het lezen of sluiten bestand" );
Scherm.opslaanals.addActionListener( new Opslaanals() );
}
}
当我按下按钮Scherm.openen时,显示的消息用于调试目的,因此我的按钮工作。 但是,下一段代码无法启动。 它只在我再次按下按钮时才会启动。 当我按下按钮时,第二次不显示该消息。
package tekstapplicatie;
import java.awt.event.*;
import java.io.*;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Lezen implements ActionListener
{
public void actionPerformed( ActionEvent arg )
{
// BufferedReader in;
// String regel;
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"TXT & GIF Images", "txt", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
Scherm.gekozenbestand = chooser.getSelectedFile().getName();
Scherm.absoluutpad = chooser.getSelectedFile().getAbsolutePath();
}
String path = Scherm.absoluutpad;
System.out.println(path);
try {
Scherm.in = new BufferedReader( new FileReader( Scherm.absoluutpad ) );
while ( ( Scherm.regel = Scherm.in.readLine() ) != null )
{
System.out.println( Scherm.regel );
System.out.println( Scherm.absoluutpad);
Scherm.tekstveld.append(Scherm.regel);
Scherm.tekstveld.setCaretPosition(Scherm.tekstveld.getDocument().getLength());
}
Scherm.in.close();
}
catch( FileNotFoundException f ) {
System.out.println( "Kan bestand niet vinden" );
}
catch( IOException i ) {
System.out.println( "Fout bij het lezen of sluiten bestand" );
i.printStackTrace();
}
}
}
我做错了什么?