1.我正在制作一个cookie点击克隆我知道这么成熟我只有12岁并且我正在测试我的能力。我有一个问题,我试图让标签更新,但它只是赢了 尝试了一切 对于奇怪的缩进和混乱我也提前抱歉我不擅长制作漂亮的代码 类
package learning;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import javax.swing.JPanel;
public class Learning extends JFrame implements MouseListener {
int clicks;
boolean Update;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Learning().start();
}
public void start(){
ImageImplement panel = new ImageImplement(new ImageIcon("Cookie.jpg").getImage());
add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(600,600);
setResizable(false);
JLabel Click = new JLabel("Clicks: " + clicks);
Click.setFont(new Font("Arial",Font.PLAIN , 20));
panel.add(Click);
Click.setSize(100,100);
Click.setVisible(true);
addMouseListener(this);
if(Update == true){
Click.setText("Clicks: "+ clicks);
System.out.println("Reached");
}
}
@Override
public void mouseClicked(MouseEvent e) {
clicks += 1;
System.out.println(clicks);
Update = true;
if(Update = true){
Update = false;
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
其他图片类
package learning;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;
class ImageImplement extends JPanel {
private Image img;
public ImageImplement(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
@Override
public void paintComponent(Graphics g){
g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
}
}
答案 0 :(得分:2)
事件可能在任何时候出于多种原因发生,具体取决于事件。这意味着......
if(Update == true){
Click.setText("Clicks: "+ clicks);
System.out.println("Reached");
}
永远不会是true
,因为在程序解释此命令时事件尚未发生
要解决此问题,您的mouseClicked
事件处理程序将需要了解您要更新的对象。目前,您在start
方法...
public void start(){
//...
ImageImplement panel = new ImageImplement(new ImageIcon("Cookie.jpg").getImage());
//...
JLabel Click = new JLabel("Clicks: " + clicks);
}
您需要更改这些内容,以便可以在类实例级别访问它们
public class Learning extends JFrame implements MouseListener {
int clicks;
boolean Update;
private ImageImplement panel;
private JLabel Click
public void start(){
//...
//ImageImplement panel = new ImageImplement(new ImageIcon("Cookie.jpg").getImage());
panel = new ImageImplement(new ImageIcon("Cookie.jpg").getImage());
//...
//JLabel Click = new JLabel("Clicks: " + clicks);
Click = new JLabel("Clicks: " + clicks);
}
这将允许您从当前类的任何实例中的任何方法访问这些对象。
然后,在mouseClicked
处理程序中,您可以更新Click
标签...
@Override
public void mouseClicked(MouseEvent e) {
clicks += 1;
Click.setText("Clicks: "+ clicks);
}
鼠标事件与MouseListener
注册的组件具有上下文关系。这意味着一些事情,但在您的情况下,JLabel
和ImageImplement
可能阻止阻止鼠标事件到达MouseListener
注册的组件
相反,最好将MouseListener
添加到ImageImplement
而不是......
addMouseListener(panel);
JLabel
能够显示图像,除非您正在进行某种图像处理或图形效果,否则可能更容易使用它。super.paintComponent
ImageImplement
中致电paintComponent
。setPreferred/Minimum/MaximumSize
,而应覆盖这些方法,以达到理想的效果