即时消息真的很新,我第一次创建按钮。我认为我有基本的想法,但它不起作用。任何你可以添加到我的代码的任何部分是非常有用的。帮帮我!这里是我的代码:在java中的按钮,创建和使用动作监听器
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class MovingBox extends Applet
{
Thread thread;
Dimension dim;
Image img;
Graphics g;
Color red = null;
Color blue = null;
Font fnt16P = null;
public void init()
{
resize(800,500);
Button b_Up = new Button("Up");
b_Up.setSize(100, 25);
b_Up.setLocation(450,450+ 90);
b_Up.setBackground(red);
b_Up.setForeground(blue);
b_Up.setFont(fnt16P);
b_Up.setVisible(true);
b_Up.addActionListener((ActionListener) this);
add(b_Up);
}
public void paint(Graphics gfx)
{
g.setColor(Color.green);
g.fillRect(0,0,800,500);
}
public void actionPerformed(ActionEvent event)
{
int value, total;;
Object cause = event.getSource();
if (cause == b_Up)
(
)
}
}
什么不在你当前的代码中工作?你还需要在你的'if(cause == b_Up)'之后交换'()'为'{}'。 – 2013-05-01 18:30:33
你得到什么错误? – Mavrick 2013-05-01 18:35:25