如何使MouseClicked事件在java中工作

发布于 2024-12-05 01:27:14 字数 1567 浏览 0 评论 0原文

我很确定这非常简单,而且我只缺少一两行,但尽管在互联网上搜索解决方案,我还是无法完成这项工作。我对java相当陌生,我的问题是在桌面应用程序中。

我有一个非常简单的桌面应用程序,其中包含一个文本区域、一个带有 1 个菜单和 3 个菜单项的菜单栏。我想在单击 JFrame 中的“统计”菜单项时编辑文本区域的文本。

这是我创建菜单栏、菜单和菜单项(以及它们的事件)的代码部分:

//menu
        mnuRevision.setText("Revision");

    mnuitmStats.setText("Statistique");
    mnuitmStats.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            mnumnuitmStatsMouseClicked(evt);
        }
    });
    mnuRevision.add(mnuitmStats);

    mnuitmOrthographe.setText("Grammaire et orthographe");
    mnuitmOrthographe.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            mnuitmOrthographeMouseClicked(evt);
        }
    });
    mnuRevision.add(mnuitmOrthographe);

    mnuitmAnalyse.setActionCommand("Analyse");
    mnuitmAnalyse.setText("Analyse");
    mnuRevision.add(mnuitmAnalyse);

    jMenuBar1.add(mnuRevision);

    setJMenuBar(jMenuBar1);

这是 Mousclicked 函数:

    private void mnumnuitmStatsMouseClicked(java.awt.event.MouseEvent evt){
        this.txtTexte.setText("asdf");
        this.repaint();

    }

我想要做的是当我单击 mnuitemStats 时,txtTexte将获取其中写入的文本“asdf”。不知何故,这是行不通的。看起来程序甚至没有进入该函数。我查看了一些教程,除了对象名称之外,它们几乎与我具有相同的代码,因为大多数教程使用 JButton 而不是 JMenuItem

如果需要的话,我可以提供我的整个代码,但我认为其余的都是无关紧要的,因为它不接触菜单栏或文本区域。我正在使用 Eclipse Java EE IDE。

I am pretty sure this is very easy and that I am only missing one line or two but I just cannot make this work despite searching for solutions over the internet. I am fairly new to java and my problem is in a desktop application.

I have a pretty simple desktop application with one text area, one menu bar with one menu and 3 menu item. I want to edit the text of the text area when I click on the Statistic menu item in a JFrame.

Here is the part of the code where I create the menu bar, menu and menu items (as well as their events):

//menu
        mnuRevision.setText("Revision");

    mnuitmStats.setText("Statistique");
    mnuitmStats.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            mnumnuitmStatsMouseClicked(evt);
        }
    });
    mnuRevision.add(mnuitmStats);

    mnuitmOrthographe.setText("Grammaire et orthographe");
    mnuitmOrthographe.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            mnuitmOrthographeMouseClicked(evt);
        }
    });
    mnuRevision.add(mnuitmOrthographe);

    mnuitmAnalyse.setActionCommand("Analyse");
    mnuitmAnalyse.setText("Analyse");
    mnuRevision.add(mnuitmAnalyse);

    jMenuBar1.add(mnuRevision);

    setJMenuBar(jMenuBar1);

Here is the Mousclicked function:

    private void mnumnuitmStatsMouseClicked(java.awt.event.MouseEvent evt){
        this.txtTexte.setText("asdf");
        this.repaint();

    }

What I want to do is when I click on mnuitemStats, the txtTexte will get the text "asdf" written in it. Somehow, this is not working. It looks like the program is not even getting into the function. I looked on some tutorials and they pretty much have the same code as me except for the objects names since most tutorials uses JButton instead of JMenuItem.

I can provide my whole code if it is needed, but I thought the rest would be irrelevant since it does not touch the menu bar or the textarea. I am using Eclipse Java EE IDE.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无畏 2024-12-12 01:27:14

我通常写类似的东西

mnuitemStats.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event)
    {// your logic here;
    }});

I usually write something like

mnuitemStats.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event)
    {// your logic here;
    }});
请叫√我孤独 2024-12-12 01:27:14

假设 mnuitmStats 是一个 JMenuItem,它应该是。多一点代码会有所帮助,但考虑到您应该使用 ActionListener 而不是 MouseListener 来实现此目的。

像这样的东西:

class MenuActionListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
    //do something

  }
}

JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);

JMenuItem newMenuItem = new JMenuItem("New");
newMenuItem.addActionListener(new MenuActionListener());
fileMenu.add(newMenuItem);

Assuming mnuitmStats is a JMenuItem, it should be. A little more code would be helpful but given that assumption you should use ActionListener not a MouseListener for this.

Something like:

class MenuActionListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
    //do something

  }
}

and

JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);

JMenuItem newMenuItem = new JMenuItem("New");
newMenuItem.addActionListener(new MenuActionListener());
fileMenu.add(newMenuItem);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文