我正在寻找回答这个问题的方法:如何计算整个jPanel的点击次数?以下代码仅计算一个像素的点击次数。

public class NewJFrame extends javax.swing.JFrame {


    public NewJFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jPanel1MouseClicked(evt);
            }
        });
        jPanel1.setLayout(new java.awt.BorderLayout());

        jLabel1.setText("Zapraszamy do klieknięcia");

        jButton1.setText("Algorytm przyrostowy");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(162, 162, 162)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(74, 74, 74)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(89, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jLabel1)
                .addGap(31, 31, 31)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(53, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
        int clicks = evt.getClickCount();
        int x = evt.getX();
        int y = evt.getY();


        System.out.println("Kliknołeś");

        double x1, y1, x2, y2;
        System.out.println("Współrzędne x: "+ x + ", y: "+ y + " Kliknoles: " + clicks);

    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        System.out.println("Kliknołeś JBUTTon");
    }

    private void formMouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
}

最佳答案

以下代码仅计算一个像素的点击次数。


实际上,它不计算与事件相关的点击次数。例如,如果操作系统将其报告为双击,则值将为2。通常,这基于单击之间的延迟,但最终取决于操作系统。

MouseEvent.getClickCount()


Returns the number of mouse clicks associated with this event.

Returns:
   integer value for the number of clicks



您是否要跟踪JPanel上发生的点击次数?如果是这样,我将添加一个鼠标侦听器,并保留一个变量,该变量每次单击都会增加。

//instance variable somewhere
int clickCount = 0;

//after you create your panel
panel.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
        clickCount++;
    }
});


编辑

tutorial on writing Mouse Listeners也相关:


int getClickCount()

  
  返回用户进行的连续快速单击(包括此事件)的次数。例如对于双击返回2。


如此暗示,Java不会将连续的点击汇总到单个事件中……如果您收到的getClickCount()等于1的点击事件,则单击可能仍然是代表双击的一系列单击中的第一个。

关于java - 如何计算整个jPanel的点击次数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5567740/

10-12 04:53