繁体   English   中英

JAVA-将图像添加到JPanel

[英]JAVA - Add an image to JPanel

我是JAVA的新手。 我想在我的窗口(JPanel)中获取图片以进行处理(例如添加圆圈)。 我创建了一个菜单,当我单击“文件>导入”时,出现打开的对话框以选择我的图像。 我得到了图像文件的正确路径(感谢System.out.println(FC.getSelectedFile()。toString());检查),但是图像没有出现...

这是代码:

        //On crée les listeners pour le menu "Fichier" :
        this.importer.addActionListener(new ActionListener()
        {
              public void actionPerformed(ActionEvent event)
              {
                  //On ouvre la boîte de dialogue pour charger le dessin :
                  JFileChooser FC = new JFileChooser();
                  FC.showOpenDialog(null);
                  BufferedImage myPicture=null;
                try {
                    myPicture = ImageIO.read(FC.getSelectedFile());
                    System.out.println(FC.getSelectedFile().toString());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                  contenant.add(new JLabel(new ImageIcon(myPicture)), BorderLayout.CENTER);
                  contenant.repaint();
              }
          });

谢谢你的支持。

替换为:

contenant.repaint();

有了这个:

contenant.revalidate();

当您添加JLabel时,它不会自动强制内容窗格对布局进行排序,因此即使JLabel位于其中,其大小也尚未确定。 调用revalidate()可解决此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM