oBox如何在单击JButton时从两个JComboBox计算两

oBox如何在单击JButton时从两个JComboBox计算两

本文介绍了JComboBox如何在单击JButton时从两个JComboBox计算两个整数并将结果存储在JTextfield中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个JComboBox,由数字combobox1= 1 to 5和c ombobox2= 1 to 6.

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

,当我单击我的JButton时,我希望将两个选定的数字相加并显示在Textfield.

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

除了计算以及如何在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){


            }
        });



  form.show();

}




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

    }

}

请帮助.

推荐答案

提供的 sscce 显然是家庭作业,但演习的目标尚不明确.有几件事值得注意.

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

  1. GUI应该构建在事件分配线程上.

按照惯例,类名以首字母大写开头.

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

长度初始化应考虑可读性.

Lengthy initialization should be factored for readability.

添加到每个JComboBox的对象是类 Integer ,该模型为数学整数的子集建模.请注意为什么 valueOf() 通常应优先于构造方法使用."

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."

在需要更新actionPerformed()中的显示之前,无需将任何内容转换为String.因为 getSelectedItem() 返回类型为Object的值,结果必须转换为Integer;在本地构建的数据中这是绝对安全的.

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.

一旦恢复了Integer值,就很容易获得作为int值的总和.

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

sum最终转换为 String 被保留为练习.提示: String 具有合适的方法,其名称现在看起来似乎很熟悉.

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.

布局管理器视觉指南 .

修订示例:

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

作为参考,Java 7引入了 ComboBoxModel<E> 安全地改进类型,尽管getSelectedItem()仍然向后兼容.

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

这篇关于JComboBox如何在单击JButton时从两个JComboBox计算两个整数并将结果存储在JTextfield中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:27