我正在尝试使用“刷新”按钮刷新图标面板,但无法弄清楚在事件处理程序中放置什么来执行此任务。我只有一次执行任务的代码(如下)。有人可以帮我弄这个吗?
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class FourRandomCards extends JFrame {
JButton jbtRefresh = new JButton("Refresh");
CardsPanel cardsPanel = new CardsPanel();
public FourRandomCards() {
add(cardsPanel, BorderLayout.CENTER);
add(jbtRefresh, BorderLayout.SOUTH);
//=============================================================
/** can't figure out what to put in event-handler **/
jbtRefresh.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
//=============================================================
}
public static void main(String[] args) {
JFrame frame = new FourRandomCards();
frame.setTitle("Four Random Cards");
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private class CardsPanel extends JPanel {
public CardsPanel() {
for (int i = 0; i < 4; i++) {
int card = (int)(Math.random() * 54 + 1);
ImageIcon cardIcon = new ImageIcon
("image/card/" + card + ".png");
JLabel jlblCard = new JLabel(cardIcon);
add(jlblCard);
}
}
}
}