当我单击 fileItem1 时,fileItem1 是一个 JMenuItem,这就是您如何让它打开一个文件,然后在 JFrame 中显示该文件的名称:
// open file
fileItem1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
Component parent = null;
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
}
jStyledTextPane.setText("You chose to open this file: " + chooser.getSelectedFile().getName());
}
});