English 中文(简体)
如何存储textFields中的信息?
原标题:How do I store information from the textFields?

现在,我只是在测试,以确保按钮正常工作。。。但这是我用Pascal三角展开二项式的applet代码!我有实际计算部分的方程式,我只需要知道如何存储JTextFields中的信息!谢谢

import java.applet.Applet;     
import java.awt.*;     
import java.awt.event.*;     
import java.applet.*;     
import javax.swing.*;   
import java.text.DecimalFormat;  
import java.util.ArrayList;  
import javax.swing.Action; 

public class BinomialExpander extends JApplet implements ActionListener 
{   
  JLabel welcome;    
  JLabel directions;  
  JLabel example;    

  JLabel instructions;    
  JLabel startOfBinomial;    
  JLabel plusSign;    
  JLabel forExponent;    
  JLabel endOfBinomial;    

  JTextField txtFirst;    
  JTextField firstVar;    
  JTextField txtSecond;    
  JTextField secondVar;    
  JTextField exp;    


  JLabel lblExpanded;     
  JLabel outputExpanded;      
  double degreesFahrenheit;     
  FlowLayout layout;    
  Timer timer;    

  Button compute;    

  private int[] pascal1 = {1,1};    
  private int[] pascal2 = {1,2,1};    
  private int[] pascal3 = {1,3,3,1};    
  private int[] pascal4 = {1,4,6,4,1};    
  private int[] pascal5 = {1,5,10,10,5,1};    
  private int[] pascal6 = {1,6,15,20,15,6,1};    
  private int[] pascal7 = {1,7,21,35,35,21,7,1};    
  private int[] pascal8 = {1,8,28,56,70,56,28,8,1};    
  private int[] pascal9 = {1,9,36,84,126,84,36,9,1};    
  private int[] pascal10 = {1,10,45,120,210,120,45,10,1};    

  public void init()     
  {    
      Container c = getContentPane();     
      c.setBackground(Color.cyan);    

      layout = new FlowLayout();    
      layout.setAlignment(FlowLayout.LEFT);    
      c.setLayout(layout);    
      setSize(500,175);    

      welcome = new JLabel("Welcome to the Binomial Expander Applet!");    
      directions = new JLabel("Enter binomial in the form:  (number)(variable) + (number)(variable)^exponent .");    
      example = new JLabel("Example: (4a + 2)^2.");    
      // instantiate JLabel object for Degrees Fahrenheit
      instructions = new JLabel("Enter the first number of your binomial(if there is a variable, add it into the second box):");
      // instantiate JTextField object for the degrees fahrenheit
      startOfBinomial = new JLabel(" (");
      txtFirst = new JTextField(4);
      // instantiate JLabel object for Degrees Celesius
      firstVar = new JTextField(4);
      plusSign = new JLabel(" + ");
      txtSecond = new JTextField(4);
      secondVar = new JTextField(4);
      endOfBinomial = new JLabel(")");
      forExponent = new JLabel("^");
      forExponent.setFont(new Font("Times New Roman", Font.BOLD, 9));
      exp = new JTextField(2);
      compute = new Button("Compute!");
      compute.addActionListener(this);
      lblExpanded = new JLabel("Your expanded binomial is: ");
      // JLabel to display the equivalent degrees Celsius
      outputExpanded = new JLabel("");
      c.add(welcome);
      c.add(directions);
      c.add(example);
      c.add(instructions);
      c.add(startOfBinomial);
      //CALL the addActionListener() method on the JTextField object 
      // for the degrees Fahrenheit
      txtFirst.addActionListener(this);
      // Add the textbox the celsius label and output label
      c.add(txtFirst);
      c.add(firstVar);
      c.add(plusSign);
      c.add(txtSecond);
      c.add(secondVar);
      c.add(endOfBinomial);
      c.add(forExponent);
      c.add(exp);
      c.add(compute);
      c.add(lblExpanded);
      c.add(outputExpanded);
     // timer = new Timer(1, this);
     // timer.start();
  }

  public void actionPerformed(ActionEvent event)     
  {    
      if (event.getSource() == compute)      
      {    
          outputExpanded.setText(expand());    
      }    
}
  public String expand()    
  {    
     String x = "callingComputing";    
     return x;    
  }    
} 
问题回答

yourTextField.getText() returns a String you can use as you would any other String, store it in an array, teach it to sing, fly and enjoy life. Conversely, you can use yourTextField.setText() to display stuff in your JLabel, textfield etc.

JTextField扩展JTextComponent,它具有处理文本操作的API。

正如其他答案所建议的那样,使用setText将当前文本替换为新文本。

同时阅读官方教程以了解如何使用文本字段

JTextField txtName = new JTextField();
JButton btn = new JButton();

在执行操作的方法中添加以下内容

String name = txtname.getText();

此语句返回您单击按钮前一秒在文本字段中输入的文本

您创建一个类来存储数据,并在Frame类中创建一个对象,即向数据类提供GUI的类,然后在框架的提交按钮的动作监听器中,将getText()返回的字符串分配给对象的字段

for eg: you want to take input name and age from the text field, create a Person class

public class Person{
public String name;
public int age;
}

现在创建一个GUI类

public PersonFrame extends JFrame{

public person;
public PersonFrame(){
person = new Person();
JTextField txtName = new JTextField();
JButton btn = new JButton();

btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
  person.name = txtName.getText();
  person.age = Integer.parseInt(txtAge.getText()); //as the textfield returns a String always
}
});
}

} and avoid using

  if (event.getSource() == compute)      
      {    
          outputExpanded.setText(expand());    
      } 

如果你有100个按钮,那么编写嵌套的If-else语句来检查哪个按钮生成了事件是愚蠢的!!

remember that swing is designed for providing the user interface and you need an object of a class for storing the data! for java tutorial visit here





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签