import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
public class MainInterface {
private JFrame frame;
public MainInterface() {
frame = new JFrame("File Selection");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JButton button = new JButton("Select File");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
// Do something with the selected file
System.out.println("Selected file: " + selectedFile.getAbsolutePath());
}
}
});
frame.add(button);
frame.setVisible(true);
}
public static void main(String[] args) throws InterruptedException {
MainInterface mainInterface = new MainInterface();
}
}
05-28
05-18
1549
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-28
09-06