Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Swing文本字段可见性问题_Java_Swing_Jtextfield_Layout Manager_Null Layout Manager - Fatal编程技术网

Java Swing文本字段可见性问题

Java Swing文本字段可见性问题,java,swing,jtextfield,layout-manager,null-layout-manager,Java,Swing,Jtextfield,Layout Manager,Null Layout Manager,我试图制作一个框架,并在其中添加一个文本字段。 为此,我使用了JTextField。但它没有出现 代码 正如你所说的,使用适当的方法,例如: 不要使用空布局,请参阅和 如果不想使用布局管理器,则需要使用JTextField的setBounds(x,y,width,height)方法设置其边界,其中x和y是文本字段在JFrame中的位置: tf.setBounds(100 , 100 , 100 , 20 ); 首先将布局设置为框架,然后向其添加元素和组件,如完整代码中所示: import ja

我试图制作一个框架,并在其中添加一个文本字段。 为此,我使用了
JTextField
。但它没有出现

代码
正如你所说的,使用适当的方法,例如:

不要使用空布局,请参阅和


如果不想使用布局管理器,则需要使用
JTextField
setBounds(x,y,width,height)
方法设置其边界,其中
x
y
是文本字段在
JFrame
中的位置:

tf.setBounds(100 , 100 , 100 , 20 );
首先将布局设置为框架,然后向其添加元素和组件,如完整代码中所示:

import javax.swing.*;

class Tst
{
    public Tst()
    {
        JTextField tf = new JTextField(10);
        tf.setBounds(100 , 100 , 100 , 20 );

        JFrame f = new JFrame();

        f.setSize(400, 400);
        f.setLayout(null);
        f.setVisible(true);
        f.add(tf);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String s[])
    {
        new Tst();
    }
}

忘记空布局,改用。但如果这是一项要求,您还必须设置textfield的位置和大小。(您可以使用
setBounds(x,y,width,height
)谢谢,setBounds()有效。但我见过一些代码,其中添加了textfield而没有使用setBounds()。这就是为什么我没有使用setBounds()。是的,有了适当的布局管理器,就没有必要调用
setBounds()
,但我认为对于空布局来说确实如此(或
setLocation()
setSize()
)。
tf.setBounds(100 , 100 , 100 , 20 );
import javax.swing.*;

class Tst
{
    public Tst()
    {
        JTextField tf = new JTextField(10);
        tf.setBounds(100 , 100 , 100 , 20 );

        JFrame f = new JFrame();

        f.setSize(400, 400);
        f.setLayout(null);
        f.setVisible(true);
        f.add(tf);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String s[])
    {
        new Tst();
    }
}