java 按钮换成图片_java – 单击按钮将ImageIcon更改为另一张图片

本文介绍了一个Java程序,实现点击按钮时JLabel中的ImageIcon自动更换。程序创建了一个Card_panel类,包含两个按钮“Higher”和“Lower”,每次点击按钮会更新图片。用户选择“Higher”或“Lower”后,程序会加载新的ImageIcon。
摘要由CSDN通过智能技术生成

所以我想在每次按下按钮时替换JLabel中的

ImageIcon.我做了它,因此图像,标签和GridBagConstraints是公开的.当我试图改变它虽然没有任何反应.

我是以错误的方式来做这件事还是?

谢谢!

package hi.low;

import java.awt.*;

import java.awt.event.*;

import java.util.ArrayList;

import javax.swing.*;

import java.util.Random;

import java.util.ArrayList;

public class Card_panel extends JPanel implements ActionListener

{

private final int WIDTH = 400, HEIGHT = 200;

private static String[] imageList = {

"images/2h.png", "images/3h.png", "images/4h.png", "images/5h.png", "images/6h.png",

"images/7h.png", "images/8h.png", "images/9h.png", "images/th.png", "images/jh.png",

"images/qh.png", "images/kh.png", "images/ah.png", "images/2d.png", "images/3d.png",

"images/4d.png", "images/5d.png", "images/6d.png", "images/7d.png", "images/8d.png",

"images/9d.png", "images/td.png", "images/jd.png", "images/qd.png", "images/kd.png",

"images/ad.png", "images/2c.png", "images/3c.png", "images/4c.png", "images/5c.png",

"images/6c.png", "images/7c.png", "images/8c.png", "images/9c.png", "images/tc.png",

"images/jc.png", "images/qc.png", "images/kc.png", "images/ac.png", "images/2s.png",

"images/3s.png", "images/4s.png", "images/5s.png", "images/6s.png", "images/7s.png",

"images/8s.png", "images/9s.png", "images/ts.png", "images/js.png", "images/qs.png",

"images/ks.png", "images/as.png"

};

private static int imageNum = -1;

GridBagConstraints gbc = new GridBagConstraints();

GridBagConstraints c = new GridBagConstraints();

ImageIcon image;

JLabel label;

private static ArrayList deck;

private static Card tempCard, currentCard;

public Card_panel()

{

deck = new ArrayList();

char[] suits = {'h', 'd', 'c', 's'};

char[] values = {'2', '3', '4', '5', '6', '7', '8', '9', 't', 'j', 'q', 'k', 'a'};

for(int a = 0; a

{

for(int b = 0; b

{

tempCard = new Card(suits[a],values[b]);

deck.add(tempCard);

}

}

int rand_num;

int cards_left = 52;

Random generator = new Random( System.currentTimeMillis() );

for(int a = 0; a<52; a++)

{

rand_num = generator.nextInt(cards_left);

currentCard = deck.get(rand_num);

deck.remove(rand_num);

cards_left -= 1;

}

setPreferredSize(new Dimension(WIDTH, HEIGHT));

setBackground (Color.green.darker().darker());

setLayout(new GridBagLayout());

gbc.gridx = 0;

gbc.gridy = 0;

gbc.gridwidth = 2;

image = new ImageIcon(imageList[0]);

label = new JLabel("", image, JLabel.CENTER);

add( label, gbc );

gbc.gridx = 0;

gbc.gridy++;

gbc.gridwidth = 1;

JButton higher = new JButton("Higher");

higher.setActionCommand("higher");

higher.addActionListener (this);

add( higher, gbc );

gbc.gridx++;

JButton lower = new JButton("Lower");

lower.setActionCommand("lower");

lower.addActionListener (this);

add( lower, gbc );

}

@Override

public void actionPerformed(ActionEvent e)

{

String Action;

Action = e.getActionCommand ();

if (Action.equals ("higher"))

{

System.out.println("User chose higher!");

//function to check if it is right if right go to next card

image = new ImageIcon(imageList[1]);

label = new JLabel("", image, JLabel.CENTER);

add( label, gbc );

}

if (Action.equals ("lower"))

{

System.out.println("User chose lower!");

//function to check if it is right if right go to next card

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值