有4個類別:Main
,GUI
,Logic
和Calculator
。我很難讓Calculator課程正確,第一個操作/計算給了我錯誤的答案,但下一個是正確的。我試圖爲第一個和第二個數字創建另一個變量(fnum
和snum
),但它不起作用。我是否需要修改邏輯類?Java計算器GUI操作
主類
class Main
{
public static void main(String args[])
{
GUI gui = new GUI();
gui.display();
}
}
GUI類
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class GUI
{
private static final int H = 400; // Height of window pixels
private static final int W = 500; // Width of window pixels
private JLabel theMes = new JLabel(); // Message area
private JTextArea theOutput1 = new JTextArea(); // Input number area
private JTextArea theOutput2 = new JTextArea(); // Result area
private JScrollPane theSP = new JScrollPane();
private ButtonPress onButtonPress = new ButtonPress();
public void display()
{
JFrame rpc = new JFrame(); // Window
Container cp = rpc.getContentPane(); // Content Pane
Container rootWindow = (Container) rpc; // Root Window
cp.setLayout(null); // No layout manager
rootWindow.setSize(W, H); // Size of Window
String labels[] = {
"7", "8", "9", "", "CR", "+",
"4", "5", "6", "", "=", "-",
"1", "2", "3", "", "", "*",
"C", "0", "", "", "", "/" };
final int LABELS = labels.length; // # Button Labels
final int GAP = 15; // Horizontal Gap
final int HLAB = 20; // Label
final int SHLAB = GAP;
final int EHLAB = SHLAB+HLAB;
final int HTA1 = 30; // Output area 1
final int SHTA1 = EHLAB + GAP;
final int EHTA1 = SHTA1 + HTA1;
final int HTA2 = 100; // Output area 2
final int SHTA2 = EHTA1 + GAP;
final int EHTA2 = SHTA2 + HTA2;
final int HBUT = 170; // Buttons
final int SHBUT = EHTA2 + GAP;
final int BNR = 4, BNC = 6; // number rows cols of buttons
final int BW = W/BNC, BH = HBUT/BNR; // Size of landscape for button
final int SBH = SHBUT, SBW = 7; // Start position for buttons
JButton buttons[] = new JButton[LABELS];
Font font = new Font("Serif",Font.BOLD,20); // Button font
for (int i=0; i<LABELS; i++)
{
if (labels[i].length() >= 1)
{
buttons[i] = new JButton(labels[i]);
final int col = i%BNC * BW, row = i/BNC * BH;
buttons[i].setBounds(SBW+col, SBH+row, BW-20, BH-10);
buttons[i].addActionListener(onButtonPress);
buttons[i].setFont(font);
cp.add(buttons[i]);
}
}
font = new Font("Serif",Font.BOLD,14); // Font is
theMes.setBounds(10, SHLAB, W-25, HLAB); // Message area
theMes.setText(""); // Blank
theOutput1.setFont(font); // Uses font
cp.add(theMes); // Add to canvas
theOutput1.setBounds(10, SHTA1, W-25, HTA1); // Input Area
theOutput1.setText(""); // Blank
theOutput1.setFont(font); // Uses font
cp.add(theOutput1); // Add to canvas
font = new Font("Serif",Font.BOLD,14); // Font is
theSP.setBounds(10, SHTA2, W-25, HTA2); // Scrolling pane
theOutput2.setText(""); // Blank
theOutput2.setFont(font); // Uses font
cp.add(theSP); // Add to canvas
theSP.getViewport().add(theOutput2); // In TextArea
rootWindow.setVisible(true); // Make visible
theMes.setText("Calculator"); // Opening message
}
private Calculator calc = new Calculator();
private Logic logic = new Logic(calc);
class ButtonPress implements ActionListener // Listener
{
public void actionPerformed(ActionEvent ae) // Interaction
{
String label = ae.getActionCommand(); // Button label
String info = logic.process(label);
theOutput2.setText("");
if (info == null)
theOutput2.append("" + logic.getResult());
else
theOutput2.append(info);
theOutput1.setText("");
theOutput1.append("Number entered: " + logic.getNumber());
}
}
}
邏輯類
class Logic
{
private enum State { FIRST_NUMBER, SUBSEQUENT_NUMBER };
private State state = State.FIRST_NUMBER;
private long number = 0;
private char op = ' ';
private Calculator calc = null;
public Logic(Calculator calculator)
{
calc = calculator;
}
public String process(String button)
{
String info = null;
if (button.length() == 1)
{
char c = button.charAt(0);
if (c >= '0' && c <= '9') // Digit
{
number = number * 10 + c-'0'; // Build number
} else {
switch (c)
{
case 'C' : number = 0;
break;
case '=' :
case '+' : case '-' :
case '*' : case '/' :
switch (state)
{
case FIRST_NUMBER:
calc.setValue(number);
state = State.SUBSEQUENT_NUMBER;
break;
case SUBSEQUENT_NUMBER:
if (op != '=')
calc.evaluate(op, number);
break;
}
op = c; number = 0;
break;
}
}
} else {
if (button.equals("CR")) // Clear Result
{
calc.reset(); number = 0; state = State.FIRST_NUMBER;
}
}
return info;
}
public long getResult()
{
return calc.getValue();
}
public long getNumber()
{
return number;
}
}
計算器類
class Calculator
{
//Evaluate an arithmetic operation on the stored result
// E.g evaluate('+',9) would add 9 to the stored result
// evaluate('/',3) would divide the stored result
// by 3
// actions are '+'. '-', '*', '/'
//Note: if the operation is
// evaluate('/',0) the result returned should be 0
long value;
public void evaluate(char action, long number)
{
if (action == '+'){
value += Float.valueOf(number).floatValue();
}
else if (action == '-'){
value -= Float.valueOf(number).floatValue();
}
else if (action == '*'){
value *= Float.valueOf(number).floatValue();
}
else if (action == '/'){
value /= Float.valueOf(number).floatValue();
}
}
//Return the stored result
public long getValue()
{
return value;
}
//Set the stored result to number
public void setValue(long number)
{
}
//Reset the stored number to 0
public void reset()
{
if (value != 0) value = 0;
}
}
'公共靜態浮動的valueOf(String s)將拋出NumberFormatException' – vulkanino 2012-03-01 16:41:04
爲什麼你的setValue()方法沒有任何代碼。它應該有this.value =數字。 – JProgrammer 2012-03-01 16:42:56