[英]Drawing shapes over a background image JFrame
I am trying to draw shapes over a background image in a JFrame. 我正在尝试在JFrame中的背景图像上绘制形状。 I can get the image I want as the background, but I can't get the shapes to be drawn on top of that image. 我可以获取想要的图像作为背景,但是无法在该图像的顶部绘制形状。 What I've tried so far is just rearranging the order that the components are called, but that doesn't seem to have had an effect. 到目前为止,我尝试的只是重新排列组件的调用顺序,但这似乎没有效果。 This is where I did it: 这是我做的事:
JFrame frame = new JFrame ();
frame.setTitle("Happy Holidays!");
frame.setSize(813,645);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setContentPane(new JLabel(new ImageIcon("C:\\Users\\Brian\\Desktop\\Eclipse Projects\\BrianBolnickFinal\\bin\\christmas.jpeg")));
frame.add(holiday);
Where the content pane contains the image and holiday
is where my shapes are being drawn. 内容窗格包含图像和holiday
的位置是绘制我的形状的地方。 Is it something with not being able to draw on a content pane? 是否无法在内容窗格上绘制? Or is there something else going wrong in some other place? 还是在其他地方出了什么问题? my holiday
class works fine without the image, so I know that is at least working. 没有图像,我的holiday
课程效果很好,所以我知道至少可以。 Any suggestions? 有什么建议么?
First of all, holiday
should be transparent if you hope to be able to draw over other components 首先,如果您希望能够利用其他要素,则holiday
应该透明
holiday.setOpaque(false);
Next, you have two options. 接下来,您有两个选择。
First, you could add holiday to the label... 首先,您可以在标签上添加假期...
JLabel label = new JLabel(new ImageIcon("C:\\Users\\Brian\\Desktop\\Eclipse Projects\\BrianBolnickFinal\\bin\\christmas.jpeg")));
label.setLayout(new BorderLayout());
label.add(holiday);
Or you could use OverlayLayout layout manager 或者您可以使用OverlayLayout布局管理器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.