解决方案 »

  1.   


    import java.awt.Color;import javax.swing.*;
    public class Test extends JFrame{ /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Test f=new Test();
    f.setSize(500,600);
    f.setLocationRelativeTo(null);
    f.setTitle("show label");
    f.setVisible(true);
    f.setDefaultCloseOperation(EXIT_ON_CLOSE);
    JLabel l=new JLabel("hello");

                    f.setLayout(null);// 使默认布局无效
    l.setLayout(null);// 使默认布局无效

    l.setBorder(BorderFactory.createLineBorder(Color.RED)); //可视化label控件的边框红色 l.setSize(100,100);
    l.setLocation(0, 0); f.add(l);
    f.repaint();
    }}
    主要修改:
    f.setLayout(null);
    l.setLayout(null);
    //没有这两处null 后面的布局应该无法起到作用
    l.setBorder(BorderFactory.createLineBorder(Color.RED));
    //把label的边框画出来了,方便看,颜色是红色下面是效果图:
    我猜label控件中的文本其实是靠左居中的,setLocation是把控件的坐标提到了左上(0,0),还有20*20太小了,字都显示不全,我增加了一点数值,setSize(100,100)