2016-10-16 15 views
0

私は、解析プログラムが入力ファイルからデータを読み込んでトークン化し、そこからGUIを構築するプロジェクトに取り組んでいます。この場合、電卓のGUIです。私は今電卓操作のためのコードを記述しようとしており、電卓のボタンにActionListenersを追加するのに問題があります。彼らは、ArrayListの中に保存されていると私は、私は次のエラーが表示されaddActionListener()関数を使用しようとすると:以下ActionListener errorArrayList内のボタンにActionListenersを追加する方法は?

は、ビルダーファイルの私のコードです:

import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
import javax.swing.*; 

public class GUIBuilder implements ActionListener { 
    //Declare variables 
    boolean[] function = new boolean[4]; 
    double[] temp = {0, 0}; 
    static JFrame outputFrame; //Declare GUI window frame 
    static JPanel panel1, panel2, panel3, panel4; //Declare GUI panel variables 
    static Container container; //Declare GUI container variable 
    static JTextField textField; //Declare GUI text field variable 
    static ArrayList<JButton> button; 
    static ArrayList<JLabel> label; 
    static ArrayList<JRadioButton> radio; 
    static GUIParser guiParser; //Create instance of GUIParser 

    public static void main(String[] args) throws Exception { 

    //Instantiates a GUIParser object 
    guiParser = new GUIParser(); 

    //Instantiates ArrayList objects 
    button = new ArrayList<>(); 
    label = new ArrayList<>();  
    radio = new ArrayList<>(); 

    //Instantiates a GUIBuilder object that invokes frameConstructor() method 
    GUIBuilder guiBuilder = new GUIBuilder(); 
    guiBuilder.frameConstructor(); 
    } 

    public void frameConstructor() { 

    //Instantiates a JFrame object 
    outputFrame = new JFrame(); 

    //Sets window size 
    outputFrame.setSize(this.guiParser.getWindowWidth(),this.guiParser.getWindowHeight()); //Sets size of outputFrame to the return value of getWindowWidth method 

    //Sets window name 
    outputFrame.setTitle(this.guiParser.getWindowName()); //Sets title of window to return value of getWindowName method 
    outputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Sets the default close operation to exit 

    container = outputFrame.getContentPane(); 
    if (this.guiParser.getWindowLayout() == 0) { //If return value of getWindowLayout is 0, use flow layout 
      container.setLayout(new FlowLayout()); 
    } else { //If return value is other than 0, use grid layout 
      if (this.guiParser.getHorizontalSpace() != 0 && this.guiParser.getVerticalSpace() != 0) { //If return values of getHorizontalSpace and getVerticalSpace are zero, create Grid layout with these dimensions 
     container.setLayout(new GridLayout 
     (this.guiParser.getNumRows(),this.guiParser.getNumColumns(), 
     this.guiParser.getHorizontalSpace(),this.guiParser.getVerticalSpace())); 
      } else { //Otherwise create a Grid layout with these dimensions 
       container.setLayout(new GridLayout 
     (this.guiParser.getNumRows(),this.guiParser.getNumColumns())); 
      } 
    } 

    //Instantiates new JPanel objects 
    panel1 = new JPanel(); 
    panel2 = new JPanel(); 
    panel3 = new JPanel(); 
    panel4 = new JPanel(); 
    if (this.guiParser.getPanelLayout() == 0) { //If return value of getPanelLayout is zero, use flow layout 
      panel1.setLayout(new FlowLayout()); 
      panel2.setLayout(new FlowLayout()); 
      panel3.setLayout(new FlowLayout()); 
     } else { //Otherwise use grid layout, set dimensions according to input values 
      if (this.guiParser.getHorizontalSpace() != 0 && this.guiParser.getVerticalSpace() != 0) { 
     panel1.setLayout(new GridLayout 
        (this.guiParser.getNumRows(),this.guiParser.getNumColumns(), 
         this.guiParser.getHorizontalSpace(),this.guiParser.getVerticalSpace())); 
     panel2.setLayout(new GridLayout 
        (this.guiParser.getNumRows(),this.guiParser.getNumColumns(), 
      this.guiParser.getHorizontalSpace(),this.guiParser.getVerticalSpace())); 
     panel3.setLayout(new GridLayout 
        (this.guiParser.getNumRows(),this.guiParser.getNumColumns(), 
      this.guiParser.getHorizontalSpace(),this.guiParser.getVerticalSpace())); 
     panel4.setLayout(new GridLayout 
        (this.guiParser.getNumRows(),this.guiParser.getNumColumns(), 
      this.guiParser.getHorizontalSpace(),this.guiParser.getVerticalSpace())); 
      } else { 
     panel1.setLayout(new GridLayout 
        (this.guiParser.getNumRows(),this.guiParser.getNumColumns())); 
     panel2.setLayout(new GridLayout 
        (this.guiParser.getNumRows(),this.guiParser.getNumColumns())); 
     panel3.setLayout(new GridLayout 
        (this.guiParser.getNumRows(),this.guiParser.getNumColumns())); 
     panel4.setLayout(new GridLayout 
        (this.guiParser.getNumRows(),this.guiParser.getNumColumns())); 
      } 
    } 

    //Instantiates a JTextField object and sets text field width with the return value of getTextWidth() 
    textField = new JTextField("", this.guiParser.getTextWidth()); 
    container.add(textField); //Add the text field to the Jframe container 
    panel1.add(textField); //Add the text field to panel 1 

    //Walk through the buttonList array and add buttons to the container and panel 2 
    int i = 0; 
    Iterator<String> iterator = this.guiParser.getButtonList().iterator(); 
    while (iterator.hasNext()) { 
      button.add(new JButton(iterator.next())); 
      container.add(button.get(i)); 
      panel2.add(button.get(i)); 
      button.addActionListener(this); 
      i++; 
    } 

    //Walk through the radioList array and add radio buttons to the container and panel 3 
    i = 0; 
    iterator = this.guiParser.getRadioList().iterator(); 
    while (iterator.hasNext()) { 
      radio.add(new JRadioButton(iterator.next())); 
      container.add(radio.get(i)); 
      panel3.add(radio.get(i)); 
      i++; 
    } 

    //Walk through the labelList array and add labels to the container and panel 4 
    i = 0; 
    iterator = this.guiParser.getLabelList().iterator(); 
    while (iterator.hasNext()) { 
      label.add(new JLabel(iterator.next())); 
      container.add(label.get(i)); 
      panel4.add(label.get(i)); 
      i++; 
    } 

     //Add each panel to the output frame and set its visibility 
    outputFrame.add(panel1); 
    outputFrame.add(panel2); 
    outputFrame.add(panel3); 
    outputFrame.setVisible(true); 
     for(i = 0; i < 16; i++){ 

     } 
    } 

    public void clear(){ 
     try{ 
      textField.setText(""); 
      for(int i = 0; i < 4; i++){ 
       function[i] = false; 
      } 
      for(int i = 0; i < 2; i++){ 
       temp[i] = 0; 
      } 
     } catch(NullPointerException e){ 

     } 
    } 


    public void calcResult(){ 
     double result = 0; 
     temp[1] = Double.parseDouble(textField.getText()); 
     String temp0 = Double.toString(temp[0]); 
     String temp1 = Double.toString(temp[1]); 
     try{ 
      if(temp0.contains("-")){ 
       String[] temp00 = temp0.split("-", 2); 
       temp[0] = (Double.parseDouble(temp00[1]) * -1); 
      } 
      if(temp1.contains("-")){ 
       String[] temp11 = temp1.split("-", 2); 
       temp[1] = (Double.parseDouble(temp11[1]) * -1); 
      } 
     } catch(ArrayIndexOutOfBoundsException e){ 

     } 
     try{ 
      if(function[2] == true){ 
       result = temp[0] * temp[1]; 
      } else if(function[3] == true){ 
       result = temp[0]/temp[1]; 
      } else if(function[0] == true){ 
       result = temp[0] + temp[1]; 
      } else if(function[1] == true){ 
       result = temp[0] - temp[1]; 
      } 
      textField.setText(Double.toString(result)); 
      for(int i = 0; i < 4; i++){ 
       function[i] = false; 
      } 
     } catch(NumberFormatException e){ 

     } 
    } 

    @Override 
    public void actionPerformed(ActionEvent ae) { 
     if(ae.getSource() == button.get(0)){ 
      textField.setText("1"); 
     } 
     if(ae.getSource() == button.get(1)){ 
      textField.setText(textField.getText().concat("2")); 
     } 
     if(ae.getSource() == button.get(2)){ 
      textField.setText(textField.getText().concat("3")); 
     } 
     if(ae.getSource() == button.get(3)){ 
      textField.setText(textField.getText().concat("4")); 
     } 
     if(ae.getSource() == button.get(4)){ 
      textField.setText(textField.getText().concat("")); 
     } 
     if(ae.getSource() == button.get(5)){ 
      textField.setText(textField.getText().concat("1")); 
     } 
     if(ae.getSource() == button.get(6)){ 
      textField.setText(textField.getText().concat("1")); 
     } 
     if(ae.getSource() == button.get(7)){ 
      textField.setText(textField.getText().concat("1")); 
     } 
     if(ae.getSource() == button.get(8)){ 
      textField.setText(textField.getText().concat("1")); 
     } 
     if(ae.getSource() == button.get(9)){ 
      textField.setText(textField.getText().concat("1")); 
     } 
     if(ae.getSource() == button.get(10)){ 
      textField.setText(textField.getText().concat("1")); 
     } 
     if(ae.getSource() == button.get(11)){ 
      textField.setText(textField.getText().concat("1")); 
     } 
     if(ae.getSource() == button.get(12)){ 
      textField.setText(textField.getText().concat("1")); 
     } 
     if(ae.getSource() == button.get(13)){ 
      textField.setText(textField.getText().concat("1")); 
     } 
     if(ae.getSource() == button.get(14)){ 
      textField.setText(textField.getText().concat("1")); 
     } 
     if(ae.getSource() == button.get(15)){ 
      textField.setText(textField.getText().concat("1")); 
     } 
    }  
} 
+0

名前が付いている場合がありますので、重要です。常に正しい名前を使用してください!私たちのケースでは、コレクションのSngular名を使用すると混乱し、エラーをサポートしてデバッグを難しくしました! –

答えて

4

あなたbutton.addActionListener(this);べきbutton.get(i).addActionListener(this);となります。言われて、あなたは、各反復ではなく、ArrayListのからあなたは後でそのループの中でそれを変更するたびに、それを取り出す作成ボタンへの参照を保持する必要があること

while (iterator.hasNext()) { 
     JButton newButton = new JButton(iterator.next()); 
     button.add(newButton); 
     container.add(newButton); 
     panel2.add(newButton); 
     newButton.addActionListener(this); 
} 
+0

あなたは男です。すみません。私は何を考えていたのか分からない!! –

3

あなたのボタンの変数がありますArrayListの、そしてので、この:それはArrayListにActionListenerを追加しても意味がありませんので、

button.addActionListener(this); 

は、意味がありません。によって保持されているJButton要素にActionListenerを追加したいとします。だから... ...

button.get(i).addActionListener(this); 

その他の問題:static修飾子を使用して上-、これは、それは非常に融通の利かないでしょうとさえ将来のバグのリスクを高めることができ

  • あなたのコードがあります。 Javaはオブジェクト指向の言語であり、OOPの方法でアプリケーションを作成する場合に最適です。
  • あなたは絶対にやりたくない:catch(NullPointerException e){。あなたのコードがNPEをスローすると、コードが壊れていて、修正する必要があります。
関連する問題