大家晚上好;
我有JSVGCanvas(来自batik框架的组件)我正在画布上显示带有链接的svg文档。当点击链接时,我想在我的秋千上打开一个新窗口(jframe)。因此我正在使用linkActivated事件。但是,我的问题是
private JPanel contentPane;
File f;
JSVGCanvas svgCanvas;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
trialgui frame = new trialgui();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public trialgui() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 452, 418);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
f = new File("./links.svg");
svgCanvas = new JSVGCanvas();
if(f.exists()==true){System.out.println("file is ok");}
try {
svgCanvas.setURI(f.toURL().toString());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
contentPane.add(svgCanvas, BorderLayout.CENTER);
svgCanvas.setMySize(new Dimension(100,100));
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
svgCanvas.setURI(f.toURL().toString());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
contentPane.add(btnNewButton, BorderLayout.NORTH);
svgCanvas.addLinkActivationListener(new LinkActivationListener() {
@Override
public void linkActivated(LinkActivationEvent arg0) {
System.out.println("here is working");
try {
JFrame gh = new JFrame();
gh.setVisible(true);
f = new File("./links.svg");
svgCanvas.setURI(f.toURL().toString());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("here");
}
});
}
当用户点击链接时,svgcanvas会在同一个canvas中显示链接。为了防止这种情况,我设置了初始svg svgCanvas.setURI(f.toURL()。toString());当链接激活时触发。但是,由于方法不起作用,引用的链接出现在同一个画布中。如果我在按钮事件中使用相同的方法,当我点击按钮时,它会将最初的svg带回我的svgcanvas。
有人能告诉我为什么这个事件与按钮事件不同,我怎么可能解决这个问题?
问候....