使用 Swing 的 BG 图像,无需重写 PaintComponent
我目前正在设计一个带有多个屏幕的菜单,每个屏幕上有多个按钮。要在 jLabel 中的背景图像顶部使用按钮(默认情况下,我不能将按钮放在 jLabel 的顶部),我使用了带有两个面板/菜单屏幕的 GridBagLayout,其中一个面板包含按钮(不透明 = false)和一个带有背景图像或 jLabel 的面板。为了根据用户在菜单中的位置切换当前显示的面板,我用单独的方法而不是类制作了每个菜单屏幕(也称为每 2 个面板)。
现在,我已经到了这样的地步:我正在处理不必要的复杂界面部分,并且我认为 GridBag 无法满足我的目的,所以我想知道是否有不同的方法来绘制我的背景图像,仍然能够使用图像顶部的按钮。
我查找的最流行的方法是重写 PaintComponent 方法,但我不能这样做,因为我已经在单独的方法中而不是类中创建了 JPanel。它们都包含在我原来的 JFrame 中。
帮助将不胜感激,谢谢!
刚刚添加了这段代码,但我的背景由于某种原因仍然是白色的?现在尝试其他建议,谢谢大家!
private void mainPanel() {
icon = new ImageIcon(getClass().getResource("/phantasma/menuv1.png"));
mainContainer1 = new javax.swing.JPanel() {
@Override
protected void paintComponent(Graphics g) {
g.drawImage(icon.getImage(), 0,0, null);
super.paintComponent(g);
}
};
I'm currently designing a menu with several screens with multiple buttons on each screen. To use buttons on top of the background image, which is in a jLabel (by default, I can't put buttons on TOP of the jLabel), I used GridBagLayout with two panels/menu screen, one panel containing the buttons (opaque = false) and one panel with the background image, or jLabel. In order to switch the current panels being displayed, depending on where the user is in the menu, I made each menu screen (aka. every 2 panels) in separate methods, not classes.
Now, I've come to the point where I'm working on parts of the interface that are unnecessarily complicated, and I don't feel GridBag will serve my purposes, so I was wondering if there was a different way to draw my background image, still being able to use my buttons on top of the image.
The most popular way I looked up was overriding the paintComponent method, but I can't do that, since I've made my JPanels in separate methods, not classes. They're all contained in my original JFrame.
Help would be greatly appreciated, thank you!
Just added this code, but my background remains white for some reason? Trying the other suggestion right now, thanks guys!
private void mainPanel() {
icon = new ImageIcon(getClass().getResource("/phantasma/menuv1.png"));
mainContainer1 = new javax.swing.JPanel() {
@Override
protected void paintComponent(Graphics g) {
g.drawImage(icon.getImage(), 0,0, null);
super.paintComponent(g);
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道为什么所有帖子都建议为此进行自定义绘制。仅当您需要自动缩放背景图像时,您才需要进行自定义绘画。
如果您希望按实际大小绘制图像,请使用 JLabel。
当然可以。只需为 JLabel 设置一个 LayoutManager,然后您就可以向其中添加任何组件,就像向面板添加组件一样。
I have no idea why all the postings suggest doing custom painting for this. You would only do custom painting if you need to automatically scale the background image.
If you want the image painted at its real size then use a JLabel.
Sure you can. Just set a LayoutManager for the JLabel and then you can add any component to it the same way you add components to a panel.
在我上面的评论中我指出:
作为我的意思的一个例子,您可以在您创建的任何 JPanel 中重写paintComponent,无论它是从独立类派生还是在方法中创建的。例如,
In my comment above I state:
As an example of what I mean, you can override paintComponent in any JPanel that you create whether it's derived from a stand-alone class or created within a method. For e.g.,