我试图学习在Eclipse中开发插件,并在遵循Eclipse提供的教程之后,尝试通过编写一个简短的Java类来跟踪和显示文本区域上的鼠标移动来扩展代码。我从Oracle Java文档中获得了相应的代码。以下是我称之为鼠标跟踪类(MouseEventDemo)的java类:
package com.example.helloworld.handlers;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.jface.dialogs.MessageDialog;
/**
* Our sample handler extends AbstractHandler, an IHandler base class.
* @see org.eclipse.core.commands.IHandler
* @see org.eclipse.core.commands.AbstractHandler
*/
public class SampleHandler extends AbstractHandler {
/**
* The constructor.
*/
public SampleHandler() {
}
/**
* the command has been executed, so extract the needed information
* from the application context.
*/
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
MouseEventDemo md = new MouseEventDemo();
md.callStart();
return null;
}
}
接下来,在MouseEventDemo类中, callStart()通过调用 createAndShowGUI()启动GUI绘制,然后调用 MouseEventDemo 类构造函数并完成工作。
package com.example.helloworld.handlers;
import javax.swing.*;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class MouseEventDemo extends JPanel implements MouseListener {
BlankArea blankArea;
JTextArea textArea;
static final String NEWLINE = System.getProperty("line.separator");
String wString;
public void callStart(){
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
/* Turn off metal's use of bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
//creating threaded implementation
/*javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});*/
}
public MouseEventDemo(String xx){
super(new GridLayout(0,1));
wString = xx;
JOptionPane.showMessageDialog (null, "assigned");
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("MouseDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new MouseEventDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public MouseEventDemo(){
JOptionPane.showMessageDialog (null, wString);
blankArea = new BlankArea(Color.YELLOW);
add(blankArea);
textArea = new JTextArea();
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(200, 75));
add(scrollPane);
//Register for mouse events on blankArea and the panel.
blankArea.addMouseListener(this);
addMouseListener(this);
setPreferredSize(new Dimension(450, 450));
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}
void eventOutput(String eventDescription, MouseEvent e) {
textArea.append(eventDescription + " detected on "
+ e.getComponent().getClass().getName()
+ "." + NEWLINE);
textArea.setCaretPosition(textArea.getDocument().getLength());
}
public void mousePressed(MouseEvent e) {
eventOutput("Mouse pressed (# of clicks: "
+ e.getClickCount() + ")", e);
}
public void mouseReleased(MouseEvent e) {
eventOutput("Mouse released (# of clicks: "
+ e.getClickCount() + ")", e);
}
public void mouseEntered(MouseEvent e) {
eventOutput("Mouse entered", e);
}
public void mouseExited(MouseEvent e) {
eventOutput("Mouse exited", e);
}
public void mouseClicked(MouseEvent e) {
eventOutput("Mouse clicked (# of clicks: "
+ e.getClickCount() + ")", e);
}
}
在所有这些中,有两个MouseEventDemo对象实例被创建。首先从 SampleHandler 调用 callStart(),然后再在createAndShowGUI方法中的MouseEventDemo类中调用。第一个在初始调用之后根本没有使用,似乎完全浪费和资源的低效使用(只是在MouseEventDemo中调用一个公共方法)。
有没有更好的方法来完成预定的任务?