-1

好的,我有一个正在上课的程序。我必须从文本字段中获取输入,然后将其放入以字符串为参数的重载构造函数中。我还必须与构造函数做一些其他的事情,所以在我的代码中你会看到一些超出这个问题的东西。另外,我没有发布我的方法,因为它是家庭作业的重要组成部分,而且这些方法都可以正常工作(在我尝试将信息传递给构造函数之前)。我想知道的是我有一种方法可以将文本字段信息传递给构造函数,因为我已经做了几个小时了,但很明显我不明白这一点。如果您只是想将我引导到某个我可以学习如何做到这一点的地方,我将不胜感激。

代码:

package lab4;

import java.awt.*;
import java.awt.List;
import javax.swing.*;
import java.awt.event.*;
import java.lang.Iterable.*;
import java.util.*;

public class Lab4 extends JFrame{

private JTextField jTextField1;
private JButton doSome;
private JLabel ansTxt;
private JLabel asciSum;
private JLabel jLabel1;
private JLabel jLabel3;
private JLabel jLabel4;
private JLabel jLabel5;
private JLabel jLabel6;
private JLabel lowCnt;
private JLabel numDigit;
private JLabel upCnt;
private JLabel vowelCnt;
private String userInput = jTextField1.getText();

public Lab4()
{        
    initComponents(); 
}
public Lab4(String x)
{
    this.userInput = x;
}
public Lab4(char[] x)
{

}
public Lab4(byte[] x)
{

}

private void initComponents() {

    jTextField1 = new JTextField();
    jLabel1 = new JLabel();
    doSome = new JButton();
    ansTxt = new JLabel();
    jLabel3 = new JLabel();
    jLabel4 = new JLabel();
    jLabel5 = new JLabel();
    jLabel6 = new JLabel();
    asciSum = new JLabel();
    vowelCnt = new JLabel();
    numDigit = new JLabel();
    upCnt = new JLabel();
    lowCnt = new JLabel();

    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setAutoRequestFocus(false);
    setFont(new java.awt.Font("Comic Sans MS", 1, 12)); // NOI18N

    jLabel1.setText("Enter a string Here and I'll show you some info about it!");

    doSome.setToolTipText("This Will Do Something");
    doSome.setInheritsPopupMenu(true);
    doSome.setLabel("Click Me When Your Done!");
    doSome.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            doSomeActionPerformed(evt);
        }
    });

    ansTxt.setText("ASCII SUM:");

    jLabel3.setText("Number of Vowels:");

    jLabel4.setText("Number of Digits:");

    jLabel5.setText("Number Of Uppercase:");

    jLabel6.setText("Number Of Lowercase:");

    GroupLayout layout = new GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                        .addComponent(ansTxt)
                        .addComponent(jLabel5, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel4, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel6, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(84, 84, 84)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addComponent(lowCnt)
                        .addComponent(vowelCnt)
                        .addComponent(asciSum)
                        .addComponent(numDigit)
                        .addComponent(upCnt)))
                .addGroup(layout.createSequentialGroup()
                    .addGap(95, 95, 95)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                        .addComponent(jTextField1)
                        .addComponent(jLabel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(doSome, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
            .addContainerGap(239, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(34, 34, 34)
            .addComponent(jLabel1)
            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jTextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(doSome)
            .addGap(34, 34, 34)
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(ansTxt)
                .addComponent(asciSum))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel3)
                .addComponent(vowelCnt))
            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel4)
                .addComponent(numDigit))
            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel5)
                .addComponent(upCnt))
            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel6)
                .addComponent(lowCnt))
            .addContainerGap(157, Short.MAX_VALUE))
    );

    pack();
}

private void doSomeActionPerformed(ActionEvent evt) 
{  
   String takeTfIn = this.jTextField1.getText(); 
    this.asciSum.setText(getAsciiSum(takeTfIn));
    this.vowelCnt.setText(getNumVowels(takeTfIn));
    this.numDigit.setText(getNumDigits(takeTfIn));
    this.upCnt.setText(getNumUpperCase(takeTfIn));
    this.lowCnt.setText(getNumLowerCase(takeTfIn));
}

 // Excluded methods go here...


public static void main(String[] args) 
{
    Lab4 test = new Lab4();
    test.setVisible(true);
}
}
4

1 回答 1

0

填写 tex 字段值并单击提交后,您需要创建 lab4 的新实例。点击提交,你可以这样做

   this.setVisible(false);//Hide current instance
   Lab4 test = new Lab4(this.jTextField1.getText()); // create new instance
   test.setVisible(true);  // set new instance visible

不要忘记添加以下构造函数:

 public Lab4 (String value) 
  {   
  this.jTextField1.setText(value); // if you like to set value back to textbox 
  }
于 2013-09-11T01:05:10.977 回答