gpt4 book ai didi

java - 如何让我自己的 mouseListener 处理当前表单?

转载 作者:行者123 更新时间:2023-12-01 22:58:33 24 4
gpt4 key购买 nike

我想在另一个类中创建我自己的 MouseListener。容易做。但我希望它销毁当前的 JFrame(关闭按钮) 如果您的 MouseListener 位于当前类的内部,那么这很容易做到。但这不是我想要的。我想制作自己的通用类,可以使用 -->

附加到任何关闭按钮
     new myMouseCloseListener({form instance probably will go here})

我知道问题出在哪里了。我需要向鼠标监听器类传递一个表示当前表单的参数。我尝试了几种不同的方法,但没有成功。

我不想使用静态变量。

问题:如何将当前表单的变量传递给 myMouseCloseListener(...)?

代码:

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

}

}

错误:

  Compile Error

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

最佳答案

错误消息清楚地说明了问题。 addActionListener(ActionListener) 方法将 ActionListener 实例作为其参数。您正在传递一个 MouseListener 实例。如果您想传入 MouseListener,则需要使用 addMouseListener(MouseListener) 来代替。

您也不需要传入 JFrame 实例。我突然想到,可能无法编译,请仔细检查一下自己:

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

当然,添加 ActionListener 而不是 MouseListener 会容易得多,因为这样您只需实现一种方法。如果您坚持使用 MouseListener,我强烈建议扩展 MouseAdapter,这样您只需重写您关心的方法。

关于java - 如何让我自己的 mouseListener 处理当前表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23682467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com