#実現したいこと
テキストフィールドを10個作り、テキストフィールド上でEnterキーもしくはtabキーを押すとフォーカスが移動するプログラムを作っているのですが、リスナー部分(36行目~85行目)とリスナーをテキストフィールドに登録する部分(24行目~33行目)の行数を短くすることはできますか?可能ならその方法を教えてください。
#該当のソースコード
java
1import java.awt.FlowLayout; 2import java.awt.event.ActionEvent; 3import java.awt.event.ActionListener; 4 5import javax.swing.JFrame; 6import javax.swing.JTextField; 7 8public class enshu64 { 9 JTextField[] tf; 10 11 public static void main(String arg[]) { 12 new enshu64(); 13 } 14 public enshu64(){ 15 JFrame f; 16 f=new JFrame(); 17 f.getContentPane().setLayout(new FlowLayout()); 18 f.setSize(300,200); 19 tf=new JTextField[10]; 20 for(int i=0; i<10; i++){ 21 tf[i]=new JTextField(10); 22 f.getContentPane().add(tf[i]); 23 } 24 tf[9].addActionListener(new enshu64entera()); //リスナーをテキストフィールドに登録 25 tf[0].addActionListener(new enshu64enterb()); 26 tf[1].addActionListener(new enshu64enterc()); 27 tf[2].addActionListener(new enshu64enterd()); 28 tf[3].addActionListener(new enshu64entere()); 29 tf[4].addActionListener(new enshu64enterf()); 30 tf[5].addActionListener(new enshu64enterg()); 31 tf[6].addActionListener(new enshu64enterh()); 32 tf[7].addActionListener(new enshu64enteri()); 33 tf[8].addActionListener(new enshu64enterj()); 34 f.setVisible(true); 35 } 36 class enshu64entera implements ActionListener{ //リスナーの作成 37 public void actionPerformed(ActionEvent e){ 38 tf[0].requestFocusInWindow(); 39 } 40 } 41 class enshu64enterb implements ActionListener{ 42 public void actionPerformed(ActionEvent e){ 43 tf[1].requestFocusInWindow(); 44 } 45 } 46 class enshu64enterc implements ActionListener{ 47 public void actionPerformed(ActionEvent e){ 48 tf[2].requestFocusInWindow(); 49 } 50 } 51 class enshu64enterd implements ActionListener{ 52 public void actionPerformed(ActionEvent e){ 53 tf[3].requestFocusInWindow(); 54 } 55 } 56 class enshu64entere implements ActionListener{ 57 public void actionPerformed(ActionEvent e){ 58 tf[4].requestFocusInWindow(); 59 } 60 } 61 class enshu64enterf implements ActionListener{ 62 public void actionPerformed(ActionEvent e){ 63 tf[5].requestFocusInWindow(); 64 } 65 } 66 class enshu64enterg implements ActionListener{ 67 public void actionPerformed(ActionEvent e){ 68 tf[6].requestFocusInWindow(); 69 } 70 } 71 class enshu64enterh implements ActionListener{ 72 public void actionPerformed(ActionEvent e){ 73 tf[7].requestFocusInWindow(); 74 } 75 } 76 class enshu64enteri implements ActionListener{ 77 public void actionPerformed(ActionEvent e){ 78 tf[8].requestFocusInWindow(); 79 } 80 } 81 class enshu64enterj implements ActionListener{ 82 public void actionPerformed(ActionEvent e){ 83 tf[9].requestFocusInWindow(); 84 } 85 } 86} 87
#実行結果
#回答を受けてコードの書き換え
回答を受けて、以下のようにコードを書き換えてみましたが、今度は21行目に「varを型に解決できません」というエラーが出ます。
java
1import java.awt.FlowLayout; 2import java.awt.event.ActionEvent; 3import java.awt.event.ActionListener; 4 5import javax.swing.JFrame; 6import javax.swing.JTextField; 7 8public class enshu64 { 9 JTextField[] tf; 10 11 public static void main(String arg[]) { 12 new enshu64(); 13 } 14 public enshu64(){ 15 JFrame f; 16 f=new JFrame(); 17 f.getContentPane().setLayout(new FlowLayout()); 18 f.setSize(300,200); 19 tf=new JTextField[10]; 20 for(int i = 0; i < tf.length; ++i){ 21 final var tmp_tf = new JTextField(10); //varを型に解決できません。 22 tmp_tf.addActionListener( 23 e -> tmp_tf.requestFocusInWindow() 24 ); 25 26 tf[i] = tmp_tf; 27 f.getContentPane().add(tf[i]); 28 } 29 f.setVisible(true); 30 } 31 32}
--
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/06/10 22:41
2019/06/10 22:57
退会済みユーザー
2019/06/10 23:22
2019/06/10 23:27
退会済みユーザー
2019/06/11 01:12