下面列出了java.awt.FileDialog#show ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void jMenuItem4ActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
// Add your handling code here:
FileDialog fileDialog = new FileDialog (this, "Open...", FileDialog.LOAD);
fileDialog.show ();
if (fileDialog.getFile () == null)
return;
fileName = fileDialog.getDirectory () + File.separator + fileDialog.getFile ();
FileInputStream fis = null;
String str = null;
try {
fis = new FileInputStream (fileName);
int size = fis.available ();
byte[] bytes = new byte [size];
fis.read (bytes);
str = new String (bytes);
} catch (IOException e) {
} finally {
try {
fis.close ();
} catch (IOException e2) {
}
}
if (str != null)
textBox.setText (str);
}
private void doSaveAs () {
FileDialog fileDialog = new FileDialog (this, "Save As...", FileDialog.SAVE);
fileDialog.show ();
if (fileDialog.getFile () == null)
return;
fileName = fileDialog.getDirectory () + File.separator + fileDialog.getFile ();
doSave (fileName);
}
/** Invoker for the saveas action */
public void saveAs() {
Doc doc = getSelectedDoc();
if (doc == null) {
throw new NullPointerException ("no doc");
}
FileDialog fd = new FileDialog (MiniEdit.this, "Save as");
fd.setMode(fd.SAVE);
fd.show();
if (fd.getFile() != null) {
File nue = new File (fd.getDirectory() + fd.getFile());
try {
boolean success = nue.createNewFile();
if (success) {
FileWriter w = new FileWriter (nue);
doc.getTextPane().write(w);
file = nue;
documentTabs.setTitleAt(documentTabs.getSelectedIndex(), nue.getName());
documentTabs.setToolTipTextAt(documentTabs.getSelectedIndex(), nue.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/** This method is called when File -> Open menu item is invoked.
* It displays a dialog to choose the file to be opened and edited.
* @param evt ActionEvent instance passed from actionPerformed event.
*/
private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed
FileDialog fileDialog = new FileDialog(this, "Open...", FileDialog.LOAD);
fileDialog.show();
if (fileDialog.getFile() == null)
return;
fileName = fileDialog.getDirectory() + File.separator + fileDialog.getFile();
FileInputStream fis = null;
String str = null;
try {
fis = new FileInputStream(fileName);
int size = fis.available();
byte[] bytes = new byte [size];
fis.read(bytes);
str = new String(bytes);
} catch (IOException e) {
} finally {
try {
fis.close();
} catch (IOException e2) {
}
}
if (str != null)
textBox.setText(str);
}
/** Asks for a file name. then saves the current content of editor pane to the file.
*/
private void doSaveAs() {
FileDialog fileDialog = new FileDialog(this, "Save As...", FileDialog.SAVE);
fileDialog.show();
if (fileDialog.getFile() == null)
return;
fileName = fileDialog.getDirectory() + File.separator + fileDialog.getFile();
doSave(fileName);
}
public String rChooseFile(Rengine re, int newFile) {
FileDialog fd = new FileDialog(new Frame(),
(newFile == 0) ? "Select a file" : "Select a new file",
(newFile == 0) ? FileDialog.LOAD : FileDialog.SAVE);
fd.show();
String res = null;
if (fd.getDirectory() != null)
res = fd.getDirectory();
if (fd.getFile() != null)
res = (res == null) ? fd.getFile() : (res + fd.getFile());
return res;
}
/**
* Import to a jMusic score a standard MIDI file
* Assume the MIDI file name is the same as the score title with .mid appended
* prompt for a fileName
* @param Score
*/
public static void midi(Score score) {
FileDialog fd = new FileDialog(new Frame(),
"Select a MIDI file to open.",
FileDialog.LOAD);
fd.show();
if (fd.getFile() != null) {
Read.midi(score, fd.getDirectory() + fd.getFile());
}
}
/**
* Import the xml file as a jMusic score.
* Prompt for a fileName
* @param Score
*/
public static void xml(Score score) {
FileDialog fd = new FileDialog(new Frame(),
"Select a jMusic XML file to open.",
FileDialog.LOAD);
fd.show();
if (fd.getFile() != null) {
Read.xml(score, fd.getDirectory() + fd.getFile());
}
}
/**
* Save the jMusic score as a standard MIDI file
* Prompt user for a filename
* @param Score
*/
public static void midi(Score score) {
FileDialog fd = new FileDialog(new Frame(),
"Save as a MIDI file ...",
FileDialog.SAVE);
fd.setFile("jMusic_composition.mid");
fd.show();
if (fd.getFile() != null) {
Write.midi(score, fd.getDirectory() + fd.getFile());
}
}
/**
* Dialog to import a MIDI file
*/
public void openMidi() {
Score s = new Score();
FileDialog loadMidi = new FileDialog(this, "Select a MIDI file.", FileDialog.LOAD);
loadMidi.setDirectory( lastDirectory );
loadMidi.setFile( lastFileName );
loadMidi.show();
String fileName = loadMidi.getFile();
if (fileName != null) {
lastFileName = fileName;
lastDirectory = loadMidi.getDirectory();
Read.midi(s, lastDirectory + fileName);
setNewScore(s);
}
}
/**
* Dialog to import a jm file
*/
public void openJM() {
FileDialog loadjm = new FileDialog(this, "Select a jm score file.", FileDialog.LOAD);
loadjm.setDirectory( lastDirectory );
loadjm.show();
String fileName = loadjm.getFile();
if (fileName != null) {
Score s = new Score();
lastDirectory = loadjm.getDirectory();
Read.jm(s, lastDirectory + fileName);
setNewScore(s);
}
}
/**
* Dialog to import a jm XML file
*/
public void openJMXML() {
FileDialog loadjmxml = new FileDialog(this, "Select a jMusic XML score file.", FileDialog.LOAD);
loadjmxml.setDirectory( lastDirectory );
loadjmxml.show();
String fileName = loadjmxml.getFile();
if (fileName != null) {
Score s = new Score();
lastDirectory = loadjmxml.getDirectory();
Read.xml(s, lastDirectory + fileName);
setNewScore(s);
}
}
/**
* Dialog to save phrase as a MIDI file.
*/
public void saveMidi() {
FileDialog fd = new FileDialog(this, "Save as a MIDI file...",FileDialog.SAVE);
fd.show();
//write a MIDI file and stave properties to disk
if ( fd.getFile() != null) {
Write.midi(score, fd.getDirectory()+fd.getFile());
/*
for(int i=0; i<staveArray.length; i++){
System.out.println(i);
StavePhraseProperties props =
new StavePhraseProperties(
staveArray[i], staveArray[i].getPhrase());
try {
System.out.println("props");
props.writeToFile(
fd.getDirectory()+fd.getFile());
}
catch ( Throwable e) {
System.out.println(
"Error Saving Properties " +
e.getMessage() );
}
}
*/
}
}
/**
* Dialog to save score as a jMusic serialized jm file.
*/
public void saveJM() {
FileDialog fd = new FileDialog(this, "Save as a jm file...",FileDialog.SAVE);
fd.show();
//write a MIDI file to disk
if ( fd.getFile() != null) {
Write.jm(score, fd.getDirectory()+fd.getFile());
}
}
/**
* Dialog to save score as a jMusic XML file.
*/
public void saveJMXML() {
FileDialog fd = new FileDialog(this, "Save as a jMusic XML file...",FileDialog.SAVE);
fd.show();
//write an XML file to disk
if ( fd.getFile() != null) {
Write.xml(score, fd.getDirectory()+fd.getFile());
}
}
/**
* Get the first phrase from a MIDI file.
*/
public Phrase readMidiPhrase() {
FileDialog loadMidi = new FileDialog(this, "Select a MIDI file.", FileDialog.LOAD);
loadMidi.show();
String fileName = loadMidi.getFile();
Phrase phr = new Phrase(0.0);
Score scr = new Score();
if (fileName != null) {
Read.midi(scr, loadMidi.getDirectory() + fileName);
}
scr.clean();
if (scr.size() > 0 && scr.getPart(0).size() > 0) phr = scr.getPart(0).getPhrase(0);
//System.out.println("Size = " + phr.size());
return phr;
}