5

JTextPane の JLabel または JButton に mouselistener を追加しようとすると、「呼び出し変換によって Mouselistener に変換できません」というエラーが表示されるという問題があります。コンポーネントを JEditorPane に配置したいと思います。HyperlinkEvent を使用できるとも聞きました。

基本的に、JEditorPane(優先)/JTextPane で右/左クリックできるコンポーネントが必要です。どんな助けでもいただければ幸いです

今では(一種の)動作し、右クリックのみを受信し、ボタンの端を描画する必要はありません。ボタンのテキストに下線を引くことはできますか?

サンプルコードは次のとおりです...

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

import javax.swing.JButton;

    public class InlineB extends JButton    {

        public InlineB( String caption )    {

            super( caption );
        }
    }
4

2 に答える 2

5

質問がどこにあるのかわかりません。

ただしJButton、ボタンのテキストをHTMLで設定するだけのテキストに下線を付けすぎているように見えますタグ:

//added <u></u> to underlone button
InlineB label = new InlineB("<html><u>JLabel</u></html>");

MouseEvent.BUTTON1左クリックについては、またはのifステートメントにチェックを追加しますSwingUtilities.isLeftMouseButton(MouseEvent me)

//added check for MouseEvent.BUTTON1 which is left click
if (e.isPopupTrigger() || e.getButton() == MouseEvent.BUTTON1) {
}

クラスまたはインスタンスのいずれかでJButton単純に呼び出しの境界線を描画しないようにするには(クラス内で行いました):setBorder(null);InlineBInlineB

   public InlineB(String caption) {
    super(caption);
    setBorder(null);//set border to nothing
}

また、のコンテンツタイプを設定していないことがわかります。これは次のJTextPaneようにする必要があります。

    //set content as html
    editorPane.setContentType("text/html");

アプレットは使用していませんが、移植は非常に簡単ですが、小さな例を示しました。

ここに画像の説明を入力してください

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
    }
}
于 2012-09-29T12:22:57.333 に答える
2

JTextPane の JLabel または JButton に mouselistener を追加しようとすると、「呼び出し変換によって Mouselistener に変換できません」というエラーが表示されるという問題があります。

addMouseListener() に渡すオブジェクトは、MouseListener インターフェイスを実装します。右?(コードサンプルを見たところです。マウスアダプタは正しいようです)。
あなたは今、 Now it works (sortof)と言います。そのエラーを修正したということですか?

ところで、それが解決され、その後の問題があり、コミュニティが再利用できる場合は、別の質問を開くことをお勧めします: https://meta.stackexchange.com/questions/48345/what-is-the-etiquette質問内容の変更について

コンポーネントを JEditorPane に配置したいと思います。

あなたが聞いているコンポーネントを意味していると思います。とにかく、JEditorPaneが他のコンポーネントのコンテナーとして使用されることを意図しているかどうかはわかりません。

HyperlinkEvent を使用できるとも聞きました。

HyperLinkEventは、ENTERED、EXITED、および ACTIVATED のイベント タイプを対象としています。ハイパーリンク イベントまたはマウス イベントを処理するつもりですか?

基本的に、JEditorPane(優先)/JTextPane で右/左クリックできるコンポーネントが必要です。どんな助けでもいただければ幸いです

次回は、質問の範囲/コンテキストを最初に与えることをお勧めします。クリックできるテキスト ペインの上に何か (もっと具体的に言えますか?) が欲しいという意味だと思います。とにかく、あなたが JEditorPane をそのように使用するつもりであることに驚きました。

于 2012-09-29T09:32:28.393 に答える