Error when adding event handler into a JButton to repaint the image inJava GUI

风流意气都作罢 提交于 2019-12-25 17:03:13

问题


I've created 2 JButtons.One of them has the function of a button and the other handles an image.I want that image to change when this button is clicked.So inserted the method repaint() and added a listener to the first JButton to change the image.When trying to add the listener or the event handler to the first JButton nothing happens.So the image doesn't change.Can anyone show me how can I insert this listener in a way that it works(changes the image when the button is clicked)?Here is my code:

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

public class Back extends JFrame{

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

    public Back(){


        super("title");
        ran = new Random();
        value = nextValue();
        setLayout(new FlowLayout());
        r=new JButton("ROLL");
        add(r);
         Icon i=new ImageIcon(getClass().getResource("1.png"));
         Icon img=new ImageIcon(getClass().getResource("2.png"));
         c= new JButton(i);

        if (value==1){
             c= new JButton(i);

        }
        else if(value==2){
             c= new JButton(img);

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

         repaint() ;
         }
     public int getValue() {
         return value ;
         }
     private class thehandler implements ActionListener{
         private Back d;
         thehandler(Back thisone) {
                d = thisone ; }
         public void actionPerformed(ActionEvent event) {
             d.roll() ;
         }
         }
    public static void main(String[] args) {

         Back  d = new Back() ;

         d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         d.getContentPane().setBackground(Color.GREEN);
         d.setSize(700,500);

         d.setVisible(true);    
    }
}

回答1:


So, basically, all your code comes down to here...

public void roll() {
    value = nextValue();

    repaint();
}

This calculates a new random value and calls repaint. But nothing in your code is effected by value at the point in time it's called.

Instead, you need to update the state of some control, maybe something more like...

public void roll() {
    value = nextValue();

    Icon i = new ImageIcon(getClass().getResource("1.png"));
    Icon img = new ImageIcon(getClass().getResource("2.png"));
    if (value == 1) {
        c.setIcon(i);
    } else if (value == 2) {
        c.setIcon(img);
    }
}

The next thing I would do is store all your images in some kind of array or List to make it easier to access, then you could simply do something like...

public void roll() {
    value = nextValue();
    c.setIcon(listOfImages.get(value - 1));
}

Maybe have a look at Java Swing Timer and Animation: how to put it together for a more detailed example



来源:https://stackoverflow.com/questions/34936103/error-when-adding-event-handler-into-a-jbutton-to-repaint-the-image-injava-gui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!