开发者

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

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

and when I click my JButton, I want the two chosen numbers to be added and shown on a Textfield.

I already have the complete code except for the calculation and how to have the result in the textfield.

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


public class exer1 extends JFrame{

    JFrame form = new JFrame ("haay");
    JButton btn = new JButton ("Compute");
    JTextField txt = new JTextField (10); 
    JComboBox cb1 = new JComboBox();
    JComboBox cb2 = new JComboBox();


public exer1(){

    form.getContentPane().setLayout(null);
    form.setSize (500,550);

    form.getContentPane().add(txt);
    form.getContentPane().add(btn);
    form.getContentPane().add(cb1);
    form.getContentPane().add(cb2);

    cb1.addItem(new Integer(1));
    cb1.addItem(new Integer(2));
    cb1.addItem(new Integer(3));
    cb1.addItem(new Integer(4));
    cb1.addItem(new Integer(5));
    cb2.addItem(new Integer(1));
    cb2.addItem(new Integer(2));
    cb2.addItem(new Integer(3));
    cb2.addItem(new Integer(4));
    cb2.addItem(new Integer(5));
    cb2.addItem(new Integer(6));

    txt.setBounds(150,90,100,30);
    btn.setBounds(40,170,100,40);
    cb1.setBounds(190,140,50,30);
    cb2.setBounds(190,190,50,30);

    btn.addActionListener(new ActionListener(){
            public void actionPerformed (ActionEvent e){


          开发者_StackOverflow中文版  }
        });



  form.show();

}




    public static void main (String args []){
        exer1 xx = new exer1();

    }

}

Please help.


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.

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

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.


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜