Swing:如何获得活跃的JInternalFrame

时间:2017-03-14 19:18:11

标签: java swing jinternalframe jdesktoppane

我遇到活动JInternalFrame的问题。我需要指出一个活动的ImageInternalFrame(我的类,它扩展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());
    }
}

1 个答案:

答案 0 :(得分:2)

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

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

不要使用paintImmeditately。更改图标时,Swing将自动安排重新绘制标签。