2017-03-14 31 views
0

我有问题获得活动JInternalFrame。我需要指出一个活动的ImageInternalFrame(我的类,这扩展了JInternalFrame),因为我会对它执行一些操作。我不知道如何解决它,有人可以帮助我吗?Swing:如何获得活动JInternalFrame

这里是我的代码:

public class MainFrame extends javax.swing.JFrame { 

    ArrayList <ImageInternalFrame> imageInternalFrameList = new ArrayList(); 


    private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {            

     JFileChooser chooser = new JFileChooser(); 
     chooser.showOpenDialog(null); 
     File f = chooser.getSelectedFile(); 
     String filePath = f.getPath(); 

     BufferedImage bufferedImage; 
     ImageInternalFrame imageInternalFrame; 

     String mimetype= new MimetypesFileTypeMap().getContentType(f); 
     String type = mimetype.split("/")[0]; 
     if(type.equals("image")){ 
      bufferedImage = null; 
      try { 
       bufferedImage = ImageIO.read(new File(filePath)); 
      } catch (IOException ex) { 
       Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex); 
      } 

      imageInternalFrame = new ImageInternalFrame(filePath); 

      imageInternalFrame.setSize(bufferedImage.getWidth(), bufferedImage.getHeight()); 
      imageInternalFrame.setVisible(true); 
      imageInternalFrame.setLocation(imageInternalFrameList.size() * 25 , imageInternalFrameList.size() * 25); 
      add(imageInternalFrame); 
      imageInternalFrameList.add(imageInternalFrame); 
     }   
     else{ 
      JOptionPane.showMessageDialog(null, "It's NOT an image"); 
     }} 

public class ImageInternalFrame extends javax.swing.JInternalFrame { 

    public ImageInternalFrame(String imagePath) { 
     initComponents(); 

     setImage(imagePath); 
    } 

    public void setImage(String imagePath){ 
     imageLabel.setIcon(new ImageIcon(imagePath)); 
     imageLabel.paintImmediately(imageLabel.getVisibleRect()); 
    } 
} 
+0

请使用代码格式设置代码和代码片段,结构化文档(如HTML/XML或输入/输出)。为此,请选择文本并单击邮件发布/编辑表单顶部的“{}”按钮。 –

回答

2

您可以使用JDesktop类的getSelectedFrame()方法。

imageLabel.setIcon(new ImageIcon(imagePath)); 
imageLabel.paintImmediately(imageLabel.getVisibleRect()); 

不要使用paintImmeditately。当您更改图标时,Swing会自动安排重新贴上标签。