帮忙看看这个java程序有什么问题
import java.awt.*;
import java.awt.event.*;
public class Totalpanel extends java.applet.Applet implements MouseLister
{public static void main(String[] args){
Totalpanel w=new Totalpanel();
w.realp();}
public void realp(){
Frame ff=new Frame("加法计算器");
GridBagLayout gr=new GridBagLayout();
GridBagConstraints gc=new GridBagConstraints();
ff.setLayout(gr);
TextField text1,text2,text3,text4;
Label lb;
Button button1, button2;
button1=new Button("=");
button2=new Button("clear");
lb=new Label("+");
text1=new TextField(20);
text2=new TextField(20);
text3=new TextField(20);
text4=new TextField(20);
gc.fill=GridBagConstraints.BOTH;
gc.weightx=11;
gc.weighty=11;
gr.setConstraints(text1,gc);
gc.weightx=22;
gr.setConstraints(lb,gc);
gc.weightx=33;
gr.setConstraints(text2,gc);
gc.weightx=44;
gr.setConstraints(button1,gc);
gc.weightx=55;
gc.gridwidth=GridBagConstraints.REMAINDER;
gr.setConstraints(text3,gc);
GridBagConstraints gc1=new GridBagConstraints();
gc1.fill=GridBagConstraints.BOTH;
gc1.weightx=11;
gc1.weighty=22;
gr.setConstraints(text3,gc1);
gc1.weightx=22;
gc1.weighty=22;
gc1.gridwidth=GridBagConstraints.REMAINDER;
gr.setConstraints(button2,gc1);
ff.addMouseLister(this);
ff.setSize(500, 300);
ff.add(text1);
ff.add(lb);
ff.add(text2);
ff.add(button1);
ff.add(text3);
ff.add(text4);
ff.add(button2);
ff.setVisible(true);
}
public void mouseClicked(MouseEvent e)
{int s,r,u;
s=text1.getText();
r=text2.getText();
u=s+r;
text3.setText(u);}
public void mouseMoved(MouseEvent e){}
public void mouseDragged(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
}
答案:楼主我把你的程序几个比较明显的错误改了一下,至于里面的功能你自己再试下:
我修改了一下,你看看能否运行,图片应该是你运行出来的效果吧?
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;;
public class Totalpanel extends java.applet.Applet implements MouseListener {
public static void main(String[] args) {
Totalpanel w = new Totalpanel();
w.realp();
}
TextField text1, text2, text3, text4;
public void realp() {
Frame ff = new Frame("加法计算器");
GridBagLayout gr = new GridBagLayout();
GridBagConstraints gc = new GridBagConstraints();
ff.setLayout(gr);
Label lb;
Button button1, button2;
button1 = new Button("=");
button2 = new Button("clear");
lb = new Label("+");
text1 = new TextField(20);
text2 = new TextField(20);
text3 = new TextField(20);
text4 = new TextField(20);
gc.fill = GridBagConstraints.BOTH;
gc.weightx = 11;
gc.weighty = 11;
gr.setConstraints(text1, gc);
gc.weightx = 22;
gr.setConstraints(lb, gc);
gc.weightx = 33;
gr.setConstraints(text2, gc);
gc.weightx = 44;
gr.setConstraints(button1, gc);
gc.weightx = 55;
gc.gridwidth = GridBagConstraints.REMAINDER;
gr.setConstraints(text3, gc);
GridBagConstraints gc1 = new GridBagConstraints();
gc1.fill = GridBagConstraints.BOTH;
gc1.weightx = 11;
gc1.weighty = 22;
gr.setConstraints(text3, gc1);
gc1.weightx = 22;
gc1.weighty = 22;
gc1.gridwidth = GridBagConstraints.REMAINDER;
gr.setConstraints(button2, gc1);
ff.addMouseListener(this);
ff.setSize(500, 300);
ff.add(text1);
ff.add(lb);
ff.add(text2);
ff.add(button1);
ff.add(text3);
ff.add(text4);
ff.add(button2);
ff.setVisible(true);
}
public void mouseClicked(MouseEvent e) {
String s;
String r, u;
s = text1.getText();
r = text2.getText();
u = s + r;
text3.setText(u);
}
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
你不是把触发加法的运算写在mouseClicked事件里了吗?你填完1、2个框后在空白的地方点一下鼠标。。应该是第一个框加上第二个框等于第三个框啊,第四个框你没用到啊。dad as
上一个:java 与数据库的问题??
下一个:Java中哪些东西可以产生异常