Java - 文本字段数组不显示

标签 java arrays user-interface textfield

在我的 GUI 类中,我有一个文本字段数组,我在此类构造函数中对其进行了初始化。代码如下

JTextField[] arrayOne= new JTextField [10];
        for(int a = 0;a < 10;a++)
        {
            arrayOne[a] = new JTextField(String.valueOf(0));
            arrayOne[a].setBounds(189 + a * 50, 156, 39, 23);
            arrayOne[a].setVisible(true);

        }

问题是,当我运行 GUI 时,它不显示这些文本字段。为什么不呢?

最佳答案

您应该在布局中添加 JTextField

如果您在 for 内扩展 JFrame,则应该使用容器/或类的 add 方法。

阅读this .

Adding Components to a Container

When you add components to a panel or content pane, the arguments you specify to the add method depend on the layout manager that the panel or content pane is using. In fact, some layout managers do not even require you to add the component explicitly; for example, GroupLayout. For example, BorderLayout requires that you specify the area to which the component should be added (using one of the constants defined in BorderLayout) using code like this:

pane.add(aComponent, BorderLayout.PAGE_START); The how-to section for each layout manager has details on what, if any, arguments you need to specify to the add method. Some layout managers, such as GridBagLayout and SpringLayout, require elaborate setup procedures. Many layout managers, however, simply place components based on the order they were added to their container.

Swing containers other than JPanel and content panes generally provide API that you should use instead of the add method. For example, instead of adding a component directly to a scroll pane (or, actually, to its viewport), you either specify the component in the JScrollPane constructor or use setViewportView. Because of specialized API like this, you do not need to know which layout manager (if any) many Swing containers use. (For the curious: scroll panes happen to use a layout manager named ScrollPaneLayout.)

For information about how to add components to a specific container, see the how-to page for the container. You can find the component how-to pages using How to Use Various Components.

关于Java - 文本字段数组不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22595404/

相关文章:

java - 新 Maven 安装 : mvn -version java. lang.ClassNotFoundException

javascript - 如何将数组中的多组重复整数分组到自己的数组数组中?

ios - 如何判断用户有一段时间没有按下任何按钮

java - 方法不明确 - Java

java - 在按钮组中选择了哪个单选按钮

php - 列出php文件中所有定义的常量

javascript - 拆分字符串来初始化javascript数组有什么好处?

iPhone:在应用程序中同时使用 OpenGL 和 native 用户界面的最佳方式是什么?

user-interface - 需要改变的用户界面范例?

java - jax-ws/wsimport 生成带有 void 返回的 WebMethod - 相同的 Request/ResponseWrapper