Java 单击按钮将ImageIcon更改为其他图片
所以我想在每次按下按钮时替换JLabel中的ImageIcon。我这样做是为了使图像、标签和网格约束是公开的。当我试图改变它时,尽管什么都没有发生 我是走错方向了还是 谢谢Java 单击按钮将ImageIcon更改为其他图片,java,swing,jbutton,jlabel,imageicon,Java,Swing,Jbutton,Jlabel,Imageicon,所以我想在每次按下按钮时替换JLabel中的ImageIcon。我这样做是为了使图像、标签和网格约束是公开的。当我试图改变它时,尽管什么都没有发生 我是走错方向了还是 谢谢 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;
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<Card> 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<suits.length; a++)
{
for(int b = 0; b<values.length; 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
}
}
}
package hi.low;
导入java.awt.*;
导入java.awt.event.*;
导入java.util.ArrayList;
导入javax.swing.*;
导入java.util.Random;
导入java.util.ArrayList;
公共类Card_面板扩展JPanel实现ActionListener
{
专用最终整型宽度=400,高度=200;
私有静态字符串[]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”
};
私有静态int-imageNum=-1;
GridBagConstraints gbc=新的GridBagConstraints();
GridBagConstraints c=新的GridBagConstraints();
图像图标图像;
JLabel标签;
专用静态阵列列表甲板;
专用静态卡tempCard、currentCard;
公共卡面板()
{
deck=newarraylist();
char[]suits={'h','d','c','s'};
char[]值={'2','3','4','5','6','7','8','9','t','j','q','k','a'};
对于(int a=0;a而不是每次尝试创建和添加新标签,只需在标签本身上调用setIcon
更像是
image = new ImageIcon(imageList[1]);
label.setIcon(image);
查看更多详细信息
我还建议您先加载图像,这样您就不需要在每个actionPerformed
我还建议您使用ImageIcon
谢谢您的帮助!:)@user2122589 Like简单解决方案:D@MadProgrammer请问为什么(我也推荐ImageIO而不是ImageIcon),原因是什么,或者你能不能请shareImageIcon将acutal加载委托给一个后台线程,这意味着你实际上不知道什么时候加载图像(SwingAPI就是为了支持这一点而设计的),如果无法加载映像,它也不会引发任何类型的异常