我正在创建游戏,需要Swing帮助。我在JPanel上以JLabels的形式有一些永久图像。图片代码:
private void addPermanentImage(String path) {
JLabel imgLabel = new JLabel();
imgLabel.setIcon(new ImageIcon(getClass().getResource(path)));
add(imgLabel);
display.pack();
}
我要添加4张图像
addPermanentImage("/textures/background.png");
addPermanentImage("/textures/button_draw.png");
addPermanentImage("/textures/button_stand.png");
addPermanentImage("/textures/button_reset.png");
如果背景被忽略,则类似于this。我想对它们进行布局,以使绘图位于左和中间,立场在右和中间,重置在背景的中间和底部。我该怎么做呢?可以仅将它们放置在坐标上吗?我知道这是不好的做法,但如果可行,它就会奏效。旁注:我是秋千菜鸟。预先感谢。
您可能必须为JPanel使用某些布局:https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
或将布局设置为null,然后为元素设置绝对位置,如下所示:https://stackoverflow.com/a/56547877/10802597