我正在寻找回答这个问题:如何计算整个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;
}
答案 0 :(得分:4)
以下代码只对一个像素点击次数。
实际上不是,它计算与事件相关的点击次数。例如,如果操作系统将其报告为双击,则值为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()
返回用户所做的快速连续点击次数(包括此事件)。例如,双击返回2。
正如此提示,Java不会将连续点击聚合到单个事件中...如果您收到getClickCount()
等于1的点击事件,那么一次点击可能仍然是点击序列中的第一次点击代表双击。