第一次选择图像时,效果很好。但当我尝试更改它时,它不起作用,第一个图像仍保留在屏幕上。
label = new JLabel("");
panel_1.add(label);
btnAddImage = new JButton("Select Image");
btnAddImage.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
File f = null ;
fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int value = fileChooser.showOpenDialog(fileChooser);
if (value == JFileChooser.APPROVE_OPTION)
{
f = fileChooser.getSelectedFile();
if (f.exists())
{
inputImage_textField.setText(f.getName());
BufferedImage bi = getMyBuffImage();
label = new JLabel(new ImageIcon(bi));
label.setBounds(0, 68, 98, 92);
panel_1.add(label);
panel_1.repaint();
}
}
}
});
我重新喷漆时是不是做错了什么,还是其他什么问题?
感谢
如果要替换现有标签,请替换
label = new JLabel(new ImageIcon(bi));
label.setBounds(0, 68, 98, 92);
panel_1.add(label);
panel_1.repaint();
带有
label.setIcon(new ImageIcon(bi));
label.setBounds(0, 68, 98, 92);
panel_1.revalidate();
或者,如果你想添加第二个标签,只需更换
label = new JLabel(new ImageIcon(bi));
label.setBounds(0, 68, 98, 92);
panel_1.add(label);
panel_1.repaint();
带有
JLabel newLabel = new JLabel(new ImageIcon(bi));
label.setBounds(0, 68, 98, 92);
panel_1.add(newLabel);
panel_1.revalidate();