0

我为我的 java 类分配了这个任务,我必须使用滑块添加两个数字,我真的被卡住了。这是我的代码:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; 
import javax.swing.*;

public class slideCalculator {
JButton add,subtract;
JSlider num1st,num2nd;
JLabel answer;
public slideCalculator(){
    JFrame calc = new JFrame("Mediocre Calculator");
    num1st = new JSlider (0, 100, 0);
    num1st.setMajorTickSpacing (10);
    num1st.setMinorTickSpacing (5);
    num2nd = new JSlider (0, 100, 0);
    num2nd.setMajorTickSpacing (10);
    num2nd.setMinorTickSpacing (5);
    add = new JButton("+");
    subtract = new JButton("-");
    answer = new JLabel("");
    calc.setDefaultCloseOperation(calc.EXIT_ON_CLOSE);
    calc.setVisible(true);
    calc.setBounds(500, 200, 290, 210);
    calc.setLayout(new GridLayout(0,2,1,1));
    calc.add(new JLabel("First Number: "));
    calc.add(num1st);
    calc.add(new JLabel("Second Number: "));
    calc.add(num2nd);
    calc.add(new JLabel("Answer: "));
    calc.add(answer);
    calc.add(add);
    calc.add(subtract);
    add.addActionListener(new action());
    subtract.addActionListener(new action());

}
public static void main(String[] args) {
    slideCalculator lc = new slideCalculator();
}
public class action implements ActionListener {
    public void actionPerformed(ActionEvent ae) {
        int firstNum = Integer.parseInt(num1st.getValue());
        int secondNum = Integer.parseInt(num2nd.getValue());
        if(ae.getValue()== add){
            answer.setText(String.valueOf(firstNum+secondNum));
        }
        else if (ae.getValue()==subtract){
            answer.setText(String.valueOf(firstNum-secondNum));
        }
        } 

}
}

当我去运行它时,它不想工作。我可能错过了一些东西,但我似乎无法弄清楚。我什至是正确的吗?

4

3 回答 3

5

这个:

public void actionPerformed(ActionEvent ae) {
    int firstNum = Integer.parseInt(num1st.getValue());
    int secondNum = Integer.parseInt(num2nd.getValue());
    if(ae.getValue()== add){  // *****

没有意义。射击——它甚至无法编译,因为 ActionEvent 没有getValue()方法。您不会获得 ActionEvent 的“值”,而是通过 获得其 actionCommand 字符串getActionCommand(),然后您可以使用 String 的equals(...)orequalsIgnoreCase(...)方法将其与其他字符串进行比较。

IE,

if ("+".equals(ae.getActionCommand()) {
   // do addition
} else if ("-".equals(ae.getActionCommand()) {
   // do subtraction
}

在以后的问题中,如果您遇到任何编译器错误,请发布。

于 2013-03-17T21:44:11.597 回答
3

您在这里有 2 个编译错误来源。

JSlider#getValue返回一个int值,因此无需使用Integer.parseIntwhich 期望String. 相反,您可以使用:

int firstNum = num1st.getValue();
int secondNum = num2nd.getValue();

getValue未定义ActionEventgetSource将为您提供Object参考JButton

Object source = ae.getSource();
if (ae.getSource() == add) {
   answer.setText(String.valueOf(firstNum + secondNum));
} else if (source == subtract) {
   answer.setText(String.valueOf(firstNum - secondNum));
}
于 2013-03-17T21:42:52.817 回答
0
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; 
import javax.swing.*;

public class slideCalculator {
JButton add,subtract;
JSlider num1st,num2nd;
JLabel answer;
public slideCalculator(){
    JFrame calc = new JFrame("Mediocre Calculator");
    num1st = new JSlider (0, 100, 0);
    num1st.setMajorTickSpacing (10);
    num1st.setMinorTickSpacing (5);
    num2nd = new JSlider (0, 100, 0);
    num2nd.setMajorTickSpacing (10);
    num2nd.setMinorTickSpacing (5);
    add = new JButton("+");
    subtract = new JButton("-");
    answer = new JLabel("");
    calc.setDefaultCloseOperation(calc.EXIT_ON_CLOSE);
    calc.setVisible(true);
    calc.setBounds(500, 200, 290, 210);
    calc.setLayout(new GridLayout(0,2,1,1));
    calc.add(new JLabel("First Number: "));
    calc.add(num1st);
    calc.add(new JLabel("Second Number: "));
    calc.add(num2nd);
    calc.add(new JLabel("Answer: "));
    calc.add(answer);
    calc.add(add);
    calc.add(subtract);
    add.addActionListener(new action());
    subtract.addActionListener(new action());

}
public static void main(String[] args) {
    slideCalculator lc = new slideCalculator();
}
public class action implements ActionListener {
    public void actionPerformed(ActionEvent ae) {
        int firstNum = num1st.getValue();
        int secondNum = num2nd.getValue();
        if("+".equals(ae.getActionCommand())) {
            answer.setText(String.valueOf(firstNum+secondNum));
        }
        else 
        if ("-".equals(ae.getActionCommand())) {
            answer.setText(String.valueOf(firstNum-secondNum));
        }
        } 

}
}
于 2013-12-20T11:12:39.980 回答