繁体   English   中英

JTextField窗口未出现

[英]JTextField window does not appear

在面板上,我要添加一个文本字段和一个按钮。 我正在尝试更改文本字段的大小或位置,但是它没有改变。 查看示例图片

/**
 *
 * @author Tsiatas
 */
import javax.swing.*;


public class MyFrame extends JFrame {

    private JPanel panel;
    private JTextField textField;
    private JButton button;

    public MyFrame() {
        panel = new JPanel();
        textField = new JTextField();
        panel.add(textField);
        button = new JButton("press me");

        panel.add(button);
        this.setContentPane(panel);
        this.setVisible(true);
        this.setSize(800, 400);
        this.setTitle("My first test frame");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}

您缺少文本字段首选大小的显式设置。 以下代码将以某种方式工作:

import javax.swing.*;

public class MyFrame extends JFrame {

   private JPanel panel;
   private JTextField textField;
   private JButton button;

   public MyFrame() {
       panel = new JPanel();
       textField = new JTextField();
       textField.setPreferredSize(new Dimension(200, 20)); // This is the added line
       panel.add(textField);
       button = new JButton("press me");

       panel.add(button);
       this.setContentPane(panel);
       this.setVisible(true);
       this.setSize(800, 400);
       this.setTitle("My first test frame");
       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   }
}

要更改位置,您将需要在JPanel上使用布局。 每种布局都有其自己的特殊配置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM