1

今のところ、ボタンが機能することを確認するために をテストしているところですが、これはパスカルの三角形を使用して二項式を展開するアプレットのコードです! 私は実際に計算する部分の方程式を持っています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;    
  }    
} 
4

3 に答える 3

2

あなたのテキストフィールド。getText()は、他の文字列と同じように使用できる文字列を返し、それを配列に格納し、歌ったり、飛んだり、人生を楽しんだりするように教えることができます。逆に、yourTextField.setText() を使用して、JLabel やテキストフィールドなどに内容を表示できます。

于 2012-05-27T16:12:48.117 に答える
1

JTextFieldextendsJTextComponentには、テキスト操作を処理するための API があります。

他の回答で示唆されているように、 setText を使用して現在のテキストを新しいテキストに置き換えます。

また、公式チュートリアルを読んでテキスト フィールドの使用方法を理解してください。

于 2012-05-27T16:32:10.820 に答える
0
JTextField txtName = new JTextField();
JButton btn = new JButton();

アクション実行メソッドでこれを追加します

String name = txtname.getText();

このステートメントは、ボタンをクリックする2秒前にテキストフィールドに入力されたテキストを返します

データを格納するクラスを作成し、そのオブジェクトをFrameクラス、つまりデータクラスに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ステートメントを記述して、どのボタンがイベントを生成したかを確認するのはばかげています。

スイングはユーザーインターフェイスを提供するために設計されており、データを格納するためのクラスのオブジェクトが必要であることを忘れないでください!Javaチュートリアルについては、こちらをご覧ください

于 2012-05-27T17:10:00.523 に答える