小问题(急)

playboyxp 2003-10-16 05:45:54
编译能通过,但运行有异常
最大的问题是怎么把数字转换成字符串
题目是点Add加一,点Minus减一
import java.awt.*;
import java.awt.event.*;
public class AddMinus extends WindowAdapter implements ActionListener
{
private Frame frm;
private Panel pan1,pan2;
private TextField note;
private Button b1,b2,b3;
public void AddMinus()
{
frm=new Frame("Add and Minus");
frm.setLayout(new BorderLayout());
pan1=new Panel();
pan2=new Panel();
pan1.setLayout(new FlowLayout());
pan2.setLayout(new FlowLayout());
b1=new Button("Add");
b1.addActionListener(this);
b2=new Button("Minus");
b2.addActionListener(this);
b3=new Button("Exit");
b3.addActionListener(this);
note=new TextField();
pan1.add(note);
pan2.add(b1);
pan2.add(b2);
pan2.add(b3);
frm.add(pan1,BorderLayout.CENTER);
frm.add(pan2,BorderLayout.SOUTH);
frm.setSize(400,300);
frm.addWindowListener(this);
frm.pack();
frm.show();
}
public void WindowClosing(WindowEvent e)
{
System.exit(0);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Add"))
{
note.setText("");
int a;
a=Integer.parseInt(note.getText())+1;
String s=""+a;
note.setText(s);
}
else if(e.getActionCommand().equals("Minus"))
{
note.setText("");
int a;
a=Integer.parseInt(note.getText())-1;
String s=""+a;
note.setText(s);
}
else System.exit(0);
}
public static void main(String args[])
{
AddMinus e=new AddMinus();
}
}
...全文
84 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
LoveRose 2003-10-17
  • 打赏
  • 举报
回复
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
playboyxp 2003-10-16
  • 打赏
  • 举报
回复
上面的问题解决了
在问一下,为什么按了窗口右上角的关闭关闭不了?
wli 2003-10-16
  • 打赏
  • 举报
回复
改了一下,可以工作

import java.awt.*;
import java.awt.event.*;

public class AddMinus extends WindowAdapter implements ActionListener {
private Frame frm;
private Panel pan1,pan2;
private TextField note;
private Button b1,b2,b3;

public AddMinus() {
frm = new Frame("Add and Minus");
frm.setLayout(new BorderLayout());
pan1 = new Panel();
pan2 = new Panel();
pan1.setLayout(new FlowLayout());
pan2.setLayout(new FlowLayout());
b1 = new Button("Add");
b1.addActionListener(this);
b2 = new Button("Minus");
b2.addActionListener(this);
b3 = new Button("Exit");
b3.addActionListener(this);
note = new TextField();
pan1.add(note);
pan2.add(b1);
pan2.add(b2);
pan2.add(b3);
frm.add(pan1, BorderLayout.CENTER);
frm.add(pan2, BorderLayout.SOUTH);
frm.setSize(400, 300);
frm.addWindowListener(this);
frm.pack();
frm.show();
}

public void WindowClosing(WindowEvent e) {
System.exit(0);
}

public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Add")) {
//note.setText("");
int a;
try {
a = Integer.parseInt(note.getText()) + 1;
} catch (NumberFormatException e1) {
a = 0;
}
String s = "" + a;
note.setText(s);
} else if (e.getActionCommand().equals("Minus")) {
//note.setText("");
int a;
try {
a = Integer.parseInt(note.getText()) - 1;
} catch (NumberFormatException e2) {
a = 0;
}
String s = "" + a;
note.setText(s);
} else
System.exit(0);
}

public static void main(String args[]) {
AddMinus e = new AddMinus();
}
}
LoveRose 2003-10-16
  • 打赏
  • 举报
回复
把下面代码里的东西去掉note.setText("");抛异常的


if(e.getActionCommand().equals("Add"))
{
× note.setText("");×
int a;
a=Integer.parseInt(note.getText())+1;
String s=""+a;
note.setText(s);
}
else if(e.getActionCommand().equals("Minus"))
{
× note.setText("");×
int a;
a=Integer.parseInt(note.getText())-1;
String s=""+a;
note.setText(s);
}
LoveRose 2003-10-16
  • 打赏
  • 举报
回复
public void AddMinus()
这是构造函数没有返回值的
去掉void写成public AddMinus()
就可以了
blacksun8334 2003-10-16
  • 打赏
  • 举报
回复
Integer.toSring() or Double.toSring()
playboyxp 2003-10-16
  • 打赏
  • 举报
回复
运行以后连窗口也没显示
playboyxp 2003-10-16
  • 打赏
  • 举报
回复
还是不行!!
beming 2003-10-16
  • 打赏
  • 举报
回复
or:
int to string : Integer.toString(123);

2, the same as vcshcn(xx)
vcshcn 2003-10-16
  • 打赏
  • 举报
回复
1 + ""
2 try { a=Integer.parseInt(note.getText())+1; } catch

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧