現在Javaで、テキストエディタを作成しているのですが。
解らないことが有り、質問させてもらいます。
・テキストエディタで、開いているファイルを削除するボタンと保存するボタンの実装
・削除と保存するときに確認ダイアログの表示
・保存ボタンは上書き保存と新規作成それぞれに、違う確認文のダイアログが表示されようにする(上書き保存と新規作成はchoiceで選択します)
以上なのですが、
どうすれば良いのか、皆目見当がつきません
どなたかご回答よろしくお願いします。
下記のように作成しています
//コンポーネントの宣言
public class Frame
JRadioButton rb = new JRadioButton("新規作成", true);
JRadioButton rb1 = new JRadioButton("修正", false);
JRadioButton rb2 = new JRadioButton("削除", false);
JButton jb = new JButton("終了");
public JButton jb1 = new JButton("開く");
JButton jb2 = new JButton("クリア");
JButton jb3 = new JButton("削除");
-
- リスト JButton jb4 = new JButton("保存");
JButton jb5 = new JButton("・・・"); JButton jb6 = new JButton("・・・");
JLabel l1 = new JLabel("動作:");
JLabel l2 = new JLabel("入力ファイル:");
JLabel l3 = new JLabel("出力方法:");
JLabel l4 = new JLabel("出力ファイル:");
JTextField t = new JTextField();
JTextField t1 = new JTextField();
JFileChooser jc = new JFileChooser();
Choice c = new Choice();
JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta);
//コンポーネントの配置 public Frame() { super("テキストファイル編集ツール");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(700, 500);
ta.setLineWrap(true);
//パネル スクロール JPanel p = new JPanel(); p.setLayout(null); sp.setBounds(50, 93, 476, 227); p.add(sp); //ラジオ rb.setBounds(240, 15, 85, 20); rb1.setBounds(335, 15, 85, 20); rb2.setBounds(420, 15, 85, 20); ButtonGroup gr =new ButtonGroup(); p.add(rb); p.add(rb1); p.add(rb2); //ボタン jb.setBounds(575, 50, 90, 20); jb1.setBounds(575, 90, 90, 20); jb2.setBounds(575, 165, 90, 30); jb3.setBounds(575, 200, 90, 30); jb4.setBounds(575, 340, 90, 50); jb5.setBounds(500, 400, 20, 23); jb6.setBounds(500, 55, 20, 23); p.add(jb); p.add(jb1); p.add(jb2); p.add(jb3); p.add(jb4); p.add(jb5); p.add(jb6); //ラベル l1.setBounds(180, 17, 50, 15); l2.setBounds(75, 55, 100, 15); l3.setBounds(100, 350, 80, 30); l4.setBounds(80, 400, 100, 15); p.add(l1); p.add(l2); p.add(l3); p.add(l4); //テキストエリア t.setBounds(174, 55, 320, 23); t1.setBounds(174, 400, 323, 23); p.add(t); p.add(t1); //チョイス c .setBounds(174, 352, 323, 19); c.add("名前を付けて保存"); c.add("上書き保存"); p.add(c); jb.addActionListener(this); jb1.addActionListener(this); jb2.addActionListener(this); jb4.addActionListener(this); getContentPane().add(p, BorderLayout.CENTER);
}
public static void main(String[] args) { new Frame().setVisible(true); } //ボタンが押されたときの処理
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(jb)) { System.out.println("actionPerformed()"); System.exit(0); } if (e.getSource().equals(jb2)) { System.out.println("actionPerformed()"); ta.setText("");
}
if (e.getSource().equals(jb1)) { openFile(); } else if (e.getSource().equals(jb4)) { saveFile(); } } //ファイルを開くメソッド void openFile() { int returnVal = jc.showOpenDialog(this); try { if (returnVal == JFileChooser.APPROVE_OPTION) { File file = jc.getSelectedFile(); FileReader reader = new FileReader(file); ta.read(reader, null); setTitle(file.getAbsolutePath()); reader.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //ファイルを保存するメソッド void saveFile() { int returnVal = jc.showSaveDialog(this); try { if (returnVal == JFileChooser.APPROVE_OPTION) { FileWriter writer = new FileWriter(jc.getSelectedFile()); writer.write(ta.getText()); writer.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
}
回答1件
あなたの回答
tips
プレビュー