简单的JFrame程序但看不到JTextfield [英] Simple JFrame program but can't see JTextfield

查看:152
本文介绍了简单的JFrame程序但看不到JTextfield的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自己学习Swing。
我正在玩玩具程序,要求用户输入他们的名字。我将JLabel和JTextfield放入JPanel,用户可以在其中输入名称并提交。然而,我的JTextfield被压扁并且看不见,我无法让它显示(我试过setSize无效)。

I'm trying to learn Swing on my own. I'm playing with a toy program that asks the user to input their name. I put a JLabel and JTextfield into a JPanel where the user can input their name and submit. However my JTextfield is squished up and invisible and I can't get it to show (I've tried "setSize" to no avail).

这是我的代码:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class NamePrompt extends JFrame{


    private static final long serialVersionUID = 1L;

    String name;

    public NamePrompt(){

        setLayout(new BorderLayout());

        JLabel enterYourName = new JLabel("Enter Your Name Here:");
        JTextField textBoxToEnterName = new JTextField();
        textBoxToEnterName.setSize(40, 10);
        JPanel panelTop = new JPanel();
        panelTop.add(enterYourName);
        panelTop.add(textBoxToEnterName);

        JButton submit = new JButton("Submit");
        submit.addActionListener(new SubmitButton());
        JPanel panelBottom = new JPanel();
        panelBottom.add(submit);

        //Add panelTop to JFrame
        add(panelTop, BorderLayout.NORTH);
        add(panelBottom, BorderLayout.SOUTH);

        //JFrame set-up
        setTitle("Name Prompt Program");
        setSize(300, 150);
        setLocationRelativeTo(null);


    }



    public static void main(String[] args) {
        NamePrompt promptForName = new NamePrompt();
        promptForName.setVisible(true);


    }


推荐答案

JTextField textBoxToEnterName = new JTextField(20);

参见 新的JTextField(列) 以供说明。 EG

See new JTextField(columns) for explanation. E.G.

import java.awt.BorderLayout;
import javax.swing.*;

public class NamePrompt extends JFrame{

    private static final long serialVersionUID = 1L;

    String name;

    public NamePrompt(){

        setLayout(new BorderLayout());

        JLabel enterYourName = new JLabel("Enter Your Name Here:");
        JTextField textBoxToEnterName = new JTextField(21);
        //textBoxToEnterName.setSize(40, 10);
        JPanel panelTop = new JPanel();
        panelTop.add(enterYourName);
        panelTop.add(textBoxToEnterName);

        JButton submit = new JButton("Submit");
        //submit.addActionListener(new SubmitButton());
        JPanel panelBottom = new JPanel();
        panelBottom.add(submit);

        //Add panelTop to JFrame
        add(panelTop, BorderLayout.NORTH);
        add(panelBottom, BorderLayout.SOUTH);

        //JFrame set-up
        setTitle("Name Prompt Program");
        //setSize(300, 150);
        pack();
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        NamePrompt promptForName = new NamePrompt();
        promptForName.setVisible(true);
    }
}

这篇关于简单的JFrame程序但看不到JTextfield的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆