前提・実現したいこと
Javaでの分岐
エラーの文字が出たあと、OKボタンを押して文字を消せるようにしたい。
あわせて消したあとは文字が黒字になるようにしたい。
手で消した場合も黒字になるようにしたい。
発生している問題・エラーメッセージ
もうひとつif分を加えましたが、エラーの表示すらなくなってしまいました。
該当のソースコード
java
1public void actionPerformed(ActionEvent e){ 2 3 //okbuttonが押された場合の処理 4 if(e.getSource() == okbutton){ 5 6 //入力欄がカラの場合の処理(赤字でエラーを表示) 7 if(text1.getText().equals("")){ 8 text1.setForeground(Color.RED); 9 text2.setForeground(Color.RED); 10 text1.setText("”エラー”(文字を入力してください)"); 11 text2.setText("”エラー”(文字を入力してください)"); 12 13 //ここでokbuttonを押しても文字を消せるようにしたい。 14 //消したあとは文字が黒字になるようにしたい。 15 //以下の文を試してみましたが、エラーの表示すらなくなります。 16// if(e.getSource() == okbutton){ 17// //色を黒に戻す 18// text1.setForeground(Color.BLACK); 19// text2.setForeground(Color.BLACK); 20// //文字のない状態を入れる=("") 21// text1.setText(""); 22// text2.setText(""); 23// } 24 25 //入力欄に文字が入力されていた場合、入力された文字を表示 26 }else{ 27 text2.setText(text1.getText()); 28 } 29 //clearbuttonが押された場合の処理 30 }else if(e.getSource() == clearbutton){ 31 32 //色を黒に戻す 33 text1.setForeground(Color.BLACK); 34 text2.setForeground(Color.BLACK); 35 //文字のない状態を入れる=("") 36 text1.setText(""); 37 text2.setText(""); 38 } 39 }
試したこと
以下のコードを加えました。
java
1if(e.getSource() == okbutton){ 2//色を黒に戻す 3text1.setForeground(Color.BLACK); 4text2.setForeground(Color.BLACK); 5//文字のない状態を入れる=("") 6text1.setText(""); 7text2.setText(""); 8}
補足情報(FW/ツールのバージョンなど)
eclipseを使用
java
1package bottan; 2 3import java.awt.BorderLayout; 4import java.awt.Color; 5import java.awt.Container; 6import java.awt.event.ActionEvent; 7import java.awt.event.ActionListener; 8 9import javax.swing.JButton; 10import javax.swing.JFrame; 11import javax.swing.JLabel; 12import javax.swing.JPanel; 13import javax.swing.JTextField; 14 15//入力画面を表示し、文字入力を受け付けるクラス 16class Screen extends JFrame implements ActionListener{ 17 18 JTextField text1; 19 JTextField text2; 20 21 //2つのボタンを設置 22 JButton okbutton = new JButton("OK"); 23 JButton clearbutton = new JButton("クリア"); 24 25 public static void main(String args[]){ 26 27 Screen frame = new Screen("タイトル"); 28 29 //フレームを表示 30 frame.setVisible(true); 31 } 32 33 Screen(String title){ 34 setTitle("入力画面"); 35 36 //サイズ ・ 位置を指定 37 setBounds(600, 400, 800, 75); 38 39 ////×を押した時の処理(アプリケーションを終了する) 40 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 41 42 //パネルを作成 43 JPanel p = new JPanel(); 44 45 //addActionListener=対象となるボタンがActionEventを処理 46 //イベントの通知先をthis(このオブジェクト)にするように指定 47 okbutton.addActionListener(this); 48 clearbutton.addActionListener(this); 49 50 //入力欄1を設置 51 text1 = new JTextField("", 20); 52 53 //”入力欄”の文字を表示 54 JLabel lav=new JLabel("入力欄"); 55 p.add(lav, BorderLayout.CENTER); 56 57 //入力欄1をコンテンツ区画に追加する 58 p.add(text1); 59 60 //ボタンをコンテンツ区画に追加する 61 p.add(okbutton); 62 63 //ボタンをコンテンツ区画に追加する 64 p.add(clearbutton); 65 66 //ContentPane(=ボタンなどのコンポーネント)を取得 67 Container contentPane = getContentPane(); 68 69 //BorderLayout=配置を指定 70 //add=コンポーネントを追加 71 contentPane.add(p, BorderLayout.CENTER); 72 73 //”出力欄”の文字を表示 74 JLabel lav2=new JLabel("出力欄"); 75 p.add(lav2, BorderLayout.CENTER); 76 77 //入力欄2を設置 78 text2 = new JTextField("", 20); 79 80 //入力欄2をコンテンツ区画に追加する 81 p.add(text2, BorderLayout.WEST);; 82 } 83 84 public void actionPerformed(ActionEvent e){ 85 86 //okbuttonが押された場合の処理 87 if(e.getSource() == okbutton){ 88 89 //入力欄がカラの場合の処理(赤字でエラーを表示) 90 if(text1.getText().equals("")){ 91 text1.setForeground(Color.RED); 92 text2.setForeground(Color.RED); 93 text1.setText("”エラー”(文字を入力してください)"); 94 text2.setText("”エラー”(文字を入力してください)"); 95 96 //ここでokbuttonを押しても文字を消せるようにしたい。 97 //消したあとは文字が黒字になるようにしたい。 98 //以下の文を試してみましたが、エラーの表示すらなくなります。 99// if(e.getSource() == okbutton){ 100// //色を黒に戻す 101// text1.setForeground(Color.BLACK); 102// text2.setForeground(Color.BLACK); 103// //文字のない状態を入れる=("") 104// text1.setText(""); 105// text2.setText(""); 106// } 107 108 //入力欄に文字が入力されていた場合、入力された文字を表示 109 }else{ 110 text2.setText(text1.getText()); 111 } 112 //clearbuttonが押された場合の処理 113 }else if(e.getSource() == clearbutton){ 114 115 //色を黒に戻す 116 text1.setForeground(Color.BLACK); 117 text2.setForeground(Color.BLACK); 118 //文字のない状態を入れる=("") 119 text1.setText(""); 120 text2.setText(""); 121 } 122 } 123}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/03/25 15:41