swing hide cursor

It appears that the Cursor class does not have a "blank" cursor to begin with, so one could define a new "blank" cursor using the Toolkit.createCustomCursor method.

Here's one way I've tried which seems to work:

 

// Transparent 16 x 16 pixel cursor image.
BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);

// Create a new blank cursor.
Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(
    cursorImg, new Point(0, 0), "blank cursor");

// Set the blank cursor to the JFrame.
mainJFrame.getContentPane().setCursor(blankCursor);

 

Edit

Regarding the comment about everything inside the JFrame ending up without a cursor, it seems that the Components which are contained in the JFrame will end up inheriting the cursor of the container (the JFrame), so if it is a requirement to have a certain Component have the cursor appear, one would have to manually set the desired cursor.

For example, if there is a JPanel contained in the JFrame, then one could set the cursor of that JPanel to the system's default using the Cursor.getDefaultCursor method:

JPanel p = ...
// Sets the JPanel's cursor to the system default.
p.setCursor(Cursor.getDefaultCursor());

 

或者下面的程序

import java.awt.Cursor;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.MemoryImageSource;

public class Main {
  public static void main(String[] argvthrows Exception {

    int[] pixels = new int[16 16];
    Image image = Toolkit.getDefaultToolkit().createImage(
        new MemoryImageSource(1616, pixels, 016));
    Cursor transparentCursor = Toolkit.getDefaultToolkit().createCustomCursor(
        image, new Point(00)"invisibleCursor");
  }
}

 

===========================================

 

 

private Chooser(java.util.Date date, String format, int startDAY){ if(startDAY > -1 && startDAY < 7) defaultStartDAY = startDAY; int dayIndex = defaultStartDAY; for(int i=0; i<7; i++){ if(dayIndex > 6) dayIndex = 0; weekLabels[i] = new WeekLabel(dayIndex, showTEXT[dayIndex]); dayIndex ++ ; } sdf = new java.text.SimpleDateFormat(format); calendar = Calendar.getInstance(); calendar.setTime(date); initCalendarPanel(); } public static Chooser getInstance(java.util.Date date, String format){ return new Chooser(date, format, defaultStartDAY); } public static Chooser getInstance(java.util.Date date){ return getInstance(date, DEFAULTFORMAT); } public static Chooser getInstance(String format){ return getInstance(new java.util.Date(), format); } public static Chooser getInstance(){ return getInstance(new java.util.Date(), DEFAULTFORMAT); } private void initCalendarPanel(){ calendarPanel = new JPanel(new java.awt.BorderLayout()); calendarPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0xAA, 0xAA, 0xAA))); calendarPanel.add(titlePanel = new TitlePanel(), java.awt.BorderLayout.NORTH); calendarPanel.add(bodyPanel = new BodyPanel(), java.awt.BorderLayout.CENTER); calendarPanel.add(footerPanel = new FooterPanel(),java.awt.BorderLayout.SOUTH); this.addAncestorListener(new AncestorListener() { public void ancestorAdded(AncestorEvent event) { } public void ancestorRemoved(AncestorEvent event) {hidePanel();} //hide pop when move component. public void ancestorMoved(AncestorEvent event) { hidePanel(); } }); } public void register(final JComponent showComponent) { this.showDate = showComponent; showComponent.setRequestFocusEnabled(true); showComponent.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent me) { showComponent.requestFocusInWindow(); } }); this.add(showComponent, BorderLayout.CENTER); this.setPreferredSize(new Dimension(90, 25)); this.setBorder(BorderFactory.createLineBorder(Color.GRAY)); showComponent.addMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent me) { if (showComponent.isEnabled()) { showComponent.setCursor(new Cursor(Cursor.HAND_CURSOR)); } } public void mouseExited(MouseEvent me) { if (showComponent.isEnabled()) { showComponent.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); showComponent.setForeground(Color.BLACK); } } public void mousePressed(MouseEvent me) { if (showComponent.isEnabled()) { showComponent.setForeground(hoverColor); if (isShow) { hidePanel(); } else { showPanel(showComponent); } } } public void mouseReleased(MouseEvent me) { if (showComponent.isEnabled()) { showComponent.setForeground(Color.BLACK); } } }); showComponent.addFocusListener(new FocusListener() { public void focusLost(FocusEvent e) { hidePanel(); } public void focusGained(FocusEvent e) { } }); } //hide the main panel. private void hidePanel() { if (pop != null) { isShow = false; pop.hide(); pop = null; } }逐行解释
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值