更改/显示带有文本的光标 [英] Change/Display Cursor with Text
问题描述
我当前正在尝试将某些 button
的 actionPerformed()
方法正在执行.到目前为止,我发现的唯一解决方案是使用包含所需文本的 Image
更改 Cursor
.代码如下:
I am currently trying to change the cursor shown on my JFrame
to some text like "wait please your job is being done" while a certain button
's actionPerformed()
method is executing. The only solution I have found as of yet is to change the Cursor
with an Image
which contains my desired Text. The code is below:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Hasikome extends JFrame{
static Cursor cursor;
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
final Hasikome hasi = new Hasikome();
hasi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
hasi.setSize(new Dimension(1200, 1200));
hasi.setPreferredSize(new Dimension(500, 500));
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension dim = kit.getBestCursorSize(16, 16);
BufferedImage buffered = null;
try {
buffered = ImageIO.read(new File(Paths.get("").toAbsolutePath().toString() + "\\belge.png"));
} catch (IOException e) {
e.printStackTrace();
}
java.awt.Shape circle = new Ellipse2D.Float(0, 0, dim.width - 1, dim.height - 1);
Graphics2D g = buffered.createGraphics();
g.setColor(Color.BLUE);
g.draw(circle);
g.setColor(Color.RED);
hasi.add(new JButton(new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
hasi.setCursor(cursor);
try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
e1.printStackTrace();
} finally {
hasi.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}
}), BorderLayout.NORTH);
int centerX = (dim.width - 1) /2;
int centerY = (dim.height - 1) / 2;
g.drawLine(centerX, 0, centerX, dim.height - 1);
g.drawLine(0, centerY, dim.height - 1, centerY);
g.dispose();
cursor = kit.createCustomCursor(buffered, new Point(centerX, centerY), "myCursor");
hasi.pack();
hasi.setVisible(true);
}
}
此代码的问题是 Dimension dim = kit.getBestCursorSize(16,16);
此行在 Windows上始终生成大小为32x32的
Cursor
代码>,并且它取决于平台.我不能使用超过32x32的值,否则行 cursor = kit.createCustomCursor(buffered,new Point(centerX,centerY),"myCursor");
会出现异常.而且由于我使用的是相当长的文本(250x16),因此我无法正确显示 Cursor
的 text image
.
The problem with this code is Dimension dim = kit.getBestCursorSize(16, 16);
this line always generates Cursor
with size 32x32 on Windows
and it is platform dependant. I can't use values more than 32x32 or else I get exception for the line cursor = kit.createCustomCursor(buffered, new Point(centerX, centerY), "myCursor");
. And because I use a reasonably long text (250x16) it won't allow me to show the text image
as Cursor
properly.
此解决方案不是必需的,我要实现的是将文本显示为 Cursor
,而某些 Button
的 actionPerformed()
方法正在执行.有什么办法可以做到吗?预先感谢.
This solution not necessary, what I want to accomplish is to show text to users as Cursor
while some Button
's actionPerformed()
method is being executed. Is there any way I can do this? Thanks in advance.
推荐答案
查看如果允许您在使用等待"光标时显示带有半透明背景上绘制的文字的 Glass Pane
.
If allows you to display the Glass Pane
with text painted on a semi transparent background while using the "wait" cursor.
这篇关于更改/显示带有文本的光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!