日期:2014-05-20  浏览次数:20727 次

setLayout(null)问题。。。
为什么我把布局管理器给null了就不显示东西了
我上网查了下 他们说要setLocation, setSize 或setBounds 方法 最后一个兼有前两者的功能 所以我用了setBounds 结果还是空白 为什么啊?
 
Java code

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class ButtonFrame {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Frames frame = new Frames();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

}
class Frames extends JFrame {
    
    JTextField t;
    Random random;
    public Frames() {
        random = new Random();
        //JTextField
        t = new JTextField();
        t.setBounds(200, 200, 10, 1);
        t.setBackground(Color.BLACK);
        /*JButton
        JButton b1 = new JButton("Left");
        JButton b2 = new JButton("Center");
        JButton b3 = new JButton("Right");
        JButton b4 = new JButton("Left");
        JButton b5 = new JButton("Center");
        JButton b6 = new JButton("Right");
        b1.addActionListener(new Actt());
        //JPanel
        JPanel p = new JPanel();
        p.setLocation(240, 240);
        p.setVisible(true);
        p.setLayout(new GridLayout(3,2));
        p.setBackground(Color.YELLOW);
        p.add(b1);
        p.add(b2);
        p.add(b3);
        p.add(b4);
        p.add(b5);
        p.add(b6);*/
        //this
        setLayout(null);
        //getContentPane().add(p);
        getContentPane().add(t);
        setSize(300,300);
        setTitle("T-T");
        //setBackground(Color.BLACK);
    }
    class Actt implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            t.setColumns(random.nextInt(10)* 2);
            Frames.this.validate();
        }
    }
}




------解决方案--------------------
Java code

t.setBounds(200, 200, 10, 1);