中易网

java图形界面的事件处理

答案:1  悬赏:80  
解决时间 2021-12-30 23:04
程序功能:在窗口中输入操作数1和操作数2,选择加减乘除中的一种运算,点击“计算”按钮,得到结果。结果输出在结果计算按钮右方,就是刚开始出现“请输入操作数”的位置。图像界面部分我已经做好,需要事件处理部分。谢谢代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Calculator extends JFrame implements ActionListener, WindowListener {
String resultStr;
String buts[]={"+","-","*","/"};
JRadioButton c[]=new JRadioButton[buts.length];
ButtonGroup bg = new ButtonGroup();
JButton but =new JButton("确定");
JPanel p1;
JPanel p2,p3,p4;
JTextField text1;
JTextField text2;
JLabel result=new JLabel("请输入操作数。");

public Calculator() {
super("简单计算器");
resultStr=new String();
setSize(600,450);
setLayout(new GridLayout(4,0));
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
text1=new JTextField("0",12);
text2=new JTextField("0",12);

p1.setLayout(new FlowLayout());
setFont(new Font("Arial",Font.BOLD,12));
p1.add(text1);
text1.add( new JLabel("操作数一:") );

for(int i=0; i<c.length; i++) {
c[i]=new JRadioButton(buts[i]);
c[i].addActionListener(this);
p2.add(c[i]);
bg.add(c[i]);

}

p3.setLayout(new FlowLayout());
p3.add(text2);
text2.add( new JLabel("操作数二:") );

p4.setLayout(new FlowLayout());
p4.add(but);
p4.add(result);
add(p1);
add(p2);
add(p3);
add(p4);
addWindowListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String str1=new String();

}
public void windowOpened(WindowEvent e) { }
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowClosed(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }
public void windowActivated(WindowEvent e) { }
public void windowDeactivated(WindowEvent e) { }
public static void main(String args[]){
Calculator myCal = new Calculator();
}
}
最佳答案
以下就是actionPerformed方法中的代码:并且我提示你一下,你写的JButton but = new JButton("确定"),没注册。but需要注册but.addActionListener(this);

public void actionPerformed(ActionEvent e) {
Integer num1= new Integer(this.text1.getText());
Integer num2 = new Integer(this.text2.getText());

if(this.c[0].isSelected()){
Integer num3 = num1+num2;
String num = num3.toString();
this.result.setText(num);
}
if(this.c[1].isSelected()){
Integer num3 = num1-num2;
String num = num3.toString();
this.result.setText(num);
}
if(this.c[2].isSelected()){
Integer num3 = num1*num2;
String num = num3.toString();
this.result.setText(num);
}
if(this.c[3].isSelected()){
Integer num3 = num1/num2;
String num = num3.toString();
this.result.setText(num);
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
计算机专业英语句子翻译
2015年农历3月21日几点出生的孩子好
衡阳到耒阳几公里? 城市到城市
薶血加书的意思是什么?薶血加书的释义是什么
2017年达一中招生考试怎么在网上报名?
男人梦见牵牛去配种
中医执业医师证求租,乡镇用,
硬实的意思是什么啊?请解释下!
绿珠万功山茶油专卖地址在哪,我要去那里办事
君子兰可不可以施草木灰
1991年农历正月14日生的人今年运势
推荐创意策划网站
徯落的意思是什么啊?请解释下!
謨烈的意思是什么?謨烈的释义是什么啊?
溆浦到北京的图上距离是多少
推荐资讯
休已的意思是什么?休已的释义是什么啊?
沟娄的意思是什么?沟娄的释义是什么啊?
别墅装修周期大概需要多久
斜傾的意思是什么?斜傾的释义是什么啊?
全士的意思是什么啊?请解释下!
城管在店铺没收音响后称店主态度不好,拖后不
代办的意思是什么?代办的释义是什么啊?
佛罗伦萨的意思是什么啊?请解释下!
衣服面料铜氨丝和聚酯纤维一样吗?
愁雾的意思是什么?愁雾的释义是什么啊?
阅读下面这首宋诗,完成小题。(11分)书友人
农业户口转城镇户口得有哪些证明
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?