问题描述
我遇到一个问题,当我尝试在JTextPane中的JLabel或JButton中添加一个mouselistener时,出现错误无法通过调用转换转换为Mouselistener".我希望将组件包含在JEditorPane中.我还听说可以使用HyperlinkEvent.
I have a problem where when I try and add a mouselistener to a JLabel or JButton in a JTextPane I get the error "cannot be converted to Mouselistener by invocation conversion". I would prefer to have the component in a JEditorPane. I also heard a HyperlinkEvent could be used.
基本上,我想要一个可以在JEditorPane(推荐)/JTextPane中右/左单击的组件.任何帮助将不胜感激
Basicly I want a component that can be right/left clicked in a JEditorPane(preffered)/JTextPane. Any help would be appreciated
现在它可以工作了(排序),它只接收右键单击,而我不需要绘制按钮边缘.我可以在按钮的文字下划线吗?
Now it works (sortof) it only recives right clicks and I need to not draw button edges. Can I underline the button's text?
示例代码如下...
import java.awt.*;
import javax.swing.*;
import java.awt.Color;
import javax.swing.JTextPane;
import javax.swing.JButton;
import java.applet.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class jlabeltest extends Applet {
public void init() {
jlabeltest editorPaneExample = new jlabeltest();
editorPaneExample.setSize(550, 300);
// editorPaneExample.setText("tutorialData.com");
editorPaneExample.setVisible(true);
}
public jlabeltest() {
JTextPane editorPane = new JTextPane();
editorPane.setSelectedTextColor(Color.red);
editorPane.setText("<p color='#FF0000'>Cool!</p>");
InlineB label = new InlineB("JLabel");
label.setAlignmentY(0.85f);
label.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e)
{
if (e.isPopupTrigger())
{
JOptionPane.showMessageDialog(null,"Hello!");
// do your work here
}
}
});
editorPane.insertComponent(label);
this.add(editorPane);
}
}
InlineB.java
InlineB.java
import javax.swing.JButton;
public class InlineB extends JButton {
public InlineB( String caption ) {
super( caption );
}
}
推荐答案
我不确定您想要的问题到处都是.
I'm not sure what you want the question is everywhere.
但是看起来JButton
的下划线也很简单,只需使用HTML 标记设置按钮的文本即可:
But look too underline text of a JButton
simply set the text of the button with HTML tags:
//added <u></u> to underlone button
InlineB label = new InlineB("<html><u>JLabel</u></html>");
对于左键,在MouseEvent.BUTTON1
或SwingUtilities.isLeftMouseButton(MouseEvent me)
的if语句中添加一个检查:
as for the left click add a check to your if statement for the MouseEvent.BUTTON1
or SwingUtilities.isLeftMouseButton(MouseEvent me)
:
//added check for MouseEvent.BUTTON1 which is left click
if (e.isPopupTrigger() || e.getButton() == MouseEvent.BUTTON1) {
}
要不绘制JButton
的边框,只需在InlineB
类或InlineB
实例中调用setBorder(null);
(我在该类中做到了):
To not draw the borders of the JButton
simply call setBorder(null);
either in the InlineB
class or on the InlineB
instance (I did it within the class):
public InlineB(String caption) {
super(caption);
setBorder(null);//set border to nothing
}
我还看到您没有设置JTextPane
的内容类型,您应该:
also I see that you dont set the content type of the JTextPane
, which you should:
//set content as html
editorPane.setContentType("text/html");
我做了一个小例子,虽然我没有使用Applet,但是很容易移植:
I did a small example though I did not use an Applet but its very easy to port:
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class Test {
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Test().createAndShowUI();
}
});
}
private void createAndShowUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComponents(frame);
frame.pack();
frame.setVisible(true);
}
private void initComponents(JFrame frame) {
JTextPane editorPane = new JTextPane();
editorPane.setSelectedTextColor(Color.red);
//set content as html
editorPane.setContentType("text/html");
editorPane.setText("<p color='#FF0000'>Cool!</p>");
//added <u></u> to underlone button
InlineB label = new InlineB("<html><u>JLabel</u></html>");
label.setAlignmentY(0.85f);
label.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
//added check for MouseEvent.BUTTON1 which is left click
if (e.isPopupTrigger() || e.getButton() == MouseEvent.BUTTON1) {
JOptionPane.showMessageDialog(null, "Hello!");
// do your work here
}
}
});
editorPane.insertComponent(label);
frame.getContentPane().add(editorPane);
}
}
class InlineB extends JButton {
public InlineB(String caption) {
super(caption);
setBorder(null);//set border to nothing
}
}
这篇关于将mouselistener添加到JTextPane中插入的JLabel/JButton中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!