java图形界面的事件处理
程序功能:在窗口中输入操作数1和操作数2,选择加减乘除中的一种运算,点击“计算”按钮,得到结果。结果输出在结果计算按钮右方,就是刚开始出现“请输入操作数”的位置。图像界面...
程序功能:在窗口中输入操作数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();
}
} 展开
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();
}
} 展开
1个回答
展开全部
以下就是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);
}
}
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);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |