现在,我有以下代码将JLabel添加到面板的顶部中心,我认为这是默认的

imageLabel = new JLabel();
        ImageIcon customer1 = new ImageIcon("src/view/images/crab.png");

        imageLabel.setIcon(customer1);
        storePanel.add(imageLabel);
        imageLabel.setBounds(20, 20, 50, 50);


setBounds显然不是将其设置为20,20 ....那么,如何在面板中将某物定位到某个点?

最佳答案

使用适当的LayoutManager将组件放置在面板中。

http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

在您的情况下,您应该能够使用FlowLayout并在创建它时设置水平和垂直间隙。

http://docs.oracle.com/javase/7/docs/api/java/awt/FlowLayout.html#FlowLayout(int,%20int,%20int)

10-02 02:49
查看更多