public class JFileChooser extends JComponentimplements AccessibleJFileChooser provides a simple mechanism for the user to choose a file. For information about using JFileChooser, see How to Use File Choosers, a section in The Java Tutorial. The following code pops up a file chooser for the user's home directory that sees only .jpg and .gif images: JFileChooser chooser = new JFileChooser(); // Note: source for ExampleFileFilter can be found in FileChooserDemo, // under the demo/jfc directory in the JDK. ExampleFileFilter filter = new ExampleFileFilter(); filter.addExtension("jpg"); filter.addExtension("gif"); filter.setDescription("JPG & GIF Images"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(parent); if(returnVal == JFileChooser.APPROVE_OPTION) { System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName()); }
// 打开文件的方法 public void openFileMethod() { if (!(text.getText().equals(""))) { if (!(text.getText().equals(textBuffer.getText()))) { int value = this.YES_NO_CANCEL_DIALOG(); if (value == JOptionPane.YES_OPTION){ this.saveMethod(); text.setText(""); } else if (value == JOptionPane.CENTER_ALIGNMENT) { text.setText(""); } else { } } } dialog = new FileDialog(notepad, "打开", FileDialog.LOAD); dialog.setVisible(true); try { FileReader fr = new FileReader(dialog.getDirectory() + dialog.getFile()); BufferedReader br = new BufferedReader(fr); String str = ""; while ((str = br.readLine()) != null) { text.append(str + '\n'); } notepad.setFilePath(dialog.getDirectory() + dialog.getFile()); notepad .setTitle(notepad.getFilePath() + " - " + notepad.getTitle()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } 这是我写的一个“打开”功能的方法,但还是有很多BUG,比如说要实现文件有改变时弹出的提示框,我们点取消后却不能实现应该实现的功能,希望高手们能帮忙解决下!
上面有个地方错了,更改一下!(声明一下,偶是用eclipse写的,然后直接贴进去,可能会导致大家看的时候不方便,实在抱歉! // 打开文件的方法 public void openFileMethod() { if (!(text.getText().equals(""))) { if (!(text.getText().equals(textBuffer.getText()))) { int value = this.YES_NO_CANCEL_DIALOG(); if (value == JOptionPane.YES_OPTION){ this.saveMethod(); text.setText(""); } else if (value == JOptionPane.CANCEL_OPTION) { text.setText(""); } else { } } } dialog = new FileDialog(notepad, "打开", FileDialog.LOAD); dialog.setVisible(true); try { FileReader fr = new FileReader(dialog.getDirectory()+ dialog.getFile()); BufferedReader br = new BufferedReader(fr); String str = ""; while ((str = br.readLine()) != null) { text.append(str + '\n'); } notepad.setFilePath(dialog.getDirectory() + dialog.getFile()); notepad.setTitle(notepad.getFilePath() + " - " + notepad.getTitle()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
// Note: source for ExampleFileFilter can be found in FileChooserDemo,
// under the demo/jfc directory in the JDK.
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("jpg");
filter.addExtension("gif");
filter.setDescription("JPG & GIF Images");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}
public void openFileMethod() {
if (!(text.getText().equals(""))) {
if (!(text.getText().equals(textBuffer.getText()))) {
int value = this.YES_NO_CANCEL_DIALOG();
if (value == JOptionPane.YES_OPTION){
this.saveMethod();
text.setText("");
} else if (value == JOptionPane.CENTER_ALIGNMENT) {
text.setText("");
} else {
}
}
}
dialog = new FileDialog(notepad, "打开", FileDialog.LOAD);
dialog.setVisible(true);
try {
FileReader fr = new FileReader(dialog.getDirectory()
+ dialog.getFile());
BufferedReader br = new BufferedReader(fr);
String str = "";
while ((str = br.readLine()) != null) {
text.append(str + '\n');
}
notepad.setFilePath(dialog.getDirectory() + dialog.getFile());
notepad
.setTitle(notepad.getFilePath() + " - "
+ notepad.getTitle());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
这是我写的一个“打开”功能的方法,但还是有很多BUG,比如说要实现文件有改变时弹出的提示框,我们点取消后却不能实现应该实现的功能,希望高手们能帮忙解决下!
// 打开文件的方法
public void openFileMethod() {
if (!(text.getText().equals(""))) {
if (!(text.getText().equals(textBuffer.getText()))) {
int value = this.YES_NO_CANCEL_DIALOG();
if (value == JOptionPane.YES_OPTION){
this.saveMethod();
text.setText("");
} else if (value == JOptionPane.CANCEL_OPTION) {
text.setText("");
} else {
}
}
}
dialog = new FileDialog(notepad, "打开", FileDialog.LOAD);
dialog.setVisible(true);
try {
FileReader fr = new FileReader(dialog.getDirectory()+ dialog.getFile());
BufferedReader br = new BufferedReader(fr);
String str = "";
while ((str = br.readLine()) != null) {
text.append(str + '\n');
}
notepad.setFilePath(dialog.getDirectory() + dialog.getFile());
notepad.setTitle(notepad.getFilePath() + " - " + notepad.getTitle());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}