添加错误时,事件处理成一个JButton重绘图像inJava GUI [英] Error when adding event handler into a JButton to repaint the image inJava GUI

查看:279
本文介绍了添加错误时,事件处理成一个JButton重绘图像inJava GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建2 JButtons.One其中有一个按钮的功能和其他处理的image.I想要的形象来改变时,这个按钮是clicked.So插入的方法重绘(),并添加一个监听器第一个JButton的改变image.When尝试添加监听器或事件处理程序的第一个JButton没什么happens.So图像不change.Can有人告诉我我怎么能以一种方式,它的工作原理插入这个监听器(改变这里是我的code按钮被点击时,图像):

 进口java.awt中的*。
java.awt.event中导入*。
javax.swing.event中导入*。
进口的javax.swing *。
进口了java.util.Random;公共类返回扩展的JFrame {    私人随机RAN;
    私人int值;
    私人的JButton - [R;
    私人的JButton℃;    公共返回(){
        超(称号);
        跑=新的随机();
        值= nextValue();
        的setLayout(新的FlowLayout());
        R =的新的JButton(ROLL);
        加(R);
         图标I =新的ImageIcon(的getClass()的getResource(1.png)。);
         图标IMG =新的ImageIcon(的getClass()的getResource(2.png)。);
         C =的新的JButton(I)        如果(价值== 1){
             C =的新的JButton(I)        }
        否则,如果(价值== 2){
             C =的新的JButton(IMG);        }
        加(C);         thehandler手=新thehandler(本); // konstruktori我处理大豆 - NJE例如TE背景
         r.addActionListener(手);
         c.addActionListener(手);    }
     私人诠释nextValue(){
         返回Math.abs(ran.nextInt())%6 + 1;
         }
     公共无效卷(){
         值= nextValue();         重绘();
         }
     公众诠释的getValue(){
         返回值;
         }
     私有类thehandler实现的ActionListener {
         私人返回D组;
         thehandler(返回thisone){
                D = thisone; }
         公共无效的actionPerformed(ActionEvent的事件){
             d.roll();
         }
         }
    公共静态无效的主要(字串[] args){         返回D =新返回();         d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         。d.getContentPane()的setBackground(Color.GREEN);
         d.setSize(700500);         d.setVisible(真);
    }
}


解决方案

所以,基本上,所有的code归结到这里...

 公共无效卷(){
    值= nextValue();    重绘();
}

此计算一个新的随机值,并呼吁重新绘制。但是,没有在你的code是由的时间点上,它被称为影响。

相反,你需要更新一些控制的状态,也许更多的东西一样...

 公共无效卷(){
    值= nextValue();    图标I =新的ImageIcon(的getClass()的getResource(1.png)。);
    图标IMG =新的ImageIcon(的getClass()的getResource(2.png)。);
    如果(价值== 1){
        c.setIcon(ⅰ);
    }否则如果(价值== 2){
        c.setIcon(IMG);
    }
}

接下来的事情我会做的是所有的图像存储在某些类型的数组或列表来使它更容易访问,那么你可以简单地做一些事情
像...

 公共无效卷(){
    值= nextValue();
    c.setIcon(listOfImages.get(值 - 1));
}

也许看看<一个href=\"http://stackoverflow.com/questions/28342538/java-swing-timer-and-animation-how-to-put-it-together/28342986#28342986\">Java摆动定时器和动画:如何把它一起,了解更详细的例子。

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

解决方案

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

这篇关于添加错误时,事件处理成一个JButton重绘图像inJava GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆