私は Java (IDE: NetBeans 7.4、JDK 7、Swing API) でプログラムを書いて、Microsoft のくだらないメモ帳を代用しています。
私が直面している問題は、ユーザーがファイルを保存したいディレクトリを取得するためのボタンを追加したことです。ボタンのコード (choosedir という名前) は次のとおりです。
private void choosedirActionPerformed(java.awt.event.ActionEvent evt) {
int select=choose.showOpenDialog(this);
if (select == choose.APPROVE_OPTION){
String dir=choose.getName(choose.getCurrentDirectory());
directory.setText(dir.toString()+"-");
}
}
上記のコードは、選択したディレクトリをテキスト ボックスの「ディレクトリ」に設定します。
さて、ライターのコードは次のとおりです。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String dir=choose.getName(choose.getCurrentDirectory());
directory.setText(dir.toString());
File file=new File(dir+name+".txt");
try{
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));
out.append(input.getText());
}
catch (UnsupportedEncodingException e)
{
System.out.println(e.getMessage());
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
問題は次のとおりです。 1. ファイルが保存されていません 2. どのディレクトリを選択しても、Windows エクスプローラに表示されるディレクトリ名が表示されます。同様に、C:/Windows/Temp に入れたい場合は、Temp のみが表示されます。または、C:/ の場合でも、「Local Disk C:.
2013 年 11 月 16 日更新: アレックスの助けにもかかわらず、プログラムはまだ動作しません。ソースコード全体は次のとおりです。
import java.io.*;
public class QuickPad_v1 extends javax.swing.JFrame {
/**
* Creates new form QuickPad_v1
*/
public QuickPad_v1() {
initComponents();
}
/* Avoid the non-programmed buttons below! */
private void extensionActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String dir=directory.getText();
File file=new File("C://"+name+".txt");
try{
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));
out.append(input.getText());
}
catch (UnsupportedEncodingException e)
{
System.out.println(e.getMessage());
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
private void choosedirActionPerformed(java.awt.event.ActionEvent evt) {
int select=choose.showOpenDialog(this);
if (select==choose.APPROVE_OPTION){
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new QuickPad_v1().setVisible(true);
}
});
}
助けが必要です!私はあなたに投票します、約束します!