在单击按钮本身后,如何访问按钮(或任何其他元素)的 X-Y 坐标。我的代码是,
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
Object source2 = evt.getSource();
System.out.println(source2);
}
哪些打印,
javax.swing.JButton[,591,66,74x28,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.synth.SynthBorder@59b3c6aa,flags=288
那么,如何获得 "591"
和 "66"
作为整数。
提前致谢。
最佳答案
在检查它是否是 JButton 的实例后,您可以将其转换为 JButton,在下面的示例中我们访问的是 x > 和 y
值:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
if (evt.getSource() instanceof JButton) {
JButton source2 = (JButton) evt.getSource();
System.out.println(source2.getX());
System.out.println(source2.getY());
}
}
关于Java : How to get button position after self click-action,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53542381/