下面是代碼,當圖像放到背景上時,無論我添加什麼代碼來移動它,它都會停留在左側牆上。我試過setLocation和setBounds。我想要做的就是將圖像移動到左下方,但不完全放在框架的牆上。如何移動卡住且不會移動的JLabel?
JFrame window = new JFrame();
window.setSize(800,480);
window.setTitle("Battle");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new BorderLayout());
JLabel background = new JLabel();
ImageIcon icon = new ImageIcon("background2.png");
background.setIcon(icon);
background.setLayout(new BorderLayout());
window.setContentPane(background);
JLabel p1 = new JLabel();
p1 = a.getImage();
background.add(p1);
p1.setLocation(500,500);
p1.setVisible(true);
window.setVisible(true);
BorderLayout這是做它的工作。您對setLocation的調用沒有任何意義,因爲BorderLayout像任何佈局管理器一樣,決定放置子組件的位置。您可能需要學習如何使用[GridBagLayout](http://docs.oracle.com/javase/8/docs/api/java/awt/GridBagLayout.html),但首先,您需要了解[如何LayoutManagers通常工作](https://docs.oracle.com/javase/tutorial/uiswing/layout/)。 – VGR