简体   繁体   English

如何使自己的mouseListener处理当前表单?

[英]How can I make my own mouseListener dispose of the current form?

I want to have my own MouseListener created in another class. 我想在另一个类中创建自己的MouseListener。 Easy to do. 容易做。 But I want it to destroy the current JFrame (close button) This is easy to do if you MouseListener is inside of the current class. 但是我希望它破坏当前的JFrame(关闭按钮),如果MouseListener在当前类的内部,这很容易做到。 But this is not what I want. 但这不是我想要的。 I would like to make my own generic class that could be attached to any close button with --> 我想创建自己的通用类,可以使用->将其附加到任何关闭按钮

     new myMouseCloseListener({form instance probably will go here})

I know what the trouble is. 我知道麻烦是什么。 I need to pass the class mouse listener a parameter that represents the current form. 我需要将代表当前表单的参数传递给类鼠标侦听器。 I have tried a couple of different things but it is not working. 我尝试了几种不同的方法,但是没有用。

I do not want to use a static variable. 我不想使用静态变量。

Question: How do I pass a variable of the current form to the myMouseCloseListener(...)? 问题:如何将当前形式的变量传递给myMouseCloseListener(...)?

Code: 码:

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class RichTextBox extends JFrame {
    private static final long serialVersionUID = 1L;
    private JPanel MyPanel;
    static RichTextBox t;

    public static void main(String[] args) {
        t = new RichTextBox();
        t.setVisible(true);
    }

    public RichTextBox() {
        setResizable(false);
         try {
           UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
         } catch (Exception ex) { }
        setTitle("Personal Note Entry");
        setForeground(new Color(255, 192, 203));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 420, 266);
        MyPanel = new JPanel();
        MyPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(MyPanel);
        MyPanel.setLayout(null);

        JTextArea yourNote = new JTextArea();
        yourNote.setWrapStyleWord(true);
        yourNote.setToolTipText("This will allow the user to enter a personal note for a specific transaction if they wish.");
        yourNote.setText("Enter your note!");
        yourNote.setTabSize(5);
        yourNote.setLineWrap(true);
        yourNote.setFont(new Font("Times New Roman", Font.PLAIN, 16));
        yourNote.setBackground(new Color(255, 248, 220));
        yourNote.setBounds(10, 45, 392, 147);
        MyPanel.add(yourNote);

        JButton btnClose = new JButton("Close");
        btnClose.addActionListener(new ActionListener(t)); 
        btnClose.setBounds(313, 203, 89, 23);
        //btnClose.addActionListener(new myMouseCloseListener(t));
        MyPanel.add(btnClose);

        JButton btnNewButton_1 = new JButton("Save");
        btnNewButton_1.setBounds(214, 203, 89, 23);
        MyPanel.add(btnNewButton_1);

        JLabel lblNewLabel = new JLabel("Please enter a personal note:");
        lblNewLabel.setFont(new Font("Times New Roman", Font.PLAIN, 16));
        lblNewLabel.setToolTipText("Label name associated with the text box");
        lblNewLabel.setBackground(new Color(255, 0, 0));
        lblNewLabel.setBounds(10, 11, 202, 23);
        MyPanel.add(lblNewLabel);
    }
}

class myMouseCloseListener implements MouseListener {

    RichTextBox temp = null; <-- not sure here (trying different things)

    myMouseCloseListener(RichTextBox r) {
        this.temp = r;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        temp.dispose();

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

}

Error: 错误:

  Compile Error

   The method addActionListener(ActionListener) in the type 
   AbstractButton is not applicable for the arguments (myMouseCloseListener)

The error message clearly explains the problem. 该错误消息清楚地说明了问题。 The method addActionListener(ActionListener) takes as its argument an ActionListener instance. 方法addActionListener(ActionListener)ActionListener实例作为其参数。 You're passing in a MouseListener instance. 您正在传递一个MouseListener实例。 If you want to pass in a MouseListener , you need to use addMouseListener(MouseListener) instead. 如果要传递MouseListener ,则需要改用addMouseListener(MouseListener)

You also don't need to pass in the JFrame instance. 您也不需要传入JFrame实例。 Off the top of my head, may not compile, double-check yourself: 在我头顶上,可能无法编译,请仔细检查一下自己:

@Override
public void mousePressed(final MouseEvent e) {
    final Component source = e.getComponent();
    final JFrame frame = (JFrame) SwingUtilities.getRoot(source);
    frame.dispose();
}

Of course, it would be much easier to add an ActionListener instead of a MouseListener , because then you're only implementing one method. 当然,添加ActionListener而不是MouseListener会容易得多,因为那样的话,您只实现一种方法。 If you insist on using a MouseListener , I strongly suggest extending MouseAdapter so you only have to override the methods you care about. 如果您坚持使用MouseListener ,我强烈建议扩展MouseAdapter这样您只需覆盖您关心的方法即可。

Continued from @Eric Stein, 接@Eric Stein,

An example if this using anonymous inner classes, which may be a lot simpler than making a whole another class for it: 一个使用匿名内部类的示例,这可能比为其创建另一个类要简单得多:

btnClose.addMouseListener(new MouseListener() {
    public void mouseClicked(MouseEvent me) {
         t.setVisible(false);
         t.dispose(); // If you want.
    }
    public void mouseEntered(MouseEvent me) {} // Other required impls for MouseListener
    public void mouseExited(MouseEvent me) {}
    public void mousePressed(MouseEvent me) {}
    public void mouseReleased(MouseEvent me) {}
});

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

相关问题 如何使用MouseListener调用另一个类? - How can i call another class with MouseListener? 如何在mouseListener中删除和创建对象? - How can I delete and create an object in a mouseListener? 如何在我的矩形(自定义按钮)中添加鼠标监听器以重新执行Java中的paintComponent? - How can I add a mouselistener to my rectangle(custom button) to re-execute paintcomponent in java? 机器人框架:如何获取 selenium webdriver 的当前实例来编写自己的关键字? - Robot framework: how can I get current instance of selenium webdriver to write my own keywords? 如何使此Mouselistener只关心一个对象? - How do I make this mouselistener only care about one object? 如何让我的 MouseListener 方法在 Java 中工作? - How do I get my MouseListener method to work in Java? 如何正确地为我的 Jcomponent 实现鼠标侦听器? - How do I properly implement a mouselistener to my Jcomponent? 我如何使用MouseListener访问一个组件而不是另一个组件 - How can i access one component over another using mouselistener 如何在Java中将动作赋予“ MouseListener”以绘制形状? - how can I give the action to “MouseListener” to draw a shape in java? 如何在MouseListener中将一帧移到另一帧 - How can i move one frame to another in MouseListener
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM