【发布时间】:2012-03-12 18:38:00
【问题描述】:
我已经为 JLabel 相对于 JPanel 设置了位置 (0,0)。但它出现在中心和顶部。我犯了什么错误?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Main extends JFrame
{
private JPanel panel;
private JLabel label1;
public Main()
{
panel = new JPanel();
panel.setBackground(Color.YELLOW);
ImageIcon icon1 = new ImageIcon("3.png");
label1 = new JLabel(icon1);
label1.setLocation(0,0);
panel.add(label1);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(panel);
this.setSize(500,500);
this.setVisible(true);
}
public static void main (String[] args) {
new Main();
}
}
【问题讨论】:
-
绝对没有办法学习所有关于 LayoutManagers 的知识,f.i.在docs.oracle.com/javase/tutorial/uiswing/layout/index.html
-
SSCCE +1。它使快速加入并帮助支持正确答案变得更加容易(带有屏幕截图;)。