Java 显示文件选择器中的图像
我在显示从创建的文件选择器获取的图像时遇到问题。你能给我一些建议吗?图像被创建为缓冲图像 这是我的密码:Java 显示文件选择器中的图像,java,image,swing,Java,Image,Swing,我在显示从创建的文件选择器获取的图像时遇到问题。你能给我一些建议吗?图像被创建为缓冲图像 这是我的密码: public void actionPerformed(ActionEvent e) { if (e.getSource().getClass().getName().contains("JMenuItem")) { if (e.paramString().contains("Load")) { JFileChooser fc = n
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();
}
为什么要为显示图片的2D图形而烦恼呢 可以用于Swing组件
你到底有什么问题?你会犯什么错误?你为什么给drawImage打两次电话?你能发布完整的代码或SSCCE,或至少是stacktrace报告错误吗?我的gui上没有显示图像。我想将从文件选择器获得的图像显示到我的gui。您想查看我的gui的代码吗?这个问题也有类似的问题,仔细看吧!给自己至少一个小时,你就不会忘记如何在你的一生中做到这一点:显示代码看起来不像Swing。。。如果你是混合awt和swing-不要+1有一个很好的缩放示例。
public void paint(Graphics g){// paintComponent not paint
super.paintComponents(g); // paintComponent not paintComponents
.....
public void paintComponent(Graphics g){
super.paintComponent(g);
......