javaでメモ帳を作っています。
保存ボタンを押して保存する時に
新規作成と上書き保存の場合で、
違う文章の確認ダイアログを表示する機能を実装したいのですが、
うまく動きません。
新規作成か上書き保存かは、ラジオボタンで選択します。
それから保存ボタンを押しダイアログを表示するようにしたいのです。
大変分かりにくい質問かと思いますが何卒宜しくお願い致します。
下記がコードになります。
package kadai;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TextTool extends JFrame implements ActionListener {
JRadioButton rb = new JRadioButton("新規作成", true); JRadioButton rb1 = new JRadioButton("上書き保存", false); JRadioButton rb2 = new JRadioButton("削除", false); ButtonGroup gr =new ButtonGroup(); JButton jb = new JButton("終了"); 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(); JTextArea ta = new JTextArea(); JScrollPane sp = new JScrollPane(ta); String[] filesave = {"", ""}; JComboBox c =new JComboBox(filesave); public TextTool() { 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); gr.add(rb); gr.add(rb1); gr.add(rb2); 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); jb1.setEnabled(false); jb6.setEnabled(false); //ラベル 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); t.setEnabled(false); c .setBounds(174, 352, 323, 19); c.setEnabled(false); p.add(c); rb.addActionListener(this); rb1.addActionListener(this); rb2.addActionListener(this); jb.addActionListener(this); jb1.addActionListener(this); jb2.addActionListener(this); jb4.addActionListener(this); getContentPane().add(p, BorderLayout.CENTER);
}
public static void main(String[] args) { new TextTool().setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == (rb)) { System.out.println("actionPerformed()"); ta.setText(""); c.setEnabled(false); jb1.setEnabled(false); jb4.setEnabled(true); jb6.setEnabled(false); ta.setEnabled(true); t.setEnabled(false); } if (e.getSource() == (rb1)) { System.out.println("actionPerformed()"); c.setEnabled(true); jb1.setEnabled(true); jb4.setEnabled(true); jb6.setEnabled(true); ta.setEnabled(true); t.setEnabled(true); } if (e.getSource() ==(rb2)) { System.out.println("actionPerformed()"); jb1.setEnabled(true); jb4.setEnabled(false); jb6.setEnabled(true); ta.setEnabled(false); t.setEnabled(true); t.setText(""); } if (e.getSource() == (jb)) { System.out.println("actionPerformed()"); System.exit(0); } if (e.getSource() == (jb1)) { openFile(); } if (e.getSource() == jb2) { String selectvalues[] = {"実行", "終了"}; JOptionPane.showOptionDialog(this, "内容をクリアにします。よろしですか?", "", JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, selectvalues, selectvalues[0]); System.out.println("actionPerformed()"); ta.setText(""); } if (e.getSource() == jb3) { String selectvalues[] = {"実行", "終了"}; JOptionPane.showOptionDialog(this, "ファイルを削除します。よろしですか?", "" , JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, selectvalues, selectvalues[0]); System.out.println("actionPerformed()"); } boolean c = true; if (e.getSource().equals(jb4)) { String selectvalues[] = {"実行", "終了"}; JOptionPane.showOptionDialog(this, "ファイルを作成します。よろしですか?", "" , JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, selectvalues, selectvalues[0]); System.out.println("actionPerformed()"); saveFile(); } if (e.getSource().equals(jb4)) { String selectvalues[] = {"実行", "終了"}; JOptionPane.showOptionDialog(this, "ファイルを更新します。よろしですか?", "", JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, selectvalues, selectvalues[0]); System.out.println("actionPerformed()"); 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(); } }
}
回答2件
あなたの回答
tips
プレビュー