Java - 更新JLabel的图标时,新图标放在旧图标后面
Java - When updating JLabel's icon, the new icon is put behind the old icon
我想做的是创建一个图形用户界面,允许您 select 图像、显示图像,然后为学校项目对其执行操作。这是启动时发生的情况。 Perfect! However, when I update the icon, it puts the new icon behind it:
这是我在 Jlabel 上使用的代码:
ImageIcon imageIcon = new ImageIcon();
try {
imageIcon = new ImageIcon(ImageIO.read(new File("/Users/ryanauger/Repos/JavaGUI/GUI/Images/cameraIcon.png")));
} catch (IOException e2) {
// TODO Auto-generated catch block
imageIcon = new ImageIcon();
e2.printStackTrace();
}
JLabel lblNewLabel = new MyJLabel(imageIcon);
lblNewLabel.setBounds(0, 6, 600, 600);
frame.getContentPane().add(lblNewLabel);
JButton btnNewButton = new JButton("Pick Image File");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser jfc;
jfc = new JFileChooser();
File f = new File(System.getProperty("user.dir"));
jfc.setCurrentDirectory(f);
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
jfc.showOpenDialog(btnNewButton);
File selFile = jfc.getSelectedFile();
try {
lblNewLabel.setIcon(new ImageIcon(ImageIO.read(new File(selFile.getAbsolutePath()))));
} catch (IOException e1) {
// TODO Auto-generated catch block
System.out.println(selFile.getAbsolutePath());
e1.printStackTrace();
}
}
});
我是 java 的新手,非常感谢您的帮助。谢谢!
编辑:这是 MyJLabel 的代码:
class MyJLabel extends JLabel
{
ImageIcon imageIcon;
public MyJLabel(ImageIcon icon)
{
super();
this.imageIcon = icon;
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(imageIcon.getImage(),0,0,getWidth(),getHeight(),this);
}
}
这就是你的问题,当你打电话给
JLabel lblNewLabel = new MyJLabel(imageIcon);
您将图标存储在您自己的位置,而不是将其传递给超级 class。所以当你画画时,它也会画画相机。然后当你打电话时:
lblNewLabel.setIcon(new ImageIcon(ImageIO.read(new File (selFile.getAbsolutePath()))));
您实际上是在调用 super class 的 setIcon() 方法,您并没有更改您在本地存储的图标。然后超级 class 绘制 phone,就像它应该的那样,但你也在它上面绘制相机。
我想做的是创建一个图形用户界面,允许您 select 图像、显示图像,然后为学校项目对其执行操作。这是启动时发生的情况。
这是我在 Jlabel 上使用的代码:
ImageIcon imageIcon = new ImageIcon();
try {
imageIcon = new ImageIcon(ImageIO.read(new File("/Users/ryanauger/Repos/JavaGUI/GUI/Images/cameraIcon.png")));
} catch (IOException e2) {
// TODO Auto-generated catch block
imageIcon = new ImageIcon();
e2.printStackTrace();
}
JLabel lblNewLabel = new MyJLabel(imageIcon);
lblNewLabel.setBounds(0, 6, 600, 600);
frame.getContentPane().add(lblNewLabel);
JButton btnNewButton = new JButton("Pick Image File");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser jfc;
jfc = new JFileChooser();
File f = new File(System.getProperty("user.dir"));
jfc.setCurrentDirectory(f);
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
jfc.showOpenDialog(btnNewButton);
File selFile = jfc.getSelectedFile();
try {
lblNewLabel.setIcon(new ImageIcon(ImageIO.read(new File(selFile.getAbsolutePath()))));
} catch (IOException e1) {
// TODO Auto-generated catch block
System.out.println(selFile.getAbsolutePath());
e1.printStackTrace();
}
}
});
我是 java 的新手,非常感谢您的帮助。谢谢!
编辑:这是 MyJLabel 的代码:
class MyJLabel extends JLabel
{
ImageIcon imageIcon;
public MyJLabel(ImageIcon icon)
{
super();
this.imageIcon = icon;
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(imageIcon.getImage(),0,0,getWidth(),getHeight(),this);
}
}
这就是你的问题,当你打电话给
JLabel lblNewLabel = new MyJLabel(imageIcon);
您将图标存储在您自己的位置,而不是将其传递给超级 class。所以当你画画时,它也会画画相机。然后当你打电话时:
lblNewLabel.setIcon(new ImageIcon(ImageIO.read(new File (selFile.getAbsolutePath()))));
您实际上是在调用 super class 的 setIcon() 方法,您并没有更改您在本地存储的图标。然后超级 class 绘制 phone,就像它应该的那样,但你也在它上面绘制相机。