我正在寻找这个问题的答案:如何计算整个 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;
}
最佳答案
Code below counts clicks only on the one pixel.
实际上不是,它计算与事件关联的点击次数。例如,如果操作系统将其报告为双击,则该值将为 2
。通常,这是基于点击之间的延迟,但最终取决于操作系统。
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()
Returns the number of quick, consecutive clicks the user has made (including this event). For example, returns 2 for a double click.
正如本文所暗示的,Java 不会将连续点击聚合到单个事件中...如果您收到 getClickCount()
等于 1 的点击事件,则该一次点击可能仍然是第一次点击按代表双击的点击顺序。
关于java - 如何统计整个jPanel的点击次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5567740/