我在显示从我创建的文件选择器中获取的图像时遇到问题。你能给我一些建议吗?图像创建为缓冲图像。
这是我的代码:
public void actionPerformed(ActionEvent e)
{
if (e.getSource().getClass().getName().contains("JMenuItem"))
{
if (e.paramString().contains("Load")) {
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new File("."));
int retVal = fc.showOpenDialog(null);
if (retVal == 0)
{
File file = fc.getSelectedFile();
try {
image = ImageIO.read(file);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
以下是显示代码:
public void paint(Graphics g){
super.paintComponents(g);
g.drawImage(getIconImage(), 0, 0, control);
g.drawImage( image, 0, 0,null);
repaint();
}
答案 0 :(得分:4)
为什么要使用2D图形来处理显示图片,将Image/ImageIcon放到JLabel,例如关于JFileChooser + Image + paintCompoent(),
public void paint(Graphics g){// paintComponent not paint
super.paintComponents(g); // paintComponent not paintComponents
.....
可以用于Swing JComponent
s
public void paintComponent(Graphics g){
super.paintComponent(g);
......