在Java中使用2个JButton的问题

时间:2011-12-02 18:19:15

标签: java swing jbutton

我正在制作一个程序,用两个计算中的一个从两个输入的数字中返回一个值。 但我不能让第二个JButton工作。当我输入两个值并单击“获取替代定义”时,它不返回任何内容。 我已经测试了我的AltDefinition类,它正在进行探测。 任何人都可以帮我找到这段代码中的问题吗?:

public class BinomialCoefficient implements ActionListener {
    private JTextField tJTextFieldResult;
    private JTextField tTextNumber1;
    private JTextField tTextNumber2;

    public BinomialCoefficient(String pString) {
            JFrame tJFrame = new JFrame(pString);

            tJTextFieldResult = new JTextField("Enter value n and k. Note: 0 < k < n < 60", 20);
            tTextNumber1 = new JTextField("n", 10);
            tTextNumber2 = new JTextField("k", 10);

            JButton tButton1 = new JButton("Get BinomialCoefficient");
            JButton tButton2 = new JButton("Get Alternative Definition");

            tButton1.addActionListener(this);
            tButton2.addActionListener(this);

            Container tContentPane = tJFrame.getContentPane();

            tContentPane.add(tJTextFieldResult, BorderLayout.NORTH);
            tContentPane.add(tTextNumber1, BorderLayout.WEST);
            tContentPane.add(tTextNumber2, BorderLayout.EAST);
            tContentPane.add(tButton1, BorderLayout.CENTER);
            tContentPane.add(tButton2, BorderLayout.SOUTH);

            tJFrame.pack();
            tJFrame.setVisible(true);
        }

    public void actionPerformed(ActionEvent pActionEvent) {
            String tCommand = pActionEvent.getActionCommand();
            int number1 = new Integer(tTextNumber1.getText()).intValue();
            int number2 = new Integer(tTextNumber2.getText()).intValue();

            if (number1 > 60 || number2 > 60 || number1 <= 0 || number2 <= 0 || number1 < number2 ) {
                tJTextFieldResult.setText("Please follow these conditions: 0 < k < n < 60");
            } else if
             (tCommand.equals("Get BinomialCoefficient")) {
                try {
                    final double Result = Conditions.GetBiCoefficient(number1, number2);
                    tJTextFieldResult.setText("" + Result); 
                } catch (IOException e) {
                    e.printStackTrace();
                } 

                if (number1 > 60 || number2 > 60 || number1 <= 0 || number2 <= 0 || number1 < number2 ) {
                    tJTextFieldResult.setText("Please follow these conditions: 0 < k < n < 60");
                } else if
                 (tCommand.equals("Get Alternative Definition")) {
                    try {
                        final double Result2 = AltDefinition.GetAltDefinition(number1, number2);
                        tJTextFieldResult.setText("" + Result2); 
                    } catch (IOException e) {
                        e.printStackTrace();
                    }   
                }
            }
    }
}   

1 个答案:

答案 0 :(得分:2)

如果检查它是否等于“获取BinomialCoefficient”,那么在你的另一个。

如果它等于“Get BinomialCoefficient”,那么它显然不能等于“Get Alternative Definition”。重新思考动作处理程序的流程。

编辑:它实际上看起来你只是忘了一个右大括号。这应该纠正它:

if (tCommand.equals("Get Alternative Definition")) {