我可以使用以下命令在JFileChooser窗口中设置默认文件名:
fileChooser.setSelectedFile();
我想知道是否也可以选择它,所以如果你想将文件另存为其他东西,你可以立即开始改写它。感谢您的帮助。
package filetest;
import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
@SuppressWarnings("serial")
class Editor {
public static class TextClass extends JTextArea {
FileClass fileClass = new FileClass();
public void setKeyboardShortcuts() {
fileClass.setKeyboardShortcuts();
}
private class FileClass {
private File directory;
private String filepath = "";
private String filename = "";
private void setKeyboardShortcuts() {
Action ctrlo = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
try {
openFile();
} catch (UnsupportedEncodingException e1) {
}
}
};
getInputMap().put(KeyStroke.getKeyStroke("ctrl O"), "ctrlo");
getActionMap().put("ctrlo", ctrlo);
Action ctrls = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
try {
saveFile();
} catch (UnsupportedEncodingException e1) {
}
}
};
getInputMap().put(KeyStroke.getKeyStroke("ctrl S"), "ctrls");
getActionMap().put("ctrls", ctrls);
}
private String selectFile(String fileaction) throws FileNotFoundException {
JFileChooser filechooser = new JFileChooser();
if (directory != null) {
filechooser.setCurrentDirectory(directory);
} else {
filechooser.setCurrentDirectory(new File("."));
}
filechooser.setSelectedFile(new File(filepath));
int r = 0;
if (fileaction.equals("openfile"))
r = filechooser.showDialog(new JPanel(), "Open file");
else
r = filechooser.showDialog(new JPanel(), "Save file");
if (r == JFileChooser.APPROVE_OPTION) {
try {
directory = filechooser.getSelectedFile().getParentFile();
filename = filechooser.getSelectedFile().getName();
return filename;
} catch (Exception exception) {
return "";
}
} else {
return "";
}
}
private void openFile() throws UnsupportedEncodingException {
try {
String filestr = selectFile("openfile");
if (filestr.equals(""))
return;
else
filepath = filestr;
} catch (FileNotFoundException ex) {
Logger.getLogger(Editor.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void saveFile() throws UnsupportedEncodingException {
try {
String filestr = selectFile("savefile");
if (filestr.equals(""))
return;
else
filepath = filestr;
} catch (FileNotFoundException ex) {
Logger.getLogger(Editor.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Editor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Editor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Editor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Editor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
createAndShowGui();
}
});
}
private static void createAndShowGui() {
JFrame frame = new JFrame();
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
JTextArea textArea = new TextClass();
frame.add(textArea);
((TextClass) textArea).setKeyboardShortcuts();
frame.setVisible(true);
}
}
答案 0 :(得分:0)
默认情况下,我在输入的机器上执行此操作:
package stackoverflow;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
*
* @author ub
*/
public class StackOverflow
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Images", "jpg", "gif","png");
chooser.setFileFilter(filter);
chooser.setSelectedFile(new File("C:\\Users\\ub\\Pictures\\Capt.PNG"));
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION)
System.out.println("You chose to open this file: "+chooser.getSelectedFile().getName());
}
}
答案 1 :(得分:0)
这是因为当您第一次致电.setSelectedFile
时,您的filepath
是一个空字符串。
在向用户显示文件选择器后,您设置了filepath
变量。
如果您在调用filepath
之前打印以控制字符串值.showDialog
,则应该看到此内容。
答案 2 :(得分:0)
问题似乎是由以下几行造成的:
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Editor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Editor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Editor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Editor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
如果删除它们,文件名会突出显示,如Unai Vivi的例子。