将文本字段值传递给构造函数

时间:2013-09-11 00:52:54

标签: java constructor

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

代码:

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);
}
}

1 个答案:

答案 0 :(得分: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 
  }
相关问题