比如,有一个主按钮和若干个二级按钮,主按钮的作用是选择任务,比如,打印和保存,二级按钮是对不同文件进行打印或者保存。等于说是需要两次按钮交互才可以完成一个任务,两次按钮之间的交互怎么定义?请指教!谢谢!
那你为什么不搞1个按纽就能完成任务呢
这只是我的假设,具体的工作就必须两个。
现在我这里已经可以实现交互了,只是事件监听这里还不对。
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.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.gvt.GVTTreeRendererAdapter;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.apache.batik.swing.svg.SVGDocumentLoaderAdapter;
import org.apache.batik.swing.svg.SVGDocumentLoaderEvent;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;
import org.w3c.dom.svg.SVGDocument;
public class StatusBar4 implements ActionListener{
Frame fr;
Button bt1,bt2,bt3,bt4;
Panel pn;
Label label;
JSVGCanvas canvas;
SVGDocument svgDoc;
EventTarget root;
String attr,tool;
File f=new File("sxmap.svg");
private static final long serialVersionUID = 884142590900760515L;
public StatusBar4() {
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 JSVGCanvas();
canvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
canvas.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {
public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
label.setText("正在加载图形文件...");
}
public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
label.setText("文件加载完毕");
}
});
canvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
label.setText("正在解析图形文件...");
}
public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
label.setText("完毕");
svgDoc= canvas.getSVGDocument();
root = (EventTarget) svgDoc.getRootElement();
registerListeners();
}
});
fr.addWindowListener(new WindowAdapter() {
public void windowOpened(WindowEvent e) {
// The canvas is ready to load the base document now, from the AWT thread.
canvas.setURI(f.toURI().toString());
}
});
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 registerListeners() {
NodeList chs=svgDoc.getElementById("sx").getChildNodes();
for(int i=0; i<chs.getLength(); i++)
{
EventTarget t = (EventTarget) chs.item(i);
t.addEventListener("mouseover", new listener1(), false);
t.addEventListener("mouseout", new listener2(), false);
}
}
public class listener1 implements EventListener {
public void handleEvent(Event evt) {
Element e = (Element) evt.getTarget();
attr=e.getAttribute("fill");
e.setAttribute("fill", "red");
}
}
public class listener2 implements EventListener {
public void handleEvent(Event evt) {
Element e = (Element) evt.getTarget();
e.setAttribute("fill", attr);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==bt4)
{
//tool="pan";
root.addEventListener("mousedrag", new drag(), false); //这里的"nousedrag"好像不对,拖动鼠标事件应该怎么写?
}
else
{
if(e.getSource()==bt1)
{
//tool="reset";
root.addEventListener("click", new reset(), false);//如果点击两次,就会执行两次,我再后面都已经remove了还是不对,怎么写呢?
}
if(e.getSource()==bt2)
{
//tool="big";
root.addEventListener("click", new big(), false);//鼠标点击按钮也可以放大图,后面还要实现以鼠标点击点为中心放大,所以写了这个监听事件,而没有写在button上。
}
if(e.getSource()==bt3)
{
//tool="small";
root.addEventListener("click", new small(), false);
}
}
}
public class drag implements EventListener {
public void handleEvent(Event evt) {
System.out.println("drag");
//root.removeEventListener("click", new drag(), false);
}
}
public class reset implements EventListener {
public void handleEvent(Event evt) {
float sa=svgDoc.getRootElement().getCurrentScale();
svgDoc.getRootElement().setCurrentScale(1);
System.out.println(sa);
root.removeEventListener("click", new reset(), false);
}
}
public class big implements EventListener {
public void handleEvent(Event evt) {
float sa=svgDoc.getRootElement().getCurrentScale();
svgDoc.getRootElement().setCurrentScale(sa*2);
System.out.println(sa);
root.removeEventListener("click", new big(), false);
}
}
public class small implements EventListener {
public void handleEvent(Event evt) {
float sa=svgDoc.getRootElement().getCurrentScale();
svgDoc.getRootElement().setCurrentScale((float) (sa*0.5));
System.out.println(sa);
root.removeEventListener("click", new small(), false);
}
}
public static void main(String[] args) {
new StatusBar4();
}
}