Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
728 views
in Technique[技术] by (71.8m points)

swing - how to compute the result of two integer values but get the additive or multiplicative operator from a jComboBox in java

assume i have integer variables a,b and c.

c = a + b; or c = a - b; or c = a / b; or c = a * b;

As you can see, the computation operator needs to be passed dynamically at run time. So i have a jComboBox of operators, so a user will select either a +, -, * or / from the jcomboBox.

how do i get the jCombobox selectedItem(which will be either /, *, -, or + ) and use it in getting the value of c.

Eg. if the user selects *, then the expression should be c = a * b else if the user selected say +, then the expression should be c = a + b.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is a way to 'cheat'. All the parsing is done by the ScriptEngine, we just need to assemble the parts of the expression.

ScriptEngine Calculator

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

class ScriptEngineCalculations {

    public static void main(String[] args) {
        final ScriptEngine engine = new ScriptEngineManager().
            getEngineByExtension( "js" );

        String[] ops = {"+", "-", "*", "/"};

        JPanel gui = new JPanel(new BorderLayout(2,2));
        JPanel labels = new JPanel(new GridLayout(0,1));
        gui.add(labels, BorderLayout.WEST);
        labels.add(new JLabel("a"));
        labels.add(new JLabel("operand"));
        labels.add(new JLabel("b"));
        labels.add(new JLabel("="));

        JPanel controls = new JPanel(new GridLayout(0,1));
        gui.add(controls, BorderLayout.CENTER);
        final JTextField a = new JTextField(10);
        controls.add(a);
        final JComboBox operand = new JComboBox(ops);
        controls.add(operand);
        final JTextField b = new JTextField(10);
        controls.add(b);
        final JTextField output = new JTextField(10);
        controls.add(output);

        ActionListener al = new ActionListener(){
            public void actionPerformed(ActionEvent ae) {
                String expression =
                    a.getText() +
                    operand.getSelectedItem() +
                    b.getText();
                try {
                    Object result = engine.eval(expression);
                    if (result==null) {
                        output.setText( "Output was 'null'" );
                    } else {
                        output.setText( result.toString() );
                    }
                } catch(ScriptException se) {
                    output.setText( se.getMessage() );
                }
            }
        };

        // do the calculation on event.
        operand.addActionListener(al);
        a.addActionListener(al);
        b.addActionListener(al);

        JOptionPane.showMessageDialog(null, gui);
    }
}

See Also


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...