请高手帮忙看下java的小程序

importjava.awt.BorderLayout;importjava.awt.GridLayout;importjava.awt.event.ActionEven... import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class CalculatorDemo implements ActionListener{
static InputPanel ip ;
static JButton y;
static JTextField t3;
public static void main(String[] args) {
JFrame f = new JFrame("欢迎使用计算机");
CalculatorPanel cp = new CalculatorPanel();
InputPanel ip = new InputPanel();
cp.init();
ip.init();
f.setLayout(new BorderLayout());
f.add(cp,BorderLayout.SOUTH);
f.add(ip,BorderLayout.NORTH);
f.setSize(350, 130);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);

}

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
double num1 =Double.parseDouble(ip.getNum1()) ;
double num2 =Double.parseDouble(ip.getNum2());
if(e.getSource()==y){
y.addActionListener(this);

double res = num1+num2;
t3.setText(String.valueOf(res));
}
}
}
class CalculatorPanel extends InputPanel implements ActionListener{
JRadioButton r1,r2,r3,r4;

void init(){
setLayout(new GridLayout(1,5));
JRadioButton r1 = new JRadioButton("加");
JRadioButton r2 = new JRadioButton("减");
JRadioButton r3 = new JRadioButton("乘");
JRadioButton r4 = new JRadioButton("除");
JButton y=new JButton("运算");
ButtonGroup b = new ButtonGroup();
b.add(r1);
b.add(r2);
b.add(r3);
b.add(r4);
b.add(y);
add(r1);
add(r2);
add(r3);
add(r4);
add(y);

y.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {

}

}
class InputPanel extends JPanel{
JTextField t1,t2,t3;
void init(){
JTextField t1 =new JTextField("",3);
JTextField t2 =new JTextField("",3);
JTextField t3 =new JTextField("",3);
setLayout(new GridLayout(3,2));
add(new JLabel("操作数1"));
add(t1);
add(new JLabel("操作数2"));
add(t2);
add(new JLabel("结果"));
add(t3);
}

public String getNum1(){
return t1.getText();
}
public String getNum2(){
return t2.getText();
}
}

为什么在运行的时候监听不管用啊 (现在只是要实现运算+功能),可以的话请帮忙改下 比顺便实现+ - * / 四个功能
展开
 我来答
席宏阔U6
2010-06-05 · TA获得超过686个赞
知道小有建树答主
回答量:142
采纳率:100%
帮助的人:215万
展开全部
import java.awt.*;
import java.awt.event.*;

public class CalculatorDemo {
public static void main(String[] args) {
Windows win = new Windows("欢迎使用计算机");
}
}

class Windows extends Frame implements ActionListener {
Button[] b = new Button[4];
static TextField t, t1, t2;
static String s;
static int i;

Windows(String m) {
super(m);
setLayout(new FlowLayout());
b[0] = new Button("加");
b[1] = new Button("减");
b[2] = new Button("乘");
b[3] = new Button("除");
t = new TextField(18);
t1 = new TextField(18);
t2 = new TextField(18);
t2.setEditable(false);
add(new Label("操作数1 "));
add(t);
add(new Label("操作数2 "));
add(t1);
add(new Label("结果 "));
add(t2);
for (int i = 0; i < 4; i++) {
add(b[i]);
b[i].addActionListener(this);
}
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setBounds(100, 100, 280, 150);
setVisible(true);
validate();
}

public static void SuanFa() {
if (Double.parseDouble(s.substring(s.indexOf("."))) == 0) {
t2.setText(s.substring(0, s.indexOf(".")));
} else
t2.setText(s);
}

public void actionPerformed(ActionEvent e) {
try {
double a = Double.parseDouble(t.getText());
double c = Double.parseDouble(t1.getText());
for (i = 0; i < 4; i++) {
switch (i) {
case 0:
s = String.valueOf(a + c);
break;
case 1:
s = String.valueOf(a - c);
break;
case 2:
s = String.valueOf(a * c);
break;
case 3:
s = String.valueOf(a / c);
break;
}
if (e.getSource() == b[i]) {
Windows.SuanFa();
}
}
} catch (NumberFormatException event) {
t2.setText("请输入数字");
}
}
}

我觉得这样清爽点。。而且能实现你要的功能。
其实,除了主类,你在用一个类调用ActionListener 和ItemListener(选择框 Checkbox类的监视器事件处理接口)两个接口,就能搞定你要的界面和要实现的功能了。
panzc126
2010-06-04 · TA获得超过154个赞
知道答主
回答量:95
采纳率:0%
帮助的人:0
展开全部
你的actionPerformed方法是CalculatorDemo这个类的,而 y.addActionListener(this)的this是CalculatorPanel这个类的,建议JAVA基础好好学,了解this是什么再用,不要滥用this,下面有个现成的计算器给你吧,拷贝过去就能用
public class MyComputer implements ActionListener{
private boolean appendnumber = false;
private boolean append = false;
private boolean flag = false;
private String temp1 = null;
private String op1 = null;
private JTextField jtf = new JTextField("0.",28);
public MyComputer(){
init();
}
private void init() {
/*
* 下面都是在画计算机界面你想看也可以,
* 不看也行,具体计算在public void
* actionPerformed(ActionEvent e)
* 里
*/
JFrame frame = new JFrame("我的计算器");
JPanel panelTop = new JPanel();
panelTop.setLayout(new FlowLayout());
panelTop.add(jtf);
JPanel panelBotton = new JPanel();
String[] str = {"BackSpace","CE","C","+",
"7","8","9","-",
"4","5","6","*",
"1","2","3","/",
"0","+/-",".","="};
JButton[] jb = new JButton[str.length];
panelBotton.setLayout(new GridLayout(5,4));
for(int i = 0;i < jb.length;i++)
{
jb[i] = new JButton(str[i]);
panelBotton.add(jb[i]);
jb[i].addActionListener(this);
}
frame.add(panelTop,BorderLayout.NORTH);
frame.add(panelBotton);
frame.setSize(400, 400);
frame.setVisible(true);
}
public static void main(String[] args) {
new MyComputer();
}
public void actionPerformed(ActionEvent e) {
String comm = e.getActionCommand();//获得button里的数据
/*
* "0123456789".indexOf(comm)!=-1是判断button里获得的数据
* 是0---9的情况
*/
if ("0123456789".indexOf(comm)!=-1) {
if(!appendnumber){
jtf.setText(comm);
appendnumber = true;
}else{
jtf.setText(jtf.getText()+comm);
}
}
/*
* 判断button里获得的数据是加减乘除的情况
*/
if("+-*/".indexOf(comm)!=-1){
//在遇到加减乘除的符号时我们先存储刚输入的值
temp1 = jtf.getText();
//然后存储符号
op1 = comm;
/*
* 然后可以重新输入数据到输入框,所以这时候我们
* 不能追加数据,设置追加数据标志为false
*/
appendnumber = false;
}
/*
* 判断button里获得的数据是等号的情况
*/
else if("=".equals(comm)) {
if(temp1==null||"".equals(temp1))return;
/*
* 遇到等号的时候,我们取出刚才保存的数据和当前输入的数据做运算
*/
double num1 = Double.parseDouble(temp1);//转化数据成double型的
double num2 = Double.parseDouble(jtf.getText());//转化数据成double型的
//取出刚才保存的符号看是加减乘除的那种运算
if("+".equals(op1)){
num1+=num2;
}
if("-".equals(op1)){
num1-=num2;
}
if ("*".equals(op1)) {
num1*=num2;
}
if("/".equals(op1)){
num1/=num2;
}
jtf.setText(num1+"");
appendnumber = false;
}
/*
* 判断button里获得的数据是小数点的情况
*/
else if (".".equals(comm)) {
if(jtf.getText().indexOf(".")==-1){
append = true;
}
if (append) {
jtf.setText(jtf.getText()+".");
append = false;
appendnumber = true;
}
}
/*
* 判断button里获得的数据是在数据前面加正负号的情况
*/
else if ("+/-".equals(comm)) {
if(!flag){
jtf.setText("-"+jtf.getText());
flag = true;
}
else{
String result = jtf.getText().substring(1, jtf.getText().length());
jtf.setText(result);
flag = false;
}
}
/*
* 判断button里获得的数据是清除输入框的情况
*/
else if ("C".equals(comm)) {
jtf.setText("0.");
appendnumber = false;
}
/*
* 判断button里获得的数据是清除输入框的情况
*/
else if ("CE".equals(comm)) {
jtf.setText("0.");
appendnumber = false;
}
/*
* 判断button里获得的数据是输入框的数据退一位的情况
*/
else if ("BackSpace".equals(comm)) {
if(jtf.getText()==null||"".equals(jtf.getText()))return;
String result = jtf.getText().substring(0, jtf.getText().length()-1);
jtf.setText(result+"");
appendnumber = true;
}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
windliu122
2010-06-05
知道答主
回答量:52
采纳率:0%
帮助的人:32万
展开全部
我做了一些改动,你自己看一下,我把按钮动作指向最原始的外框的动作,还有InputPanel的JTextField重复定义了,至于你的选择运算功能,你没写,我也就没弄了,只有加法功能,你自己对比下程序

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class CalculatorDemo implements ActionListener {
InputPanel ip;

CalculatorDemo(){
JFrame f = new JFrame("欢迎使用计算机");
CalculatorPanel cp = new CalculatorPanel();
ip = new InputPanel();
ip.init();
cp.init(this);

f.setLayout(new BorderLayout());
f.add(cp, BorderLayout.SOUTH);
f.add(ip, BorderLayout.NORTH);
f.setSize(350, 130);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
System.out.print("yes");

}

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
double num1 = Double.parseDouble(ip.getNum1());

double num2 = Double.parseDouble(ip.getNum2());

double res = num1 + num2;
ip.t3.setText(String.valueOf(res));
}

public static void main(String[] args){
CalculatorDemo cal = new CalculatorDemo();
System.out.print(cal.ip.getNum1());
}
}

class CalculatorPanel extends InputPanel{
JRadioButton r1, r2, r3, r4;

void init(CalculatorDemo cd) {
setLayout(new GridLayout(1, 5));
JRadioButton r1 = new JRadioButton("加");
JRadioButton r2 = new JRadioButton("减");
JRadioButton r3 = new JRadioButton("乘");
JRadioButton r4 = new JRadioButton("除");
JButton y = new JButton("运算");
ButtonGroup b = new ButtonGroup();
b.add(r1);
b.add(r2);
b.add(r3);
b.add(r4);
b.add(y);
add(r1);
add(r2);
add(r3);
add(r4);
add(y);

y.addActionListener(cd);
}

}

class InputPanel extends JPanel {
JTextField t1, t2, t3;

void init() {
t1 = new JTextField("", 3);
t2 = new JTextField("", 3);
t3 = new JTextField("", 3);
setLayout(new GridLayout(3, 2));
add(new JLabel("操作数1"));
add(t1);
add(new JLabel("操作数2"));
add(t2);
add(new JLabel("结果"));
add(t3);
}

public String getNum1() {
return t1.getText();
}

public String getNum2() {
return t2.getText();
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式