クラス間で JFileChooser によって生成された文字列を渡そうとしています。私のプログラムの他の部分は正常に動作します。ファイルパスを文字列としてローカルに定義すると、うまく動作します。
このようなコードを実装する必要があると思います.
public class A {
private static final String x = "This is X";
public static String getX() { return x;}
}
public class B {
public static void main(String args[]) {
String x = A.get();
System.out.println("x = " + x);}
}
私の完全なコード:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class FileChooser extends JFrame {
public FileChooser() {
super("File Chooser Test Frame");
setSize(350, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
JButton openButton = new JButton("Open");
JButton goButton = new JButton("Go");
final JLabel statusbar = new JLabel(
"Output of your selection will go here");
openButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
int option = chooser.showOpenDialog(FileChooser.this);
if (option == JFileChooser.APPROVE_OPTION) {
File[] sf = chooser.getSelectedFiles();
String filelist = "nothing";
if (sf.length > 0)
filelist = sf[0].getName();
for (int i = 1; i < sf.length; i++) {
filelist += ", " + sf[i].getName();
}
statusbar.setText(filelist);
String thefilename = filelist;
} else {
statusbar.setText("You canceled.");
}
}
});
goButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String filepath = statusbar.getText();
System.out.println(filepath);
}
});
c.add(openButton);
c.add(goButton);
c.add(statusbar);
}
public static void main(String args[]) {
FileChooser sfc = new FileChooser();
sfc.setVisible(true);
}
}