我尝试向 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() 方法吗?
谢谢
最佳答案
除非您有充分的理由直接绘制到 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;
}
}
关于java - JInternalFrame 图像显示;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7221197/