aa改好了,谢谢!
现在还要回到我原来的程序了,希望适用!
非常感谢千里冰封!
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Button;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class aa implements ActionListener,MouseListener{
Frame fr;
Button bt1,bt2,bt3,bt4;
Panel pn;
Label label;
Canvas canvas;
String attr,tool;
private static final long serialVersionUID = 884142590900760515L;
public aa() {
fr=new Frame("陕西地图");
FlowLayout layout=new FlowLayout(FlowLayout.LEFT);//组件左对齐;
bt1=new Button("复位");
bt1.setCursor(new Cursor(Cursor.HAND_CURSOR));
bt1.addActionListener(this);
bt2=new Button("放大");
bt2.setCursor(new Cursor(Cursor.HAND_CURSOR));
bt2.addActionListener(this);
bt3=new Button("缩小");
bt3.setCursor(new Cursor(Cursor.HAND_CURSOR));
bt3.addActionListener(this);
bt4=new Button("漫游");
bt4.setCursor(new Cursor(Cursor.HAND_CURSOR));
bt4.addActionListener(this);
pn=new Panel();
pn.setLayout(layout);//设置布局管理器;
pn.add(bt1);pn.add(bt2);pn.add(bt3);pn.add(bt4);
pn.setBackground(Color.white.darker());
label=new Label();
label.setBackground(Color.white.darker());
canvas=new Canvas();
fr.add(pn,"North");
fr.add(canvas,"Center");
fr.add(label,"South");
fr.setBounds(50,50,800,680);
fr.setVisible(true);
fr.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==bt4)
{
tool="pan";
canvas.addMouseListener(this);
}
if(e.getSource()==bt1)
{
tool="reset";
canvas.addMouseListener(this);
}
if(e.getSource()==bt2)
{
tool="big";
canvas.addMouseListener(this);
}
if(e.getSource()==bt3)
{
tool="small";
canvas.addMouseListener(this);
}
}
public void mouseClicked(MouseEvent event) {
if(tool=="reset")
{
label.setText(tool);
System.out.println(tool);
canvas.removeMouseListener(this);
}
if(tool=="big")
{
label.setText(tool);
System.out.println(tool);
canvas.removeMouseListener(this);
}
if(tool=="small")
{
label.setText(tool);
System.out.println(tool);
canvas.removeMouseListener(this);
}
if(tool=="pan")
{
label.setText(tool);
System.out.println(tool);
canvas.removeMouseListener(this);
}
}
public static void main(String[] args) {
new aa();
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
又有新问题了,如果点击同一个button多次,再点击canvas,就会有多次输出,怎么判断点击的是同一个button?