将使用两个滑块生成的两个数字相加的 Java 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15466530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:42:17  来源:igfitidea点击:

Java program that adds two numbers generated by using two sliders

javaswingsliders

提问by user2180239

I have this assignment for my java class where I must add two numbers using sliders, and I'm really stuck. Here is my code:

我的 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));
        }
        } 

}
}

When I go to run it it does not want to work. I'm probably missing something but I can't seem to figure it out. Am I even correct on any of it?

当我去运行它时,它不想工作。我可能遗漏了一些东西,但我似乎无法弄清楚。我对其中的任何一个都正确吗?

回答by Hovercraft Full Of Eels

This:

这:

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

makes no sense. Shoot -- it doesn't even compile since ActionEvent's don't have a getValue()method. You don't get the ActionEvent's "value" but rather you get its actionCommand String via getActionCommand(), and then you can compare it with other Strings using String's equals(...)or equalsIgnoreCase(...)method.

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

i.e.,

IE,

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

In future questions, please post all compiler errors that occur if you get any.

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

回答by Reimeus

You have 2 sources of compilation error here.

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

JSlider#getValuereturns an intvalue so there's no need to use Integer.parseIntwhich expects a String. Instead you can use:

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

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

Also getValueis undefined for ActionEvent, getSourcewill give you the Objectreference for the JButton:

也是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));
}

回答by nisa

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));
        }
        } 

}
}