簡體   English   中英

如何使自己的mouseListener處理當前表單?

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

我想在另一個類中創建自己的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這樣您只需覆蓋您關心的方法即可。

接@Eric Stein,

一個使用匿名內部類的示例,這可能比為其創建另一個類要簡單得多:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM