简体   繁体   中英

refresh a jFrame with a JButton ActionListener

I'm trying a simple app that would pick 4 random cards and display them on 4 buttons so far it's all good the thing is I want to add a refresh button that would refresh the random sections each time it's invoked but I can't get that done I've tried revalidate(); and repaint(); with no success.
I hope somebody can help me with this, thanks in advance.

    import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class rest extends JFrame{
    public rest(){
        int x=(int)(Math.random()*55);
        int y=(int)(Math.random()*55);
        int z=(int)(Math.random()*55);
        int b=(int)(Math.random()*55);
        ImageIcon i1= new ImageIcon("image/card/"+x+".png");
        ImageIcon i2= new ImageIcon("image/card/"+y+".png");
        ImageIcon i3= new ImageIcon("image/card/"+z+".png");
        ImageIcon i4= new ImageIcon("image/card/"+b+".png");
        JButton b1 = new JButton();
        b1.setIcon(i1);     
        JButton b2 = new JButton();
        b2.setIcon(i2);
        JButton b3 = new JButton();
        b3.setIcon(i3);
        JButton b4 = new JButton();
        b4.setIcon(i4);
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(1,4,5,5));
        p.add(b1);      
        p.add(b2);
        p.add(b3);
        p.add(b4);
        JPanel p1= new JPanel();
        JButton b5 = new JButton("refresh");
        p1.setLayout(new BorderLayout());
        p1.add(b5,BorderLayout.EAST);
        setLayout(new BorderLayout());
        add(p,BorderLayout.CENTER);
        add(p1,BorderLayout.SOUTH);
        b5.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //revalidate();
                //repaint();
            }
        });
    }

    public static void main(String[] args) {
        rest f= new rest();
        f.setTitle("poker");
        f.setSize(300,300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.setVisible(true);

    }

}

revalidate/repaint will do nothing. You need to actually create the new random icon and set the icons to the buttons again eg

@Override
public void actionPerformed(ActionEvent e) {
    int random = (int)(Math.random()*55);
    b1.setIcon(new ImageIcon("image/card/"+random+".png"));
    random = (int)(Math.random()*55);
    b2.setIcon(new ImageIcon("image/card/"+random+".png"));
    random = (int)(Math.random()*55);
    b3.setIcon(new ImageIcon("image/card/"+random+".png"));
    random = (int)(Math.random()*55);
    b4.setIcon(new ImageIcon("image/card/"+random+".png"));
}

You will also need to declare the buttons as final , since you are accessing them locally from an anonymous class ie final JButton b1 = new JButton();


As a side note, you constructor will become pretty large, as you application grows, if you continue to make all your objects locally. You may want to consider using some class members. Maybe create the buttons as class members. Then have a method reset() that will initialize the buttons with new random icons. For one, this will get rid of duplicate code, like in this case, where you're using two sets of code to do exactly the same thing. Instead just call the method. Once, in the constructor to first initialize them, then call it again in the reset listener

Also Use Java naming convention. Class names begin with upper case letters. rest -> Rest

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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