简体   繁体   English

如何计算整个jPanel的点击次数?

[英]How to count clicks on the whole jPanel?

I'm looking for answer this question: how to count clicks on whole jPanel? 我正在寻找回答这个问题的方法:如何计算整个jPanel的点击次数? Code below counts clicks only on the one pixel. 以下代码仅计算一个像素的点击次数。

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;
}

Code below counts clicks only on the one pixel. 以下代码仅计算一个像素的点击次数。

Actually no, it counts the number of clicks associated with the event. 实际上,它不计算与事件相关的点击次数。 For example, if the operating system reports it as a double click the value will be 2 . 例如,如果操作系统报告为双击,则该值为2 Typically this is based on the delay between clicks, but is ultimately up to the OS. 通常,这基于单击之间的延迟,但最终取决于操作系统。

MouseEvent.getClickCount() : MouseEvent.getClickCount()

 Returns the number of mouse clicks associated with this event. Returns: integer value for the number of clicks 

Are you trying to track the number of clicks that have ever happened on the JPanel? 您是否要跟踪JPanel上发生的点击次数? If so, I would add a mouse listener and keep a variable that increments with each click. 如果是这样,我将添加一个鼠标侦听器,并保留一个变量,该变量每次单击都会增加。

//instance variable somewhere
int clickCount = 0;

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

Edit 编辑

The tutorial on writing Mouse Listeners is also relevant: 有关编写“鼠标侦听器 ”的教程也与此相关:

 int getClickCount() 

Returns the number of quick, consecutive clicks the user has made (including this event). 返回用户连续快速单击(包括此事件)的次数。 For example, returns 2 for a double click. 例如对于双击返回2。

As this hints, Java does not aggregate consecutive clicks into a single event...if you receive a click event with getClickCount() equal to 1, that one click might still be the first click in the sequence of clicks representing a double click. 以此暗示,Java不会将连续的点击汇总到单个事件中……如果您收到的getClickCount()等于1的点击事件,则在表示双击的一系列点击中,仍然可能是第一次点击。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM