JComboBox how to compute for two integers from two JComboBox and have the result in a JTextfield when clicking the JButton

后端 未结 3 525
盖世英雄少女心
盖世英雄少女心 2021-01-17 02:52

I have 2 JComboBox consisting of numbers combobox1= 1 to 5 and combobox2= 1 to 6.

and when I click my JButton, I

相关标签:
3条回答
  • 2021-01-17 03:36

    I've kind of rewritten the entire script (sorry for any inconvenience)...

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class exer1 extends JFrame implements ActionListener {
        JPanel row1 = new JPanel();
        JLabel first = new JLabel("Select the first number:", JLabel.RIGHT);
        JComboBox fNum = new JComboBox();
        JPanel row2 = new JPanel();
        JLabel second = new JLabel("Select the second number:", JLabel.RIGHT);
        JComboBox sNum = new JComboBox();
        JPanel row3 = new JPanel();
        JButton comp = new JButton("Compute");
        JTextField total = new JTextField(5);
    
        public exer1() {
            super("Calculator");
            row1.add(first);
            fNum.addItem("1");
            fNum.addItem("2");
            fNum.addItem("3");
            fNum.addItem("4");
            fNum.addItem("5");
            fNum.addItem("6");
            row1.add(fNum);
            add(row1);
            row2.add(second);
            sNum.addItem("1");
            sNum.addItem("2");
            sNum.addItem("3");
            sNum.addItem("4");
            sNum.addItem("5");
            sNum.addItem("6");
            row2.add(sNum);
            add(row2);
            comp.addActionListener(this);
            row3.add(comp);
            total.setEditable(false);
            row3.add(total);
            add(row3);
            setLayout(new FlowLayout());
            setSize(500, 550);
            setVisible(true);
        }
    
        public void actionPerformed(ActionEvent event) {
            int num1 = Integer.parseInt(fNum.getSelectedItem().toString());
            int num2 = Integer.parseInt(sNum.getSelectedItem().toString());
            total.setText("" + (num1 + num2));
        }
    
        public static void main(String[] args) {
            exer1 xx = new exer1();
        }
    }
    
    0 讨论(0)
  • 2021-01-17 03:42

    use form.setVisible(true);

    then:

        btn.addActionListener(new ActionListener() {
    
            public void actionPerformed(ActionEvent e) {
                int cb1Int = Integer.parseInt(cb1.getSelectedItem().toString());
                int cb2Int = Integer.parseInt(cb2.getSelectedItem().toString());
                txt.setText(String.valueOf(cb1Int + cb2Int));
            }
        });
    

    Good luck with your homework. ;)

    0 讨论(0)
  • 2021-01-17 03:47

    The sscce provided is clearly homework, but the goal of the exercise is not so clear. A few things are worth noting.

    1. The GUI should be constructed on the event dispatch thread.

    2. By convention, class names begin with an initial capital letter.

    3. Lengthy initialization should be factored for readability.

    4. The objects added to each JComboBox are instances of the class Integer, which model a subset of the mathematical integers. Note why valueOf() "should generally be used in preference to the constructor."

    5. There's no need to convert anything to a String until it's time to update the display in actionPerformed(). Because getSelectedItem() returns a value of type Object, the result must be cast to Integer; this is perfectly safe in the context of locally constructed data.

    6. Once the Integer values have been recovered, it's easy to obtain the sum as an int value.

    7. The final conversion of the sum to a String is left as an exercise. Hint: String has a suitable method, the name of which may now seem familiar.

    8. It's never too soon to become friends with A Visual Guide to Layout Managers.

    Revised example:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Exercise1 extends JFrame {
    
        JFrame form = new JFrame("Exercise1");
        JButton btn = new JButton("Compute");
        JTextField txt = new JTextField(10);
        JComboBox<Integer> cb1 = new JComboBox<>();
        JComboBox<Integer> cb2 = new JComboBox<>();
    
        public Exercise1() {
            form.setLayout(new GridLayout(0, 1));
            form.add(txt);
            form.add(btn);
            form.add(cb1);
            form.add(cb2);
            for (int i = 1; i <= 5; i++) {
                cb1.addItem(Integer.valueOf(i));
                cb2.addItem(Integer.valueOf(i));
            }
            cb2.addItem(new Integer(6));
            btn.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    Integer v1 = (Integer) cb1.getSelectedItem();
                    Integer v2 = (Integer) cb2.getSelectedItem();
                    int sum = v1.intValue() + v2.intValue();
                    txt.setText("42"); // really should convert sum to a String
                }
            });
    
            form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            form.pack();
            form.setVisible(true);
        }
    
        public static void main(String args[]) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    Exercise1 exercise1 = new Exercise1();
                }
            });
        }
    }
    

    For reference, Java 7 introduces ComboBoxModel<E> for improved type safely, although getSelectedItem() remains backward compatible.

    0 讨论(0)
提交回复
热议问题