简体   繁体   English

MouseListener-在JTextArea上不起作用

[英]MouseListener - not working on JTextArea

I have A Gui-class that extends JFrame. 我有一个扩展JFrame的Gui类。 At the top there is a JMenuBar and the rest consists of a big JTextField. 顶部有一个JMenuBar,其余的则由一个大的JTextField组成。

I have implemented a mouseListener to this class and the problem is that it only seem to listen when clicking on the JMenuBar, not the JTextArea. 我已经为此类实现了mouseListener,问题是它似乎只在单击JMenuBar时才侦听,而不是JTextArea。 So my question is how I make the mouseListener react to mouse click on the JTextArea 所以我的问题是我如何使mouseListener对JTextArea上的鼠标单击做出反应

heres a snappshot of the Gui-class (constructor) 这是Gui类(构造函数)的快照

 public class Gui extends JFrame implements ActionListener, MouseListener {

private JMenu fileMenu;
private JTextArea textArea;
private JFileChooser chooser;

public static void main(String[] args) {

    new Gui().setVisible(true);

}

public Gui() {

    setSize(600, 600);
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);

    createFileMenu();

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);
    menuBar.add(fileMenu);

    textArea = new JTextArea();

    JScrollPane scroll = new JScrollPane (textArea, 
    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    Container contentPane = getContentPane();
    contentPane.add(scroll);

    chooser = new JFileChooser();

     addMouseListener(this);

    setDefaultCloseOperation(EXIT_ON_CLOSE);

}

截图

Add Mouse Listener to the textarea instead of the window. 将鼠标侦听器添加到文本区域而不是窗口。

 textArea = new JTextArea();
 textArea.addMouseListener(this);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM