检测多次双击/重置mouseevent?

xmakbtuz  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(418)

我正在尝试使用 JTables 导航文件系统。我遇到的问题是,只要你一直按这个按钮,鼠标事件就会一直运行,因此如果有人试图通过连续双击“上一级”字段(因为它不移动)来上多个级别,它只会返回到一个目录,除非他们长时间停止单击。因此,我意识到这是由于 MouseEvent 仍然很活跃。
我很确定只要做一件事我就能做我想做的事 if(e.getClickCount() % 2 == 0) 但这感觉像是一个不正确的解决方法。是否有更好/更正确的方法来执行此操作,例如重置 MouseEvent 在最初的双击之后?有没有可能重置 MouseEvent 手动?如果是,怎么做?
编辑:恢复为一体式代码,以便于复制/粘贴:

package main;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileSystemView;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

public class Main {

    public static void main(String[] args) {
        new CreateFrame();
    }
}

class CreateFrame extends JFrame {
    public CreateFrame() {
        setPreferredSize(new Dimension(800, 600));
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
        setContentPane(new LocalNavigator());
        pack();
        setVisible(true);
    }

    class LocalTable extends JTable {

        private DefaultTableModel dtm;
        private File path;
        private FileSystemView fsv = FileSystemView.getFileSystemView();

        public LocalTable() {
            String[] header = new String[] { "Icon", "File/Folder Name", "File Type" };

            dtm = new DefaultTableModel() {
                @Override
                public boolean isCellEditable(int row, int column) {
                    return false;
                }

                @Override
                public Class<?> getColumnClass(int column) {
                    if (getRowCount() > 0) {
                        return getValueAt(0, column).getClass();
                    }

                    return super.getColumnClass(column);
                }
            };
            dtm.setColumnIdentifiers(header);
            setAutoCreateRowSorter(true);
            setRowHeight(25);
            setModel(dtm);

            addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() == 2) {
                        JTable table = (JTable) e.getSource();
                        Point p = e.getPoint();
                        int row = table.rowAtPoint(p);
                        String type = getValueAt(row, 2).toString().toLowerCase();
                        if (type.contains("root") || type.contains("folder")) {
                            File file = new File((path != null ? path + "/" + getValueAt(row, 1) : getValueAt(row, 1).toString()));
                            if (file != null)
                                repopulateFileFolderList(file);
                        } else if (getValueAt(row, 1).toString().toLowerCase().contains("up one")
                            && getValueAt(row, 2).toString().equals("")) {
                            File file = path.getParentFile();
                            if (file != null)
                                repopulateFileFolderList(file);
                            else
                                getTableRoot();
                        } else {
                            System.out.println("Not a directory!");
                        }

                    }
                }
            });
        }

        public void getTableRoot() {
            path = null;
            while (dtm.getRowCount() > 0) {
                dtm.removeRow(0);
            }
            File[] roots = File.listRoots();
            for (File root : roots) {
                dtm.addRow(new Object[] { fsv.getSystemIcon(root), root.getAbsoluteFile(), "Root Directory" });
            }
        }

        private void repopulateFileFolderList(File folder) {
            path = folder;
            while (dtm.getRowCount() > 0) {
                dtm.removeRow(0);
            }
            dtm.addRow(new Object[] { fsv.getSystemIcon(folder), "Up One Level", "" });
            File[] files = folder.listFiles();
            for (File file : files) {
                dtm.addRow(new Object[] { fsv.getSystemIcon(file), file.getName(),
                    (file.isDirectory() ? "Folder" : file.isFile() ? "File" : "Unknown") });
            }
        }
    }

    class LocalNavigator extends JPanel {

        private LocalTable localTable;

        public LocalNavigator() {
            setLayout(new BorderLayout());

            localTable = new LocalTable();
            localTable.getTableRoot();

            JScrollPane lTree = new JScrollPane(localTable);

            add(lTree, "Center");

            TableColumnModel columns = localTable.getColumnModel();
            for (int c = 0;c < columns.getColumnCount();c++) {
                if (c == 0)
                    columns.getColumn(c).setMaxWidth(25);
                else
                    columns.getColumn(c).setPreferredWidth(50);
            }
        }
    }
}
sy5wg1nm

sy5wg1nm1#

在您的需求中,您可以解释您希望允许快速延迟的双击。但是 e.getClickCount()==2 您使用的不是解决方案,因为它受到操作系统配置的限制。你可以通过查看 Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval") 绕过这个问题的一个简单而好的方法是,在鼠标侦听器中处理两次单击之间的延迟。你可以使用一个定时器,在那里你可以选择你的点击间隔最大值,并处理自己的计数。
解决办法相当简单。既然你的代码是可测试的,我就直接修改并测试了。这对我很有用。为了总结我的修改,我添加了一个布尔示例来计算当前的点击次数,并将双击检测(技术)和双击处理(逻辑)分为两个位置。更具可读性。我已选择0.5秒作为两次单击之间的最大授权延迟。您可以选择另一个值,但如果该值太重要(例如一秒钟或更长时间),则可能会出现意外的双击。

class CreateFrame extends JFrame {
    ...
   private boolean isAlreadyOneClick;
   ...
   addMouseListener(new MouseAdapter() {

    @Override
    public void mouseClicked(MouseEvent e) {

        if (isAlreadyOneClick) {
           handleDoubleClick(e);
           isAlreadyOneClick = false;
        }
        else {
           isAlreadyOneClick = true;
           Timer t = new Timer("doubleclickTimer", false);
           t.schedule(new TimerTask() {

            @Override
            public void run() {
               isAlreadyOneClick = false;
            }
           }, 500);
        }

    }

    private void handleDoubleClick(MouseEvent e) {
        JTable table = (JTable) e.getSource();
        Point p = e.getPoint();
        int row = table.rowAtPoint(p);
        String type = getValueAt(row, 2).toString().toLowerCase();
        if (type.contains("root") || type.contains("folder")) {
        File file = new File((path != null ? path + "/" + getValueAt(row, 1) : getValueAt(row, 1).toString()));
        if (file != null)
            repopulateFileFolderList(file);
        }
        else if (getValueAt(row, 1).toString().toLowerCase().contains("up one")
            && getValueAt(row, 2).toString().equals("")) {
        File file = path.getParentFile();
        if (file != null)
            repopulateFileFolderList(file);
        else getTableRoot();
        }
        else {
        System.out.println("Not a directory!");
        }
    }
    }

相关问题