JComboBox 如何从两个 JComboBox 计算两个整数,并在单击 JButton 时将结果保存在 JTextfield 中
我有 2 个 JComboBox
,由数字 combobox1= 1 to 5
和 combobox2= 1 to 6
组成。
当我单击我的 JButton 时
,我希望将两个选定的数字相加并显示在 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();
}
}
帮助。
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){
}
});
form.show();
}
public static void main (String args []){
exer1 xx = new exer1();
}
}
Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
提供的sscce显然是作业,但练习的目标却不是那么明确。有几件事值得注意。
GUI 应在事件调度线程上构建.
按照惯例,类名以首字母大写字母开头。
为了可读性,应该考虑冗长的初始化。
添加到每个
JComboBox
的对象是类Integer
,它对数学整数的子集进行建模。请注意为什么valueOf ()
“通常应该优先于构造函数使用。”在更新
actionPerformed()
中的显示之前,无需将任何内容转换为String
。因为getSelectedItem( )
返回Object
类型的值,结果必须转换为Integer
;这在本地构建的数据上下文中是完全安全的。一旦恢复了
Integer
值,就可以轻松获得int
值的总和。sum
到String
留作练习。提示:字符串
有一个合适的方法,其名称现在看起来可能很熟悉。与视觉效果成为朋友永远不会太早布局管理器指南。
修改后的示例:
作为参考,Java 7 引入了
ComboBoxModel
用于安全地改进类型,尽管getSelectedItem()
仍然向后兼容。The sscce provided is clearly homework, but the goal of the exercise is not so clear. A few things are worth noting.
The GUI should be constructed on the event dispatch thread.
By convention, class names begin with an initial capital letter.
Lengthy initialization should be factored for readability.
The objects added to each
JComboBox
are instances of the classInteger
, which model a subset of the mathematical integers. Note whyvalueOf()
"should generally be used in preference to the constructor."There's no need to convert anything to a
String
until it's time to update the display inactionPerformed()
. BecausegetSelectedItem()
returns a value of typeObject
, the result must be cast toInteger
; this is perfectly safe in the context of locally constructed data.Once the
Integer
values have been recovered, it's easy to obtain the sum as anint
value.The final conversion of the
sum
to aString
is left as an exercise. Hint:String
has a suitable method, the name of which may now seem familiar.It's never too soon to become friends with A Visual Guide to Layout Managers.
Revised example:
For reference, Java 7 introduces
ComboBoxModel<E>
for improved type safely, althoughgetSelectedItem()
remains backward compatible.我重写了整个脚本(很抱歉给您带来不便)...
I've kind of rewritten the entire script (sorry for any inconvenience)...
使用
form.setVisible(true);
那么:
祝你作业顺利。 ;)
use
form.setVisible(true);
then:
Good luck with your homework. ;)