我在尝试更改JLabel
的图标时出现问题。我在eclipse中使用windowbuilder插件来制作这个程序,所以只要忽略标签上所有错误的命名选择,所有这些都将在以后更改。
我遇到的错误是在我的代码底部,我尝试更改label_1
的图标,我收到以下消息。
jlabel类型中的方法seticon(icon)不适用于 arguments(string)
有谁知道这可能是什么?
这是我的代码。对不起,这是相当长的,我认为这是你的窗口构建器:
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.Color;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Game {
public Timer bomb1;
public JLabel label;
public JLabel label_1;
public JLabel lblScore;
public JButton btnQuit;
public JLabel label_2;
public JLabel label_3;
public JLabel label_4;
public JLabel label_5;
public JLabel label_6;
public JLabel label_7;
public JLabel label_8;
public JLabel label_9;
public JLabel label_10;
public JLabel label_11;
public JLabel label_12;
public JLabel label_13;
public JLabel label_14;
public JLabel label_15;
public JLabel label_16;
public JLabel lblDodgeTheBombs;
public JFrame frame;
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Game window = new Game();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Game() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 489, 512);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
frame.setContentPane(contentPane);
contentPane.setLayout(null);
label_1 = new JLabel("");
label_1.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_1.setBackground(Color.WHITE);
label_1.setBounds(59, 44, 80, 80);
contentPane.add(label_1);
label = new JLabel("0");
label.setFont(new Font("Tahoma", Font.PLAIN, 14));
label.setBounds(71, 12, 46, 14);
contentPane.add(label);
lblScore = new JLabel("Score:");
lblScore.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblScore.setBounds(28, 11, 45, 17);
contentPane.add(lblScore);
btnQuit = new JButton("Quit");
btnQuit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
}
});
btnQuit.setBounds(359, 10, 89, 23);
contentPane.add(btnQuit);
label_2 = new JLabel("");
label_2.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_2.setBackground(Color.WHITE);
label_2.setBounds(149, 44, 80, 80);
contentPane.add(label_2);
label_3 = new JLabel("");
label_3.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_3.setBackground(Color.WHITE);
label_3.setBounds(239, 44, 80, 80);
contentPane.add(label_3);
label_4 = new JLabel("");
label_4.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_4.setBackground(Color.WHITE);
label_4.setBounds(329, 44, 80, 80);
contentPane.add(label_4);
label_5 = new JLabel("");
label_5.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_5.setBackground(Color.WHITE);
label_5.setBounds(59, 138, 80, 80);
contentPane.add(label_5);
label_6 = new JLabel("");
label_6.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_6.setBackground(Color.WHITE);
label_6.setBounds(149, 135, 80, 80);
contentPane.add(label_6);
label_7 = new JLabel("");
label_7.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_7.setBackground(Color.WHITE);
label_7.setBounds(239, 135, 80, 80);
contentPane.add(label_7);
label_8 = new JLabel("");
label_8.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_8.setBackground(Color.WHITE);
label_8.setBounds(329, 135, 80, 80);
contentPane.add(label_8);
label_9 = new JLabel("");
label_9.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_9.setBackground(Color.WHITE);
label_9.setBounds(329, 320, 80, 80);
contentPane.add(label_9);
label_10 = new JLabel("");
label_10.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_10.setBackground(Color.WHITE);
label_10.setBounds(59, 229, 80, 80);
contentPane.add(label_10);
label_11 = new JLabel("");
label_11.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_11.setBackground(Color.WHITE);
label_11.setBounds(149, 229, 80, 80);
contentPane.add(label_11);
label_12 = new JLabel("");
label_12.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_12.setBackground(Color.WHITE);
label_12.setBounds(239, 229, 80, 80);
contentPane.add(label_12);
label_13 = new JLabel("");
label_13.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_13.setBackground(Color.WHITE);
label_13.setBounds(329, 229, 80, 80);
contentPane.add(label_13);
label_14 = new JLabel("");
label_14.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_14.setBackground(Color.WHITE);
label_14.setBounds(239, 320, 80, 80);
contentPane.add(label_14);
label_15 = new JLabel();
label_15.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_15.setBackground(Color.WHITE);
label_15.setBounds(149, 320, 80, 80);
contentPane.add(label_15);
label_16 = new JLabel("");
label_16.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_16.setBackground(Color.WHITE);
label_16.setBounds(59, 323, 80, 80);
contentPane.add(label_16);
lblDodgeTheBombs = new JLabel("Dodge the bombs!");
lblDodgeTheBombs.setForeground(Color.RED);
lblDodgeTheBombs.setFont(new Font("Tahoma", Font.PLAIN, 25));
lblDodgeTheBombs.setBounds(137, 418, 217, 33);
contentPane.add(lblDodgeTheBombs);
bomb1 = new Timer(1000, new TimerListener());
bomb1.start();
}
private class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
label_1.setIcon("C:\\Users\\Josh\\Desktop\\blank.png");
}
}
}
答案 0 :(得分:4)
jlabel类型中的方法seticon(icon)不适用于 arguments(string)
是的,因为根据找到的文档here,setIcon()接受一个Icon作为参数而不是String。所以,你的代码应该是......
label_1.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\blank.png"));
我建议您将来使用文档。这真是一个非常好的做法。
答案 1 :(得分:0)
我认为错误信息对于出了什么问题是不言自明的。 “setIcon()”方法需要Icon类的实例而不是字符串实例。
jLabelObject.setIcon("string") // Do not pass image path