gpt4 book ai didi

java - 有没有一种方法可以覆盖 JLabel 的内容

转载 作者:行者123 更新时间:2023-11-30 04:03:12 26 4
gpt4 key购买 nike

我想知道是否有办法用新文本覆盖放入 JLabel 中的文本。

我目前的代码每次都会创建新标签,但不会替换以前创建的标签。

关于我的代码,我想根据按下的按钮更改每个字符串数组中 JLabel 中保存的文本。

已上传我的操作监听器的代码。

@Override
public void actionPerformed(ActionEvent event) {
ImageIcon gws = new ImageIcon("src\\myGui\\sharke.jpg");
ImageIcon gib = new ImageIcon("src\\myGui\\gibbone.jpg");
ImageIcon croc = new ImageIcon("src\\myGui\\croce.jpg");
ImageIcon cat = new ImageIcon("src\\myGui\\cheetahe.jpg");
String gw[] = {"Shark", "fast", "water", "sea", "a", "b"};
String gbn[] = {"Gibbon", "climb", "tree", "grass", "a", "b"};
String crc[] = {"Croc", "scale", "teeth", "river", "a", "b"};
String cht[] = {"Cheetah", "run", "fur", "sand", "a", "b"};
for (int i = 0; i < field1.length; i++) {
field1[i] = new JTextField(i);
panel6.add(field1[i]);
}

if (event.getSource() == quit) {
System.exit(0);
} else if (event.getSource() == reset) {
pic1.setIcon(null);
pic1.setBorder(null);
} else if (event.getActionCommand().equalsIgnoreCase("shark")) {
pic1.setIcon(gws);
pic1.setBorder(BorderFactory.createEtchedBorder(Color.black, Color.blue));
for (int i = 0; i < field1.length; i++) {
field1[i] = new JTextField(gw[i]);
panel6.add(field1[i]);
}
} else if (event.getActionCommand().equalsIgnoreCase("gibbon")) {
pic1.setIcon(gib);
pic1.setBorder(BorderFactory.createEtchedBorder(Color.black, Color.orange));

} else if (event.getActionCommand().equalsIgnoreCase("crocodile")) {
pic1.setIcon(croc);
pic1.setBorder(BorderFactory.createEtchedBorder(Color.black, Color.green));
} else if (event.getActionCommand().equalsIgnoreCase("cheetah")) {
pic1.setIcon(cat);
pic1.setBorder(BorderFactory.createEtchedBorder(Color.black, Color.yellow));
}
}

最佳答案

JLabel.setText()就是您要找的东西

关于java - 有没有一种方法可以覆盖 JLabel 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21466619/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com