这个问题与我之前的问题有关:How To create A Large Size Custom Cursor In Java?
如果您对它的用途感到好奇,您可以在以下网址找到一个名为 GATE [图形访问表格条目] 的获奖密码保护系统的实际用例:http: //gatecybertech.net/
在上一个问题之后,我找到了一种创建大型自定义光标的方法,并且工作答案发布在我之前的帖子中。但是为了实现它,我必须先单击一个复选框,现在我希望能够创建一个大的自定义光标而无需先单击一个复选框,所以我修改了我的代码,如下所示,没有复选框:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.event.MouseInputAdapter;
public class Demo_Large_Custom_Cursor_Simple
{
static private MyGlassPane_Simple myGlassPane;
private static void createAndShowGUI()
{
JFrame frame=new JFrame("Demo_Large_Custom_Cursor_Simple");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// JCheckBox changeButton=new JCheckBox("Custom Cursor \"visible\"");
// changeButton.setSelected(false);
Container contentPane=frame.getContentPane();
contentPane.setLayout(new FlowLayout());
// contentPane.add(changeButton);
JButton Button_1=new JButton("<Html><Table Cellpadding=7><Tr><Td>A</Td><Td>B</Td></Tr><Tr><Td>C</Td><Td>D</Td></Tr></Table></Html>");
Button_1.setPreferredSize(new Dimension(80,80));
Button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { Out("Button 1"); } });
contentPane.add(Button_1);
JButton Button_2=new JButton("<Html><Table Cellpadding=7><Tr><Td>1</Td><Td>2</Td></Tr><Tr><Td>3</Td><Td>4</Td></Tr></Table></Html>");
Button_2.setPreferredSize(new Dimension(80,80));
Button_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { Out("Button 2"); } });
contentPane.add(Button_2);
JMenuBar menuBar=new JMenuBar();
JMenu menu=new JMenu("Menu");
menu.add(new JMenuItem("Do nothing"));
menuBar.add(menu);
frame.setJMenuBar(menuBar);
// Set up the glass pane, which appears over both menu bar and content pane and is an item listener on the change button.
// myGlassPane=new MyGlassPane_Simple(changeButton,menuBar,frame.getContentPane());
myGlassPane=new MyGlassPane_Simple(menuBar,frame.getContentPane());
// changeButton.addItemListener(myGlassPane);
frame.setGlassPane(myGlassPane);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
private static void out(String message) { System.out.print(message); }
private static void Out(String message) { System.out.println(message); }
public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); }
}
// We have to provide our own glass pane so that it can paint.
class MyGlassPane_Simple extends JComponent implements ItemListener
{
Point point;
// public MyGlassPane_Simple(AbstractButton aButton,JMenuBar menuBar,Container contentPane)
public MyGlassPane_Simple(JMenuBar menuBar,Container contentPane)
{
// CBListener_Simple listener=new CBListener_Simple(aButton,menuBar,this,contentPane);
CBListener_Simple listener=new CBListener_Simple(menuBar,this,contentPane);
addMouseListener(listener);
addMouseMotionListener(listener);
}
// React to change button clicks.
public void itemStateChanged(ItemEvent e) { setVisible(e.getStateChange()==ItemEvent.SELECTED); }
protected void paintComponent(Graphics g)
{
try
{
if (point!=null)
{
// g.setColor(Color.red);
// g.fillOval(point.x-10,point.y-10,20,20);
BufferedImage image=ImageIO.read(new File("C:/Cursor_Crosshair.PNG"));
g.drawImage(image,point.x-39,point.y-39,null);
}
}
catch (Exception e) { }
}
public void setPoint(Point p) { point=p; }
}
// Listen for all events that our check box is likely to be interested in. Redispatch them to the check box.
class CBListener_Simple extends MouseInputAdapter
{
Toolkit toolkit;
Component liveButton;
JMenuBar menuBar;
MyGlassPane_Simple glassPane;
Container contentPane;
// public CBListener_Simple(Component liveButton,JMenuBar menuBar,MyGlassPane_Simple glassPane,Container contentPane)
public CBListener_Simple(JMenuBar menuBar,MyGlassPane_Simple glassPane,Container contentPane)
{
toolkit=Toolkit.getDefaultToolkit();
this.liveButton=liveButton;
this.menuBar=menuBar;
this.glassPane=glassPane;
this.contentPane=contentPane;
}
public void mouseMoved(MouseEvent e)
{
// redispatchMouseEvent(e,false);
redispatchMouseEvent(e,true);
}
public void mouseDragged(MouseEvent e) { redispatchMouseEvent(e,false); }
public void mouseClicked(MouseEvent e) { redispatchMouseEvent(e,false); }
public void mouseEntered(MouseEvent e) { redispatchMouseEvent(e,false); }
public void mouseExited(MouseEvent e) { redispatchMouseEvent(e,false); }
public void mousePressed(MouseEvent e) { redispatchMouseEvent(e,false); }
public void mouseReleased(MouseEvent e) { redispatchMouseEvent(e,true); }
// A basic implementation of redispatching events.
private void redispatchMouseEvent(MouseEvent e,boolean repaint)
{
Point glassPanePoint=e.getPoint();
Container container=contentPane;
Point containerPoint=SwingUtilities.convertPoint(glassPane,glassPanePoint,contentPane);
if (containerPoint.y<0)
{ // We're not in the content pane
if (containerPoint.y+menuBar.getHeight()>=0)
{
// The mouse event is over the menu bar. Could handle specially.
}
else
{
// The mouse event is over non-system window decorations, such as the ones provided by the Java look and feel. Could handle specially.
}
}
else
{
// The mouse event is probably over the content pane. Find out exactly which component it's over.
Component component=SwingUtilities.getDeepestComponentAt(container,containerPoint.x,containerPoint.y);
// if ((component!=null) && (component.equals(liveButton)))
if ((component!=null))
{
// Forward events over the check box.
Point componentPoint=SwingUtilities.convertPoint(glassPane,glassPanePoint,component);
component.dispatchEvent(new MouseEvent(component,e.getID(),e.getWhen(),e.getModifiers(),componentPoint.x,componentPoint.y,e.getClickCount(),e.isPopupTrigger()));
}
}
// Update the glass pane if requested.
if (repaint)
{
glassPane.setPoint(glassPanePoint);
glassPane.repaint();
}
}
}
这个应用程序中的 3 个类是:
Demo_Large_Custom_Cursor_Simple.java
MyGlassPane_Simple.java
CBListener_Simple.java
它们是运行此演示应用程序所需的所有类。
但是,它不再显示大的自定义光标,我想知道我做错了什么,基于这个现有的代码我应该怎么做才能在鼠标进入应用程序窗口时显示大的自定义光标?