我想在按下按钮一次时在标签上设置图标,如果按了两次就会删除我已经使用label.setIcon(null);
,但它对我不起作用。
public void actionPerformed(ActionEvent e) {
if (!"submit".equals(e.getActionCommand()))
{
JButton button = (JButton) e.getSource();
int X = button.getLocation().x;
int Y = button.getLocation().y;
JLabel tick=new JLabel();add(tick);
tick.setBounds(X+400,Y+15,50,50);
if(arr.contains(e.getActionCommand()))
{
tick.setIcon(null);
arr.remove(e.getActionCommand());
}
else
{
image=new ImageIcon(imageList[0]);
tick.setIcon(image);
arr.add(e.getActionCommand());
}
答案 0 :(得分:2)
不要重新创建JLabel,也不要在每次点击时添加它
JLabel tick=new JLabel();
add(tick);
创建类'字段,然后创建一次标签。如果它只是tick.setIcon(null)
初始化。