2012-05-27 40 views
1

现在,我只是测试,以确保按钮的作品......但这是我的代码为一个小程序使用帕斯卡的三角扩展二项式!我有实际计算部分的公式,我只需要知道如何存储来自JTextFields的信息!谢谢!如何存储textField中的信息?

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;  
    }  
} 
+0

忽略所有的华氏海狗!这一切都在那里,因为我使用摄氏到华氏转换器作为我的小程序的模型!感谢您的帮助!我只是一个初学者。 :) – user1404557

回答

2

yourTextField。 getText()返回一个字符串,您可以像使用其他任何字符串一样使用该字符串,将其存储在数组中,教会它唱歌,飞翔并享受生活。 相反,你可以使用yourTextField.setText()在你的JLabel,文本框等

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

在执行的操作方法添加此

String name = txtname.getText(); 

这个语句返回其在文本字段中点击之前进入第二个文本按钮

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

为例如:你想利用输入姓名和年龄从文本字段, 创建一个Person类

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

} 并避免使用

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

如果你有100个按钮,编写嵌套的if-else语句来检查哪个按钮产生事件将是愚蠢的!

请记住,swing是为提供用户界面而设计的,并且您需要一个用于存储数据的类的对象! 对于java教程访问here