JInternalFrame 图像显示;

发布于 2024-12-01 22:38:19 字数 489 浏览 1 评论 0原文

我尝试向 JInternalFrame 添加图像。我的paint()看起来像这样:

    public void paint (Graphics g) {
        //this should draw the loaded image
        if (bufferedImage != null) {
            g.drawImage(bufferedImage, getSize().width/2 - bufferedImage.getWidth()/2,
            getInsets().top+20, this);
        }
    }   

图像确实加载并显示,但窗口标题栏(应该有名称、最小值、最大值和关闭的标题栏)消失了。当我将鼠标移动到应有的位置时,最小/最大/关闭按钮会重新出现,当我将鼠标移动到标题栏上时,我可以拖动 整个窗户。

我应该使用其他方法来代替 Paint() 方法吗?

谢谢

I tried adding an image to JInternalFrame. my paint() looks like this:

    public void paint (Graphics g) {
        //this should draw the loaded image
        if (bufferedImage != null) {
            g.drawImage(bufferedImage, getSize().width/2 - bufferedImage.getWidth()/2,
            getInsets().top+20, this);
        }
    }   

The image does load and display, but the window title bar (the one that should have the name, min, max and close) disappears. The min/max/close buttons reappear when I move the mouse to where they should be, and when I move the mouse over the title bar I can drag
the whole window.

Should I be using something else instead of the paint() method?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欢你一世 2024-12-08 22:38:19

除非您有充分的理由直接绘制到 JInternalFrame 中,否则为什么不创建一个 new JLabel(new ImageIcon(bufferedimage)) 并仅使用 add() > 它到JInternalFrame

一个例子:

import java.awt.BorderLayout;
import java.awt.Image;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;

public class PictureDesktop extends JDesktopPane {

    public static void main(final String[] args) throws IOException {
        // some Wikipedia Commons Pictures of the Day
        final URL url1 = new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/5/5a/Hacienda_jaral_de_berrios.jpg/300px-Hacienda_jaral_de_berrios.jpg");
        final URL url2 = new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/c/cc/Magellanic_penguin%2C_Valdes_Peninsula%2C_e.jpg/300px-Magellanic_penguin%2C_Valdes_Peninsula%2C_e.jpg");
        final URL url3 = new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/b/b2/Marmolada_Sunset.jpg/300px-Marmolada_Sunset.jpg");

        final PictureDesktop desktop = new PictureDesktop();
        desktop.addPicture(ImageIO.read(url1));
        desktop.addPicture(ImageIO.read(url2));
        desktop.addPicture(ImageIO.read(url3));

        final JFrame frame = new JFrame("Pictures");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(BorderLayout.CENTER, desktop);
        frame.setSize(720, 480);
        frame.setVisible(true);
    }

    public void addPicture(final Image image) {
        add(createFrame(image));
    }

    private static int frames;

    private JInternalFrame createFrame(final Image image) {
        frames++;

        final JInternalFrame frame = new JInternalFrame("Picture " + frames);
        frame.add(BorderLayout.CENTER, new JLabel(new ImageIcon(image)));
        // without pack and setVisible, the frame isn't shown
        frame.pack();
        frame.setVisible(true);

        // cascade frames 
        frame.setLocation(40 * frames, 40 * frames);

        return frame;
    }
}

Unless you have good reason to directly draw into the JInternalFrame, why not create a new JLabel(new ImageIcon(bufferedimage)) and just add() it to the JInternalFrame?

An example:

import java.awt.BorderLayout;
import java.awt.Image;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;

public class PictureDesktop extends JDesktopPane {

    public static void main(final String[] args) throws IOException {
        // some Wikipedia Commons Pictures of the Day
        final URL url1 = new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/5/5a/Hacienda_jaral_de_berrios.jpg/300px-Hacienda_jaral_de_berrios.jpg");
        final URL url2 = new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/c/cc/Magellanic_penguin%2C_Valdes_Peninsula%2C_e.jpg/300px-Magellanic_penguin%2C_Valdes_Peninsula%2C_e.jpg");
        final URL url3 = new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/b/b2/Marmolada_Sunset.jpg/300px-Marmolada_Sunset.jpg");

        final PictureDesktop desktop = new PictureDesktop();
        desktop.addPicture(ImageIO.read(url1));
        desktop.addPicture(ImageIO.read(url2));
        desktop.addPicture(ImageIO.read(url3));

        final JFrame frame = new JFrame("Pictures");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(BorderLayout.CENTER, desktop);
        frame.setSize(720, 480);
        frame.setVisible(true);
    }

    public void addPicture(final Image image) {
        add(createFrame(image));
    }

    private static int frames;

    private JInternalFrame createFrame(final Image image) {
        frames++;

        final JInternalFrame frame = new JInternalFrame("Picture " + frames);
        frame.add(BorderLayout.CENTER, new JLabel(new ImageIcon(image)));
        // without pack and setVisible, the frame isn't shown
        frame.pack();
        frame.setVisible(true);

        // cascade frames 
        frame.setLocation(40 * frames, 40 * frames);

        return frame;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文