###前提・実現したいこと
javaです。for文でtextFieldを複数回生成する。
そこにユーザーが入力したテキスト内の文字情報を
外部ファイル(txt)にまとめて書き出す
###発生している問題・エラーメッセージ
javaで簡単な入力のためのアプリを作っています。
jqueryでは、テキストエリアにタグ付けが
簡単に出来たので、テキスト内の文字情報を
簡単に取得出来たのですが、javaでは
そのようなタグ付け?のような事がうまく出来ません。
すいません、かなり初歩的な問題だと思うのですが、
調べても全然分かりませんでした。
クラスを配列に入れる?ような事は可能なんでしょうか?
似たようなくらすを全部記述する、というのは
かなり不便なので、for文で、添え字の番号だけ変えて
作れれば便利だなと思ってます。
エラーメッセージ
コンパイルは通りますが、
ボタンを押すとNullPointerExceptionが発生します。
###該当のソースコード //java import java.awt.BorderLayout; import java.awt.Container; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.io.*; import java.util.HashMap; public class SwingAppMain_kai1 implements ActionListener { private JFrame mainFrame; private Container contentPane; private JTextField textField; private JTextArea textArea; private JScrollPane scrollPane; private JPanel buttonPane; private JButton addButton; private JButton clearButton; private JFrame mainFrame1; private Container contentPane1; private JTextField textField1; private JTextArea textArea1; private JScrollPane scrollPane1; private JPanel buttonPane1; private JButton addButton1; private JButton clearButton1; // コンストラクタ public static void main(String[] args) { new SwingAppMain_kai1(); } public SwingAppMain_kai1(){ mainFrame = new JFrame("サンプル"); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.setSize(320, 200); mainFrame.setLocationRelativeTo(null); contentPane = mainFrame.getContentPane(); textField = new JTextField(); textArea = new JTextArea(); scrollPane = new JScrollPane(textArea); addButton = new JButton("追加"); clearButton = new JButton("消去"); // 「追加」ボタンとアクション・リスナーの関連付け addButton.addActionListener(this); // 「消去」ボタンとアクション・リスナーの関連付け clearButton.addActionListener(this); buttonPane = new JPanel(); buttonPane.add(addButton); buttonPane.add(clearButton); contentPane.add(textField, BorderLayout.NORTH); contentPane.add(scrollPane, BorderLayout.CENTER); contentPane.add(buttonPane, BorderLayout.SOUTH); mainFrame.setVisible(true); } // 利用者の操作に応じた処理を実装 public void actionPerformed(ActionEvent event){ JTextArea[] array; array = new JTextArea[5]; // ユーザの操作対象を判断 if(event.getSource() == addButton) { // テキストエリアへ文字列を追加 //textArea.append(textField.getText() + "\n"); for(int i=0;i<5;i++){ int a=0; int b=0; a =(int)( Math.random()*1000 ) % 500; b =(int)( Math.random()*1000 ) % 500; mainFrame1 = new JFrame("サンプル1"); mainFrame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame1.setBounds(a,b, 200, 200); //mainFrame1.setLocationRelativeTo(null); contentPane1 = mainFrame1.getContentPane(); array[i] = new JTextArea(); //HashMap<String,String> map = new HashMap<String,String>(); //map.put(i,textField1); //JTextArea[] array; //textArea1 = new JTextArea[5]; scrollPane1 = new JScrollPane(textArea1); addButton1 = new JButton("追加"); clearButton1 = new JButton("消去"); // 「追加」ボタンとアクション・リスナーの関連付け addButton1.addActionListener(this); // 「消去」ボタンとアクション・リスナーの関連付け clearButton1.addActionListener(this); buttonPane1 = new JPanel(); buttonPane1.add(addButton1); buttonPane1.add(clearButton1); contentPane1.add(textField1, BorderLayout.NORTH); contentPane1.add(scrollPane1, BorderLayout.CENTER); contentPane1.add(buttonPane1, BorderLayout.SOUTH); mainFrame1.setVisible(true); } } if(event.getSource() == clearButton) { // テキストエリアの文字列を全消去 //textArea.setText(null); try{ String st =null; st = array[0].getText(); BufferedWriter bw = new BufferedWriter( new FileWriter("test_1_1.txt") ); //bw.newLine(); bw.write(st); bw.close(); } catch(IOException e){ System.exit(1); } } } // アプリケーションの起動 } ###試したこと 課題に対してアプローチしたことを記載してください hashamapやarrayリストを使いましたが、 うまくいきませんでした。 ###補足情報(言語/FW/ツール等のバージョンなど) より詳細な情報
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/05/14 06:40