package javaGUI;
import java.awt.Color;
import java.awt.Container;import javax.swing.*;import java.awt.*;
import java.awt.event.*;public class Myjisuanqi extends JFrame implements ActionListener {
public JButton b1;
public JTextField jt1;
public JTextField jt2;
public JTextField jt3;
public JRadioButton r1,r2,r3,r4,r5;
public JLabel la1,la2,la3,la4,la5;
public ButtonGroup bg1=new ButtonGroup();
public Myjisuanqi(){
jt1 =new JTextField();
jt1.setBounds(20, 20, 50, 20);
jt2 =new JTextField();
jt2.setBounds(90, 20, 50, 20);
jt3 =new JTextField();
jt3.setBounds(160, 20, 50, 20);
r1 =new JRadioButton();
r1.setBounds(50, 50, 20, 20);
r1.addActionListener(this);
r2 =new JRadioButton();
r2.setBounds(110, 50, 20, 20);
r2.addActionListener(this);
r3 =new JRadioButton();
r3.setBounds(170, 50, 20, 20);
r3.addActionListener(this);
r4 =new JRadioButton();
r4.setBounds(50, 100, 20, 20);
r4.addActionListener(this);
r5 =new JRadioButton();
r5.setBounds(110, 100, 20, 20);
r5.addActionListener(this);
b1 =new JButton("确定");
b1.setBounds(200,200, 80, 30);
b1.addActionListener(this);
la1 =new JLabel("+");
la1.setBounds(20, 50, 20, 20);
la2 =new JLabel("-");
la2.setBounds(80, 50, 20, 20);
la4 =new JLabel("");
la4.setBounds(75, 20, 10, 20);
la5 =new JLabel("=");
la5.setBounds(145, 20, 10, 20);
bg1.add(r1);
bg1.add(r2);
bg1.add(r3);
bg1.add(r4);
bg1.add(r5);
Container w=this.getContentPane();//获得当前窗口容器;
w.setLayout(null);
this.setTitle("毛毛虫的计算器");
this.setSize(300, 300);//设置窗口大小;
w.add(jt1);
w.add(jt2);
w.add(jt3);
w.add(r1);
w.add(r2);
w.add(r3);
w.add(r4);
w.add(r5);
w.add(b1);
w.add(la1);
w.add(la2);
w.add(la4);
w.add(la5);
this.setVisible(true);//显示窗口;
}
public static void main(String[] args){
Myjisuanqi f=new Myjisuanqi();
}
public void actionPerformed(ActionEvent e){
int x1=Integer.parseInt(jt1.getText());
int x2=Integer.parseInt(jt2.getText());
if
(e.getSource()==r1)
la4.setText("+");
jt3.setText(String.valueOf(x1+x2));
if(e.getSource()==r2)
la4.setText("-");
jt3.setText(String.valueOf(x1-x2));
if(e.getSource()==r4)
la4.setText("*");
jt3.setText(String.valueOf(x1*x2));
if(e.getSource()==r5)
la4.setText("/");
jt3.setText(String.valueOf(x1/x2));
}
}
一个简单的计算器 , 但就是不行 运行时 点击单选按钮时出错。
不知道 哪位高手能帮帮忙。谢谢
import java.awt.Color;
import java.awt.Container;import javax.swing.*;import java.awt.*;
import java.awt.event.*;public class Myjisuanqi extends JFrame implements ActionListener {
public JButton b1;
public JTextField jt1;
public JTextField jt2;
public JTextField jt3;
public JRadioButton r1,r2,r3,r4,r5;
public JLabel la1,la2,la3,la4,la5;
public ButtonGroup bg1=new ButtonGroup();
public Myjisuanqi(){
jt1 =new JTextField();
jt1.setBounds(20, 20, 50, 20);
jt2 =new JTextField();
jt2.setBounds(90, 20, 50, 20);
jt3 =new JTextField();
jt3.setBounds(160, 20, 50, 20);
r1 =new JRadioButton();
r1.setBounds(50, 50, 20, 20);
r1.addActionListener(this);
r2 =new JRadioButton();
r2.setBounds(110, 50, 20, 20);
r2.addActionListener(this);
r3 =new JRadioButton();
r3.setBounds(170, 50, 20, 20);
r3.addActionListener(this);
r4 =new JRadioButton();
r4.setBounds(50, 100, 20, 20);
r4.addActionListener(this);
r5 =new JRadioButton();
r5.setBounds(110, 100, 20, 20);
r5.addActionListener(this);
b1 =new JButton("确定");
b1.setBounds(200,200, 80, 30);
b1.addActionListener(this);
la1 =new JLabel("+");
la1.setBounds(20, 50, 20, 20);
la2 =new JLabel("-");
la2.setBounds(80, 50, 20, 20);
la4 =new JLabel("");
la4.setBounds(75, 20, 10, 20);
la5 =new JLabel("=");
la5.setBounds(145, 20, 10, 20);
bg1.add(r1);
bg1.add(r2);
bg1.add(r3);
bg1.add(r4);
bg1.add(r5);
Container w=this.getContentPane();//获得当前窗口容器;
w.setLayout(null);
this.setTitle("毛毛虫的计算器");
this.setSize(300, 300);//设置窗口大小;
w.add(jt1);
w.add(jt2);
w.add(jt3);
w.add(r1);
w.add(r2);
w.add(r3);
w.add(r4);
w.add(r5);
w.add(b1);
w.add(la1);
w.add(la2);
w.add(la4);
w.add(la5);
this.setVisible(true);//显示窗口;
}
public static void main(String[] args){
Myjisuanqi f=new Myjisuanqi();
}
public void actionPerformed(ActionEvent e){
int x1=Integer.parseInt(jt1.getText());
int x2=Integer.parseInt(jt2.getText());
if
(e.getSource()==r1)
la4.setText("+");
jt3.setText(String.valueOf(x1+x2));
if(e.getSource()==r2)
la4.setText("-");
jt3.setText(String.valueOf(x1-x2));
if(e.getSource()==r4)
la4.setText("*");
jt3.setText(String.valueOf(x1*x2));
if(e.getSource()==r5)
la4.setText("/");
jt3.setText(String.valueOf(x1/x2));
}
}
一个简单的计算器 , 但就是不行 运行时 点击单选按钮时出错。
不知道 哪位高手能帮帮忙。谢谢
public void actionPerformed(ActionEvent e) {
int x1 = Integer.parseInt(jt1.getText());
int x2 = Integer.parseInt(jt2.getText());
if (e.getSource() == r1) {
la4.setText("+");
jt3.setText(String.valueOf(x1 + x2));
}
if (e.getSource() == r2) {
la4.setText("-");
jt3.setText(String.valueOf(x1 - x2));
}
if (e.getSource() == r4) {
la4.setText("*");
jt3.setText(String.valueOf(x1 * x2));
}
if (e.getSource() == r5) {
la4.setText("/");
jt3.setText(String.valueOf(x1 / x2));
}
}
String txt1 = jt1.getText();
String txt2 = jt2.getText();
int x1 = 0;
int x2 = 0;
if (txt1 != null && txt1.trim().length() > 0) {
try {
x1 = Integer.parseInt(txt1);
} catch (Exception ex) {
jt1.setText("");
jt1.requestFocus(true);
return;
}
} else {
jt1.setText("");
jt1.requestFocus(true);
return;
}
if (txt2 != null && txt2.trim().length() > 0) {
try {
x2 = Integer.parseInt(txt2);
} catch (Exception ex) {
jt2.setText("");
jt2.requestFocus(true);
return;
}
} else {
jt2.setText("");
jt2.requestFocus(true);
return;
}
if (e.getSource() == r1) {
la4.setText("+");
jt3.setText(String.valueOf(x1 + x2));
}
if (e.getSource() == r2) {
la4.setText("-");
jt3.setText(String.valueOf(x1 - x2));
}
if (e.getSource() == r4) {
la4.setText("*");
jt3.setText(String.valueOf(x1 * x2));
}
if (e.getSource() == r5) {
if (x2 == 0) {
jt3.setText("除数为0");
jt2.setText("");
jt2.requestFocus(true);
return;
}
la4.setText("/");
jt3.setText(String.valueOf(x1 / x2));
} }
int x1=Integer.parseInt(jt1.getText());
int x2=Integer.parseInt(jt2.getText());
出错就在这,如果两个都输入的话,就不会报错了!做个判断是否等于空字符串吧