我有一個(gè)JTextPane,我添加了文本,而一些文本有一個(gè)通過(guò)StyleConstants.setIcon()設(shè)置的圖像.我還向JTextPane添加了一個(gè)鼠標(biāo)監(jiān)聽(tīng)器,以檢測(cè)鼠標(biāo)在圖像上單擊/懸停的時(shí)間,但它只在圖像的左側(cè)部分檢測(cè)到它.難道我做錯(cuò)了什么?
SSCCE(將鼠標(biāo)懸停在圖像上會(huì)更改鼠標(biāo)光標(biāo)以指示何時(shí)檢測(cè)到圖像):
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
/**
* SSCCE to show how detecting an image under the current mouse position only
* works on part of the image. It adds a simple image to the document of the
* JTextPane and changes the mouse cursor when it detects the mouse hovering
* over the image.
*/
public class JTextPaneImage {
private static void createWindow() {
// Create window
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create JTextPane and add to window
final JTextPane textPane = new JTextPane();
textPane.setEditable(false);
textPane.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
AttributeSet style = getAttributes(e);
if (style != null && StyleConstants.getIcon(style) != null) {
textPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
} else {
textPane.setCursor(Cursor.getDefaultCursor());
}
}
});
frame.add(new JScrollPane(textPane));
try {
StyledDocument doc = (StyledDocument)textPane.getDocument();
// Add some text
doc.insertString(0, "Some text ", null);
// Add the image
SimpleAttributeSet style = new SimpleAttributeSet();
StyleConstants.setIcon(style, createImage());
doc.insertString(doc.getLength(), "test", style);
} catch (BadLocationException ex) {
Logger.getLogger(JTextPaneImage.class.getName()).log(Level.SEVERE, null, ex);
}
// Display everything
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
/**
* Retrieves the style of where the mouse is positioned (assuming this is
* a JTextPane).
*
* @param e The mouse event containing the mouse position
* @return The AttributeSet or null if none could be found
*/
private static AttributeSet getAttributes(MouseEvent e) {
JTextPane text = (JTextPane)e.getSource();
Point mouseLocation = new Point(e.getX(), e.getY());
int pos = text.viewToModel(mouseLocation);
if (pos >= 0) {
StyledDocument doc = text.getStyledDocument();
Element element = doc.getCharacterElement(pos);
return element.getAttributes();
}
return null;
}
/**
* Creates a single 28x28 image filled with a single color.
*
* @return The created ImageIcon
*/
public static ImageIcon createImage() {
BufferedImage image = new BufferedImage(28,28, BufferedImage.TYPE_INT_ARGB);
Graphics g = image.getGraphics();
g.setColor(Color.GREEN);
g.fillRect(0, 0, 28, 28);
g.dispose();
return new ImageIcon(image);
}
public static final void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createWindow();
}
});
}
}
解決方法: 您使用強(qiáng)text.viewToModel(mouseLocation)來(lái)檢測(cè)偏移量,然后從獲得的偏移量中檢索樣式.
邏輯是返回更接近鼠標(biāo)位置的偏移量.因此,當(dāng)您單擊視圖的右半部分時(shí),將返回下一個(gè)偏移(視圖后的偏移).您可以嘗試相同的設(shè)置一個(gè)大字母(例如m大字體).當(dāng)你clikc接近字母結(jié)束時(shí),插入符號(hào)設(shè)置在字母后面.這里的邏輯是一樣的.
所以你得到了圖像之后的位置并從位置獲取樣式但是在圖像視圖文本元素之后沒(méi)有屬性中的圖標(biāo)而你沒(méi)有圖像.
更新:
為了提供正確的行為,我建議使用modelToView()并傳遞獲得的偏移量.從矩形中你可以弄清楚你的X位置是否已經(jīng)過(guò)時(shí)了.矩形的X.如果插入矩形的x大于鼠標(biāo)X,則可以嘗試前一個(gè)偏移.
UPDATE2:您可以覆蓋IconView并使用paint()方法存儲(chǔ)圖像視圖的最后一個(gè)繪制矩形.存儲(chǔ)在最后繪制的地圖中.在鼠標(biāo)移動(dòng)/單擊時(shí),檢查地圖以查找其中一個(gè)矩形是否包含該點(diǎn).
要么
你可以使用View的方法getChildAllocation()類(lèi)似的東西被描述為here來(lái)計(jì)算圖像邊界.從根視圖開(kāi)始,然后向下直到葉子計(jì)算X,Y的正確視圖.如果葉子視圖是IconView,則你的圖像已經(jīng)過(guò)了. 來(lái)源:https://www./content-1-426601.html
|