图像的事件处理程序在 Java GUI 中显示错误

The image's event handler shows error in Java GUI

我创建了 r Jbutton 并将图像插入 c JButton。我想要做的就是向 r JButton 添加一个事件处理程序,当它被单击时,c button 中的图像将按照方法 roll() 中指定的方式进行更改。但它显示处理程序 class 中的错误与 r.roll 行。有人可以告诉我如何将处理程序添加到 r JButton 中,以便在单击 r JButton 时执行方法滚动并更改图像吗?

import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import java.util.Random;
import javax.imageio.*;

public class Background extends JFrame{      
  private Random ran;
  private int value;
  private JButton r;
  private JButton c;

  public Background ()
  {
    super("title");
    ran = new Random();
    value = nextValue() ;
    setLayout(new FlowLayout());

    r=new JButton("ROLL ");
    r.setForeground(Color.WHITE);//ndryshon ngjyren e shkrimit
    r.setBackground(Color.YELLOW);
    add(r,BorderLayout.SOUTH);    

    Icon i=new ImageIcon(getClass().getResource("1.png"));
    Icon im=new ImageIcon(getClass().getResource("2.png"));

    c= new JButton(i);
    add(c);

    thehandler hand=new thehandler(this);//konstruktori i handler merr nje instance te Background
    r.addActionListener(hand);
    c.addActionListener(hand);
  }

  private int nextValue() {
    return Math.abs(ran.nextInt()) % 6 + 1 ;
  }

  public void roll() {
    value = nextValue() ;
    if (value==1){
      Icon i=new ImageIcon(getClass().getResource("1.png"));
      c= new JButton(i);
      add(c);
    } else if(value==2){
      Icon im=new ImageIcon(getClass().getResource("2.png"));
      c= new JButton(im);
      add(c);
    }

    repaint() ;
  }

  public int getValue() {
    return value ;
  }

  private class thehandler implements ActionListener{
    private Background m ;

    thehandler(Background thisone) {
      m = thisone ;
    }

    public void actionPerformed(ActionEvent event) {
      m.roll();
      r.roll();//ERROR  
    }
  }

  public static void main(String[] args) {       
    Background  d = new Background() ;
    d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    d.getContentPane().setBackground(Color.GREEN);
    d.setSize(700,500);
    d.setVisible(true);  
  }
}

为什么要在 roll 中创建 JButton 的新实例?你只需要改变屏幕上已经显示的按钮的图标

这个...

public void roll() {
    value = nextValue() ;
    if (value==1){
        Icon i=new ImageIcon(getClass().getResource("1.png"));
        c= new JButton(i);
        add(c);
    } else if(value==2){
        Icon im=new ImageIcon(getClass().getResource("2.png"));
        c= new JButton(im);
        add(c);
    }

    repaint() ;
}

应该...

public void roll() {
    value = nextValue() ;
    Icon i = null;
    if (value==1){
        i=new ImageIcon(getClass().getResource("1.png"));
    } else if(value==2){
        i=new ImageIcon(getClass().getResource("2.png"));
    }
    c.setIcon(i);
}

setIcon是一个绑定字段,也就是说它会自动生成一个属于自己的repaint请求。

如果图片没有更新那么很可能是你的图片没有加载,你可以通过两种方式进行测试,首先,你可以设置按钮的文本

public void roll() {
    value = nextValue() ;
    c.setText(Integer.toString(value));
    Icon i = null;
    if (value==1){
        i=new ImageIcon(getClass().getResource("1.png"));
    } else if(value==2){
        i=new ImageIcon(getClass().getResource("2.png"));
    }
    c.setIcon(i);

    repaint() ;
}

其次,您应该使用 ImageIO.read 来加载资源,例如...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Background extends JFrame {

    private Random ran;
    private int value;
    private JButton r;
    private JButton c;

    public Background() {
        super("title");
        ran = new Random();
        value = nextValue();
        setLayout(new FlowLayout());

        r = new JButton("ROLL ");
        r.setForeground(Color.WHITE);//ndryshon ngjyren e shkrimit
        r.setBackground(Color.YELLOW);
        add(r);

        try {
            BufferedImage die = ImageIO.read(getClass().getResource("1.png"));
            c = new JButton(new ImageIcon(die));
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        add(c);

        thehandler hand = new thehandler(this);//konstruktori i handler merr nje instance te Background
        r.addActionListener(hand);
        c.addActionListener(hand);
    }

    private int nextValue() {
        return Math.abs(ran.nextInt()) % 6 + 1;
    }

    public void roll() {
        value = nextValue();
        c.setText(Integer.toString(value));
        URL path = null;
        if (value == 1) {
            path = getClass().getResource("1.png");
        } else if (value == 2) {
            path = getClass().getResource("2.png");
        }
        try {
            BufferedImage die = ImageIO.read(path);
            c.setIcon(new ImageIcon(die));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public int getValue() {
        return value;
    }

    private class thehandler implements ActionListener {

        private Background m;

        thehandler(Background thisone) {
            m = thisone;
        }

        public void actionPerformed(ActionEvent event) {
            m.roll();
        }
    }

    public static void main(String[] args) {
        Background d = new Background();
        d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        d.getContentPane().setBackground(Color.GREEN);
        d.setSize(700, 500);
        d.setVisible(true);
    }
}

我抛出 NullPointerException,这意味着 Java 无法找到您的图像。根据您的代码,您的图像必须存储在与 class

相同的包中

查看 Reading/Loading an Image 了解有关 ImageIO

的更多详细信息

这个问题...

r.roll();//ERROR  

JButton没有roll方法