【问题标题】:How to set an array in Jtextfield using netbeans如何使用 netbeans 在 Jtextfield 中设置数组
【发布时间】:2016-02-02 09:13:40
【问题描述】:

我正在创建一个小型应用程序。在这个应用程序中,我有 5 个单词。我有一个 JtextField 和 JButton。JtextField 的名称设置为 name1,JButton 名称设置为 next。

我想从字符串数组的第一个单词中设置 name1 中的默认单词(我给出的这个代码中的一个例子是需要在 JtextField 中看到 "me" em>) 当应用程序运行时,我想在单击下一个按钮之前查看字符串数组中的单词。如何将数据设置为 name1?

代码如下:

String s[]={"me","and","my","friends","are"};
    int i=0; 
    private void nextActionPerformed(java.awt.event.ActionEvent evt) { 
        if(i>=s.length)
       i=0;
      name1.setText(s[i]);
      i++;
    }   

【问题讨论】:

    标签: java netbeans


    【解决方案1】:

    在创建字段后调用该方法,并使用 null 作为参数,因为您不使用该参数。

    nextActionPerformed(null); 
    

    这将模拟程序启动时按下按钮。

    【讨论】:

      【解决方案2】:

      假设您有一个附加了动作监听器的按钮。如果不是,那么它应该如下所示:

      nextButton.addActionListener(new ActionListener()
      {
      public void actionPerformed(ActionEvent e)
      {
       nextActionPerformed(e);
       // Or move your code here from nextActionPerformed(e)
      }
      });
      //And you can also initialize your text filed with first value of array:
      textField.setText(s[0]); 
      

      【讨论】:

        猜你喜欢
        • 2016-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-26
        相关资源
        最近更新 更多