在java.awt.Graphics上绘制一个java.awt.Cursor
作者:互联网
是否可以以位图图像(例如BufferedImage)或可在Graphics2D上绘制的任何形式获得标准的AWT光标?例如,文本光标为new Cursor(Cursor.TEXT_CURSOR).
我相信这些游标会根据本机平台而变化,我想将它们合并到某些Swing图标中.
解决方法:
即使使用自己的图标,也可以显示与平台相关的光标,以后将在给定上下文中使用.将鼠标悬停在按钮和相邻面板上以查看效果.使用该图标在组件上的工具提示总是很不错的选择.
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
/**
* @see https://stackoverflow.com/a/16363072/230513
*/
public class Test {
private static class CursorPanel extends JPanel {
private static final Icon ICON = UIManager.getIcon("html.pendingImage");
private Cursor cursor;
public CursorPanel(final Cursor cursor) {
this.cursor = cursor;
String name = cursor.getName();
JButton button = new JButton(name, ICON);
button.setCursor(cursor);
button.setToolTipText(name);
this.add(button);
final JPanel panel = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 125);
}
};
panel.setCursor(cursor);
panel.setBorder(new TitledBorder(name));
this.add(panel);
}
}
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridLayout(0, 1));
f.add(new CursorPanel(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)));
f.add(new CursorPanel(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Test().display();
}
});
}
}
标签:graphics2d,swing,cursor,awt,java 来源: https://codeday.me/bug/20191123/2066911.html