简体   繁体   English

Java Jlabel更新文本

[英]Java Jlabel Update Text

1.I am making a cookie click clone i know so mature I'm only 12 and I'm testing my abilities. 1.我正在制作一个cookie单击克隆,我知道我已经很成熟了,我才12岁,我正在测试自己的能力。 I have a problem I'm trying to get a label to update but it just won't tried everything Also sorry in advance for weird indentation and messiness I'm not great at making good looking code class 我有一个问题,我正在尝试更新标签,但它只是无法尝试所有操作。还要提前对怪异的缩进和混乱感到抱歉,我也不擅长制作漂亮的代码类

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) {

}













}

Other picture class 其他图片类

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

}
}

Problem #1 问题1

Swing, like most GUI's, is event driven, that is something happens and you respond to it. 像大多数GUI一样,Swing是事件驱动的,发生了某些事情,您对此做出了响应。 This makes your program non-linear (the code doesn't progress in a straight line). 这会使您的程序成为非线性程序(代码不会直线执行)。

Events can happen at any time for a multitude of reasons, depending on the event. 事件可能会因多种原因随时发生,具体取决于事件。 This means... 这意味着...

if(Update == true){
     Click.setText("Clicks: "+ clicks);
     System.out.println("Reached");
}

Will never be true , because the event has not occurred at the time the program interprets this command 永远不会为true ,因为在程序解释此命令时尚未发生该事件

Problem #2 问题二

To over come this issue, your mouseClicked event handler will need to know about the objects you want to update. 要解决此问题,您的mouseClicked事件处理程序将需要了解您要更新的对象。 Currently, you are declaring your variables within a local scope, within the start method... 当前,您是在start方法中的局部范围内声明变量。

public void start(){
    //...
    ImageImplement panel = new ImageImplement(new ImageIcon("Cookie.jpg").getImage());   
    //...
    JLabel Click = new JLabel("Clicks: " + clicks);
}

You will need to change these so that they are accessible at a class instance level 您将需要更改它们,以便在类实例级别可以访问它们。

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

This will allow you to access these objects from any method within any instance of the current class. 这将允许您从当前类的任何实例内的任何方法访问这些对象。

Then, within your mouseClicked handler, you can update the Click label... 然后,在mouseClicked处理程序中,您可以更新Click标签...

@Override
public void mouseClicked(MouseEvent e) {
    clicks += 1;
    Click.setText("Clicks: "+ clicks);
}

Problem #3 问题三

Mouse events are contextual to the component that the MouseListener is registered. 鼠标事件与注册MouseListener的组件有关。 This means a few things, but in your case, it's possible that the JLabel and ImageImplement could potentially block block mouse events from reaching the component that the MouseListener is registered to. 这意味着一些事情,但是就您而言, JLabelImageImplement可能会阻止阻止鼠标事件到达注册MouseListener的组件。

Instead, it might be better to add the MouseListener to the ImageImplement instead... 相反,最好将MouseListener添加到ImageImplement

addMouseListener(panel);

Additional 额外

  • JLabel is capable of displaying images, unless you're playing on doing some kind of image manipulation or graphical effect, it might just be easier to use it instead. JLabel能够显示图像,除非您正在玩某种图像处理或图形效果,否则使用它可能会更容易。
  • You should be calling super.paintComponent in your ImageImplement 's paintComponent before doing any additional painting. 在进行任何其他绘画之前,应该在ImageImplementpaintComponent调用super.paintComponent
  • You should avoid using setPreferred/Minimum/MaximumSize and instead, override these methods as you need to achieve your desired results 您应避免使用setPreferred/Minimum/MaximumSize ,而应根据需要重写这些方法以实现所需的结果

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

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