我为我的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));
}
}
}
}
当我去运行它时,它不想工作。我可能错过了一些东西,但我似乎无法弄明白。我甚至对任何一个都纠正了吗?
答案 0 :(得分: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的“值”,而是通过getActionCommand()
得到它的actionCommand String,然后你可以使用String的equals(...)
或equalsIgnoreCase(...)
方法将它与其他字符串进行比较。
即,
if ("+".equals(ae.getActionCommand()) {
// do addition
} else if ("-".equals(ae.getActionCommand()) {
// do subtraction
}
在以后的问题中,请发布所有编译错误。
答案 1 :(得分:3)
此处有2个编译错误源。
JSlider#getValue会返回int
值,因此无需使用期望Integer.parseInt
的{{1}}。相反,您可以使用:
String
int firstNum = num1st.getValue();
int secondNum = num2nd.getValue();
未定义getValue
,ActionEvent
会为getSource
提供Object
参考:
JButton
答案 2 :(得分: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));
}
}
}
}