1

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);
        }
    }   

画像は読み込まれて表示されますが、ウィンドウのタイトル バー (名前、min、max、close を持つべきもの) が消えます。最小/最大/閉じるボタンは、マウスをあるべき場所に移動すると再び表示され、マウスをタイトル バーの上に移動すると、ウィンドウ全体をドラッグできます。

paint() メソッドの代わりに何か他のものを使用する必要がありますか?

ありがとう

4

1 に答える 1

1

に直接描画する正当な理由がない限り、 を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;
    }
}
于 2011-08-28T13:24:44.960 に答える