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

后端 未结 3 551
盖世英雄少女心
盖世英雄少女心 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();
        }
    }
    

提交回复
热议问题