Fraction Class サンプル
計算式テキストフィールドに四則演算(カッコ使用可)の式を入れて計算ボタンを押すと 結果テキストフィールドに計算の結果を有理数で表示します。
サンプルソースを見る
import java.applet.Applet; import java.awt.*; import java.awt.event.*; /*
*/ public class FractionSample extends Applet implements ActionListener { Label inLabel = new Label("計算式:",Label.RIGHT); Label outLabel = new Label(" 結果:",Label.RIGHT); TextField inText = new TextField(40); TextField outText = new TextField(40); Button bt = new Button("計算"); public void init(){ add(inLabel); add(inText); add(outLabel); add(outText); bt.addActionListener(this); add(bt); } public void actionPerformed(ActionEvent e){ Fraction f=null; try{ f = Fraction.compute(inText.getText()); } catch(FractionException fe){ System.err.println(fe); } outText.setText(f.toString()); } }