零时给你写了个简单的:import java.awt.*;
import java.awt.event.*;public class MyTest extends Frame implements ActionListener{
Button b1 = new Button("Button1");
String str = "";
public void actionPerformed(ActionEvent e){
str = "red";
repaint();
}
public MyTest(){
setLayout(new FlowLayout());
add(b1);
b1.addActionListener(this);
setSize(400,300);
setVisible(true);
}
public void paint(Graphics g){
if(str.equals("red")){
g.setColor(Color.red);
g.drawString("MyTest",100,100);
}
}
public static void main(String[] args){
new MyTest();
}
}
import java.awt.event.*;public class MyTest extends Frame implements ActionListener{
Button b1 = new Button("Button1");
String str = "";
public void actionPerformed(ActionEvent e){
str = "red";
repaint();
}
public MyTest(){
setLayout(new FlowLayout());
add(b1);
b1.addActionListener(this);
setSize(400,300);
setVisible(true);
}
public void paint(Graphics g){
if(str.equals("red")){
g.setColor(Color.red);
g.drawString("MyTest",100,100);
}
}
public static void main(String[] args){
new MyTest();
}
}
我这儿就是不能repaint()!好奇怪啊!public void paint(Graphics g)
{
if (firstTime)
{
return;
}
//注:下面的m,n,lev就是action里由点击菜单项事件里赋值的
gameMap=new GameMap(m,n,lev);
inIcons=new ImageIcon[m][n];
icons=new Image[m][n];
for (int i=1;i<=m;i++)
{
for (int j=1;j<=n;j++)
{
int k=gameMap.map[i][j];
inIcons[i-1][j-1]=new ImageIcon
("D:/My Documents/HowToConnect/image/icon"+k+".jpg");
icons[i-1][j-1]=inIcons[i-1][j-1].getImage();
}
}
int posX=getWidth()/2-(m/2)*60;
int posY=(getHeight()-50)/2-(n/2)*60+30;
for (int j=0;j<n;j++)
{
for (int i=0;i<m;i++)
{
g.drawImage(icons[i][j],posX,posY,this);
posX+=60;
}
posX=getWidth()/2-(m/2)*60;
posY+=60;
}
}
public boolean action (Event e,Object o)
{
if (e.target instanceof MenuItem)
{
if (e.arg.equals("About"))
about=new About(this);
else if (e.arg.equals("Easy"))
{
//这里三处给m,n,lev赋值,然后调用repaint()方法啊!
m,n,lev是这个类的私有成员函数
level=1; m=12; n=7; lev=21;
firstTime=false;
repaint();
}
else if (e.arg.equals("Normal"))
{
level=2; m=14; n=8; lev=28;
firstTime=false;
repaint();
}
else if (e.arg.equals("Hard"))
{
level=3; m=16; n=9; lev=36;
firstTime=false;
repaint();
}
}
return true;
}
System.out.println("为什么不调用我?");试试
if (e.target instanceof MenuItem)
{
if (e.arg.equals("About"))
about=new About(this);
else if (e.arg.equals("Easy"))
{
//这里三处给m,n,lev赋值,然后调用repaint()方法啊!
m,n,lev是这个类的私有成员函数
level=1; m=12; n=7; lev=21;
firstTime=false;
repaint();
}
怎么这
谁能帮帮我看一下啊我把程序传给他
谢谢!!!!!!!!!!!
但是我有另外一个一样结构的小画板程序,就运行正常!!会自动转入 action方法!
import java.awt.*;public class MyDrawPad extends Frame
public boolean action (Event e,Object o)
{
if (e.target instanceof MenuItem)
{
if (e.arg.equals("White background"))
{
System.out.println("!!!");
setBackground(Color.white);
}
else if (e.arg.equals("Black background"))
setBackground(Color.black);
else if (e.arg.equals("Yellow background"))
setBackground(Color.yellow);
else if (e.arg.equals("Black"))
color=1;
else if (e.arg.equals("Red"))
color=2;
else if (e.arg.equals("Blue"))
color=3;
else if (e.arg.equals("Green"))
color=4;
else if (e.arg.equals("Circle-1"))
shape=1;
else if (e.arg.equals("Circle-2"))
shape=2;
else if (e.arg.equals("Circle-3"))
shape=3;
else if (e.arg.equals("Square"))
shape=4;
}
return true;
}
我把那些 if写在handleEvent里就对了
!!!
怎么会action里不行的啊!!!