【发布时间】:2012-02-23 09:09:57
【问题描述】:
我正在设计一个在 JFrame 中包含两个 JPanel 的程序,一个用于保存图像,另一个用于保存 GUI 组件(搜索字段等)。我想知道如何将图像绘制到 JFrame 中的第一个 JPanel?
这是我的构造函数的示例代码:
public UITester() {
this.setTitle("Airplane");
Container container = getContentPane();
container.setLayout(new FlowLayout());
searchText = new JLabel("Enter Search Text Here");
container.add(searchText);
imagepanel = new JPanel(new FlowLayout());
imagepanel.paintComponents(null);
//other constructor code
}
public void paintComponent(Graphics g){
super.paintComponents(g);
g.drawImage(img[0], -50, 100, null);
}
我试图重写 JPanel 的 paintComponent 方法来绘制图像,但是当我尝试编写时,这会导致我的构造函数出现问题:
imagepanel.paintComponents(null);
因为它只允许我传递 null 方法,而不是 Graphics g,所以有人知道此方法的修复方法或我可以用来在 JPanel 中绘制图像的其他方法吗?帮助表示赞赏! :)
一切顺利,提前感谢! 马特
【问题讨论】:
-
从你的构造函数调用paintComponents的想法是什么?。只需将其删除。
-
顺便说一句 - 你为什么不画图像最左边的 50 个像素?
标签: java image swing jframe jpanel