Topic: 各大虾帮忙看看~谢谢 |
Print this page |
1.各大虾帮忙看看~谢谢 | Copy to clipboard |
Posted by: kakaka0 Posted on: 2004-12-02 10:53 我的程序不知道什么错误,我是菜鸟帮帮忙吧!! import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Adder implements ActionListener { JFrame AdderFame; JTextField TOprand1; JTextField TOprand2; JLabel LAdd,LSum; JButton BAdd; int i=0; public Adder() { AdderFrame=new JFrame("AdderFrame"); TOprand1=new JTextField("0.0"); TOprand2=new JTextField("0.0"); LAdd=new JLabel("+"); LSum=new JLabel("= "); BAdd=new JButton("Add!"); AdderFrame.getContentPane().setLayout(new FlowLayout()); AdderFrame.getContentPane().add(TOprand1); AdderFrame.getContentPane().add(LAdd); AdderFrame.getContentPane().add(TOprand2); AdderFrame.getContentPane().add(LSum); AdderFrame.getContentPane().add(BAdd); AdderFrame.pack(); AdderFrame.setVisible(true); BAdd.addActionListener(this); public void actionPerformed(ActionEvent event) { i=(i=1)%2; if(i==1) { int sum=(int)(Double.valueOf(TOprand1.getText()).doubleValue()+Double.valueOf(TOprand.getText()).doubleValue()); LSum.setText("="+sum); }else{ TOpand1.setText("0.0"); TOpand2.serText("0.0"); LSum.setText("="); }; } AdderFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public static void main(String[] args){ Adder adder=new Adder();} } 错误:--------------------Configuration: <Default>-------------------- C:\Adder.java:32: illegal start of expression public void ActionPerformed(ActionEvent event) ^ 1 error Process completed. |
2.Re:各大虾帮忙看看~谢谢 [Re: kakaka0] | Copy to clipboard |
Posted by: miaomiao9527 Posted on: 2004-12-03 15:04 把public去掉 马原因我也不清楚 |
3.Re:各大虾帮忙看看~谢谢 [Re: kakaka0] | Copy to clipboard |
Posted by: yorksaddam Posted on: 2004-12-03 22:01 你的构造函数有问题,应该是没有返回值的 |
4.Re:各大虾帮忙看看~谢谢 [Re: kakaka0] | Copy to clipboard |
Posted by: kakaka0 Posted on: 2004-12-05 21:37 有人帮我改成这样就没事 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Adder implements ActionListener { public JFrame AdderFrame; public JTextField TOprand1; public JTextField TOprand2; public JLabel LAdd,LSum; public JButton BAdd; public int i=0; public void actionPerformed(ActionEvent event) { i=(i=1)%2; if(i==1) { int sum=(int)(Double.valueOf(TOprand1.getText()).doubleValue()+Double.valueOf(TOprand1.getText()).doubleValue()); LSum.setText("="+sum); }else{ TOprand1.setText("0.0"); TOprand2.setText("0.0"); LSum.setText("="); } } public Adder() { AdderFrame=new JFrame("AdderFrame"); TOprand1=new JTextField("0.0"); TOprand2=new JTextField("0.0"); LAdd=new JLabel("+"); LSum=new JLabel("= "); BAdd=new JButton("Add!"); AdderFrame.getContentPane().setLayout(new FlowLayout()); AdderFrame.getContentPane().add(TOprand1); AdderFrame.getContentPane().add(LAdd); AdderFrame.getContentPane().add(TOprand2); AdderFrame.getContentPane().add(LSum); AdderFrame.getContentPane().add(BAdd); AdderFrame.pack(); AdderFrame.setVisible(true); BAdd.addActionListener(this); // // public void actionPerformed(ActionEvent event) // { // i=(i=1)%2; // if(i==1) // { // int sum=(int)(Double.valueOf(TOprand1.getText()).doubleValue()+Double.valueOf(TOprand.getText()).doubleValue()); // LSum.setText("="+sum); // }else{ // TOpand1.setText("0.0"); // TOpand2.serText("0.0"); // LSum.setText("="); // } // } AdderFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public static void main(String[] args){ Adder adder=new Adder();} } 但按我以前的程序的顺序改回去就又不行了。究竟怎么回事呢? 知道的告诉我吧~~谢谢 |
5.Re:各大虾帮忙看看~谢谢 [Re: kakaka0] | Copy to clipboard |
Posted by: littledeer1974 Posted on: 2004-12-05 22:08 你的贴名改的更明确一点,会有更多的人进来帮你的,不要就只写[各大虾帮忙看看~谢谢],再写的具体一点,多传递一点信息好吗? |
6.Re:各大虾帮忙看看~谢谢 [Re: kakaka0] | Copy to clipboard |
Posted by: tcxmn Posted on: 2004-12-07 23:30 错误一: public void actionPerformed(ActionEvent event) {...................} 这个过程应该从构造器里移动出来,而你的程序中是放在构造器中了 错误二: JFrame AdderFame; 应该是AdderFrame拼写错误 错误三: int sum=(int)(Double.valueOf(TOprand1.getText()).doubleValue()+Double.valueOf(TOprand.getText()).doubleValue()); 应该是 int sum=(int)(Double.valueOf(TOprand1.getText()).doubleValue()+Double.valueOf(TOprand2.getText()).doubleValue()); TOprand2 错误四: TOpand1.setText("0.0"); TOpand2.serText("0.0");都应该改成TOprand1,TOprand2.并且是setText.你有个写成了serText. 最后楼主在发程序的时候要说明这个程序的目的,别人才好排错. |
7.Re:各大虾帮忙看看~谢谢 [Re: kakaka0] | Copy to clipboard |
Posted by: tcxmn Posted on: 2004-12-07 23:36 错误五: else{ TOpand1.setText("0.0"); TOpand2.serText("0.0"); LSum.setText("="); }; 后面的";"是多余的 |
8.Re:各大虾帮忙看看~谢谢 [Re: kakaka0] | Copy to clipboard |
Posted by: tcxmn Posted on: 2004-12-07 23:40 最终正确的代码如下: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Adder implements ActionListener { JFrame AdderFrame; JTextField TOprand1; JTextField TOprand2; JLabel LAdd, LSum; JButton BAdd; int i = 0; //构造器--------------------------------------------------- public Adder() { AdderFrame = new JFrame("AdderFrame"); TOprand1 = new JTextField("0.0"); TOprand2 = new JTextField("0.0"); LAdd = new JLabel("+"); LSum = new JLabel("= "); BAdd = new JButton("Add!"); AdderFrame.getContentPane().setLayout(new FlowLayout()); AdderFrame.getContentPane().add(TOprand1); AdderFrame.getContentPane().add(LAdd); AdderFrame.getContentPane().add(TOprand2); AdderFrame.getContentPane().add(LSum); AdderFrame.getContentPane().add(BAdd); AdderFrame.pack(); AdderFrame.setVisible(true); BAdd.addActionListener(this); AdderFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); }//构造器结束---------------------------------------- //实现ActionListener接口-------------------------------------------- public void actionPerformed(ActionEvent event) { i = (i = 1) % 2; if (i == 1) { int sum = (int) (Double.valueOf(TOprand1.getText()).doubleValue() + Double.valueOf(TOprand2.getText()).doubleValue()); LSum.setText("=" + sum); } else { TOprand1.setText("0.0"); TOprand2.setText("0.0"); LSum.setText("="); } } //程序的入口-------------------------------------------- public static void main(String[] args) { Adder adder = new Adder(); } } |
9.Re:各大虾帮忙看看~谢谢 [Re: kakaka0] | Copy to clipboard |
Posted by: tcxmn Posted on: 2004-12-07 23:45 奇怪,我原来有格式的,怎么发上去就没有了.不好意思了.... |
10.Re:各大虾帮忙看看~谢谢 [Re: kakaka0] | Copy to clipboard |
Posted by: kakaka0 Posted on: 2004-12-09 07:53 多谢各位帮忙~受益不浅 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |