0

JTextComponent からコピー アンド ペースト メソッドを取得する際に問題が発生しています

私のプログラムでは、メニューの選択肢となる文字列の配列があります。「コピー」と「貼り付け」はその 2 つです。

 else if (e.getActionCommand().equalsIgnoreCase("Copy"))
            {
                JTextArea a = new JTextArea();
                a.setEditable(true);
                a.copy();
            }
            else if (e.getActionCommand().equalsIgnoreCase("Paste"))
            {
               JTextArea a = new JTextArea();
                a.setEditable(true);
                 a.getSelectedText();
                 a.paste();
            }

エラーメッセージは表示されませんが、機能していません。任意の助けをいただければ幸いです

4

2 に答える 2

1

JTextAreaアクションを実行するたびに新しいインスタンスを作成しています。

これらは、実際に画面に表示されているものを表すものではありません。代わりに、インスタンス変数を操作するかJTextArea、画面上にある のインスタンスをパラメーターとして渡します。

于 2013-08-04T04:35:36.867 に答える
0

スコープが if 条件でのみ制限されているローカル オブジェクトを宣言しています。

            else if (e.getActionCommand().equalsIgnoreCase("Copy"))
            {
                JTextArea a = new JTextArea();    // CREATING A NEW OBJECT
                a.setEditable(true);
                a.copy();
            }                // AS Soon as the code comes HERE THE Instance IS LOST with the data

宣言する;

 JTextArea a = new JTextArea();  outside the if condition, maybe in the class before main(){}
 Create an private instance variable of the same.

お役に立てれば。ご不明な点がございましたら、お知らせください。

class TEST{
         public JTextArea a = new JTextArea();   

          TEST objectOfTEST = new TEST():
          publis static String someText = "";

         public static void main(String[] args){

               if(e.getActionCommand().equalsIgnoreCase("Copy")){
                   someText = objectOfTEST.a.getText();
               }
               else if(e.getActionCommand().equalsIgnoreCase("Paste")){
                   // PERFORM SOME OPERATION
                   someText = "Paste this";
                   objectOfTEST.a.setText("Some TEXT that you want to set here");
               }
         }
}
于 2013-08-04T04:37:15.280 に答える