我尝试将图片添加到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()方法吗?
感谢
答案 0 :(得分: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;
}
}