2016-04-28 56 views
0

JButtonとJTextFieldを使用してプログラムを作成しています。 ActionListenersはJButtonsでは動作しますが、JTextFieldsでは動作しません。私の桂コンストラクタの途中でJava JTextFieldのactionListenerが機能しない

public class Gui extends JFrame { 

private JButton Subject[] = new JButton[8]; 
private String SubjNames[] = {"Length", "Mass", "Currency", "Temperature", "Time", "Speed", "Data", "Cooking"}; 
private JButton Length1[] = new JButton[8]; 
private JButton Length2[] = new JButton[8]; 
private String LengNames[] = {"inches", "feet", "yards", "miles", "millimeters", "centimeters", "meters", "kilometers"}; 
private JTextField convertedFrom; 
private JTextField amountFrom; 
private JTextField convertedTo; 
private JTextField amountTo; 
private String from; 
private String CTo; 
private String ATo; 
private int SubjectLocX = 40; 
private int SubjectLocY = 50; 
private int Length1LocX = 40; 
private int Length1LocY = 150; 
private int Length2LocX = 330; 
private int Length2LocY = 150; 
private int t = 0; 

public Gui(){ 

    super("Converter"); 
    setLayout(null); 

    System.out.println("yes"); 

    for (int i = 0; i<8; i++) { 
    Subject[i] = new JButton(SubjNames[i]); 
    Subject[i].setLocation(SubjectLocX,SubjectLocY); 
    Subject[i].setSize(200,50); 
    add(Subject[i]); 
    if (i < 3) { 
     SubjectLocX = 40; 
     SubjectLocY += 100; 
    } else if (i == 3) { 
     SubjectLocX = 330; 
     SubjectLocY = 50; 
    } else if (i > 3) { 
     SubjectLocY += 100; 
     } 
    } 

    HandlerClass handler = new HandlerClass(); 

    for (int i = 0; i<8; i++) { 
    Subject[i].addActionListener(handler); 
    } 


    for (int i = 0; i<8; i++) { 
    Length1[i] = new JButton(LengNames[i]); 
    Length2[i] = new JButton(LengNames[i]); 
    } 
    convertedFrom = new JTextField(from, 20); 
    convertedTo = new JTextField(CTo, 20); 
    amountFrom = new JTextField("amount", 20); 
    amountFrom.addActionListener(handler); 
    amountTo = new JTextField(ATo, 20); 

    for (int i = 0; i<8; i++) { 
     Length1[i].addActionListener(handler); 
     Length2[i].addActionListener(handler); 
     } 

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setSize(600,500); 
    setLocation(400,200); 
    setVisible(true); 
} 

public void Step2() { 

    for (int i = 0; i<8; i++) { 
     remove(Subject[i]); 
    } 
    for (int i = 0; i<8; i++) { 
     remove(Length1[i]); 
     remove(Length2[i]); 
    } 
    remove(convertedFrom); 
    remove(convertedTo); 
    remove(amountFrom); 
    remove(amountTo); 

    HandlerClass handler = new HandlerClass(); 

    convertedFrom = new JTextField(from, 20); 
    convertedFrom.setEditable(false); 
    convertedFrom.setLocation(40,50); 
    convertedFrom.setSize(200,30); 
    add(convertedFrom); 

    convertedTo = new JTextField(CTo, 20); 
    convertedTo.setEditable(false); 
    convertedTo.setLocation(330,50); 
    convertedTo.setSize(200,30); 
    add(convertedTo); 

    amountFrom = new JTextField("amount", 20); 
    amountFrom.setLocation(40,100); 
    amountFrom.setSize(200,30); 
    add(amountFrom); 

    amountTo = new JTextField(ATo, 20); 
    amountTo.setEditable(false); 
    amountTo.setLocation(330,100); 
    amountTo.setSize(200,30); 
    add(amountTo); 

    Length1LocX = 40; 
    Length1LocY = 150; 
    Length2LocX = 330; 
    Length2LocY = 150; 

    for (int i = 0; i<8; i++) { 
     Length1[i].setLocation(Length1LocX, Length1LocY); 
     Length1[i].setSize(90, 50); 
     add(Length1[i]); 
     if (i < 3) { 
      Length1LocX = 40; 
      Length1LocY += 100; 
     } else if (i == 3) { 
      Length1LocX = 150; 
      Length1LocY = 150; 
     } else if (i > 3) { 
      Length1LocY += 100; 
      } 
     Length2[i].setLocation(Length2LocX, Length2LocY); 
     Length2[i].setSize(90, 50); 
     add(Length2[i]); 
     if (i < 3) { 
      Length2LocX = 330; 
      Length2LocY += 100; 
     } else if (i == 3) { 
      Length2LocX = 440; 
      Length2LocY = 150; 
     } else if (i > 3) { 
      Length2LocY += 100; 
      } 
    } 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setSize(600,600); 
    setLocation(400,200); 
    setVisible(true); 
} 
private class HandlerClass implements ActionListener { 
    public void actionPerformed(ActionEvent event) { 
     System.out.println("bruhhh"); 
     if (event.getSource() == amountFrom) { 
      System.out.println("works"); 
     } 
     for (int i = 0; i<8; i++) { 
      if (event.getSource() == Length1[i]) { 
       from = event.getActionCommand(); 
      } 
      if (event.getSource() == Length2[i]) { 
       CTo = event.getActionCommand(); 
      } 
     } 

     Step2(); 
    } 
} 
} 

、「amountFrom」のTextFieldを作成し、その後、私はActionListenerを追加しています。次に、ActionHandlerクラスの一番下にあるイベントを探します。しかし、プログラムを実行して(長さをクリックして)、長さを変更してから、テキストフィールドの値を変更してEnterキーを押すと、コンソール印刷が表示されません(textFieldアクション発生する)。 他のすべてのJbuttonアクションリスナーが機能しますが、このテキストフィールドのactionListenerは機能しません。

答えて

1

コンストラクタで、JTextFieldにActionListenerを追加します。

amountFrom = new JTextField("amount", 20); 
amountFrom.addActionListener(handler); 

しかし、あなたはのJTextFieldの新しいインスタンスを作成し、それにリスナーを追加しないStep2()あなたの方法で

amountFrom = new JTextField("amount", 20); 
amountFrom.setLocation(40,100); 
amountFrom.setSize(200,30); 
add(amountFrom); 


注: レイアウトマネージャを見て、それらを使用することを検討してください。 https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

+0

恐ろしく、ありがとう!はい、レイアウトマネージャをもう一度調べなければなりません。私は、私が現在持っているような列スタイルのレイアウトを持っているかどうか分からなかったので、私はそれを最初に使ったことはありません。 (さらに、コーナーに戻るボタンなどのランダムなボタンを追加したい場合は、そうすることができるかどうかはわかりませんでした)。 – airide101

関連する問題