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

时间:2016-01-22 18:48:41

标签: java image swing random awt

我创建了r Jbutton并将图片插入c JButton。我想要做的就是向r JButton添加一个事件处理程序,当它点击c button中的图像时,按照方法roll()中的指定进行更改。但它在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);  
  }
}

1 个答案:

答案 0 :(得分:1)

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

此...

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无法找到你的图像。根据您的代码,您的图像必须存储在与您的类相同的包中

有关ImageIO

的更多详情,请查看Reading/Loading an Image

这个问题......

r.roll();//ERROR  

JButton没有roll方法