错误部分都基本在下部了 基本都是不兼容类型 也都是在附值 变量之间传值 
package ouso_test1.jp.co.nnsinc; import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; public class Main extends JFrame implements ActionListener 

private boolean dotExist, operated, equaled; // 演算時用 
private double storedNumber; // 今の結果 
private int a , b; //aは一回目入力数字 bは二回目以降入力内容 
private int s , t ;// 計算領域 
private char lastOperator; // 前の演算子 
private JTextField operation; //text 
private JButton dot, plus, minus, multi, div,  equal, clear; // 演算子 
private JButton[] numbers; // 数字 // はじめ 
public Main() 

setTitle("Calculator"); 
// 初始化変数 
dotExist = false; // 小数点 
operated = false; // if btn 
equaled = false; //  if "=" 
storedNumber = 0; 
lastOperator = '?'; 
// 初始化窗口?量 
operation = new JTextField("0"); 
operation.setEditable(false); 
[] numbers= new JButton[10]; 
for (int i = 0; i < 10; i++) 
    numbers[i] = new JButton("" + i); 
dot = new JButton("."); 
plus = new JButton("+"); 
minus = new JButton("-"); 
multi = new JButton("*"); 
div = new JButton("/"); 
equal = new JButton("="); 
clear = new JButton("AC"); 
// 画面 
GridBagLayout layout = new GridBagLayout(); 
getContentPane().setLayout(layout); 
addComponent(layout, operation, 0, 0, 4, 1); 
addComponent(layout, numbers[1], 1, 0, 1, 1); 
addComponent(layout, numbers[2], 1, 1, 1, 1); 
addComponent(layout, numbers[3], 1, 2, 1, 1); 
addComponent(layout, numbers[4], 2, 0, 1, 1); 
addComponent(layout, numbers[5], 2, 1, 1, 1); 
addComponent(layout, numbers[6], 2, 2, 1, 1); 
addComponent(layout, numbers[7], 3, 0, 1, 1); 
addComponent(layout, numbers[8], 3, 1, 1, 1); 
addComponent(layout, numbers[9], 3, 2, 1, 1); 
addComponent(layout, dot, 4, 0, 1, 1); 
addComponent(layout, numbers[0], 4, 1, 1, 1); 
addComponent(layout, plus, 1, 3, 1, 1); 
addComponent(layout, minus, 2, 3, 1, 1); 
addComponent(layout, multi, 3, 3, 1, 1); 
addComponent(layout, div, 4, 3, 1, 1); 
addComponent(layout, equal, 5, 0, 2, 1); 
addComponent(layout, clear, 5, 3, 1, 1); 
} // バーテン メソッド 
public void actionPerformed(ActionEvent e,int i) 

JButton btn = (JButton)e.getSource(); 
        int S; 
        int T; 
        int storedNumbe; 
if (btn == clear) 

operation.setText("0"); 
dotExist = false; 
storedNumber = 0; 
lastOperator = '?'; 
a = 0; 
b = 0; 
S = 0; 
T = 0; 

else if (btn == numbers[i]) 

if (lastOperator = '?') 

a =        (int) (a + storedNumber); 
storedNumbe = a; 

else if () 

b =        (int) (b + storedNumber); 
storedNumbe = a; 


else if ( btn == plus|btn == plus| btn == plus | btn == plus) 

    if (lastOperator= "?") 
if (btn == plus) 

operate('+'); 
equaled = false; 

else if (btn == minus) 

operate('-'); 
equaled = false; 

else if (btn == multi) 

operate('*'); 
equaled = false; 

else if (btn == div) 

operate('/'); 
equaled = false; 

else if( lastOperator = "?") 

s = a; 
a = 0; 
lastOperator = " btn "; 


else if ( btn == equal ) 

if (lastOperator == " + ") 

operate('+'); 

else if (lastOperator == minus) 

operate('-'); 

else if (lastOperator == multi) 

operate('*'); 

else if (lastOperator == div) 

operate('/'); 
} } 
else 

if (equaled) 
storedNumber = 0; 
for (int i = 0; i < 10; i++) 
if (btn == numbers[i]) 

if (operation.getText().equals("0")) 
operation.setText("" + i); 
else if(! operated) 
operation.setText(operation.getText() + i); 
else 

operation.setText("" + i); 
operated = false; 


if (btn == dot && ! dotExist) 

operation.setText(operation.getText() + "."); 
dotExist = true; 



// 演算 
private void operate(char operator) 

double t = Double.valueOf(operation.getText()).doubleValue(); 
        double storedNumbe; 
if (lastOperator == '+' && equaled = false) 

b = operation.getText(); 
t = b; 
b = 0; 
storedNumbe = s + t; 
lastOperator = "btn"; 
operation.getText() = storedNumbe; 
s = storedNumbe; 
else if (lastOperator == '-'&& equaled = false) 

b = operation.getText(); 
t = b; 
b = 0; 
storedNumbe = s - t; 
lastOperator = "btn"; 
operation.getText() = storedNumbe; 
s = storedNumbe; 

else if (lastOperator == '*' && equaled = false) 

b = operation.getText(); 
t = b; 
b = 0; 
storedNumbe = s * t; 
lastOperator = "btn"; 
operation.getText() = storedNumbe; 
s =        (int) storedNumbe; 

else if (lastOperator == '/' && equaled = false) 

b = operation.getText(); 
t = b; 
b = 0; 
storedNumbe = s * t; 
lastOperator = "btn"; 
operation.getText() = storedNumbe; 
s =        (int) storedNumbe; 

else if (lastOperator == '+' && equaled ) 

b = operation.getText(); 
t = b; 
b = 0; 
storedNumbe = s + t; 
lastOperator = '?'; 
operation.getText() = storedNumbe; 
a =        (int) storedNumbe; 

else if (lastOperator == '-' && equaled ) 

b = operation.getText(); 
t = b; 
b = 0; 
storedNumbe = s - t; 
lastOperator == "?"; 
operation.getText() = storedNumbe; 
a =        (int) storedNumbe; 

else if (lastOperator == '*' && equaled ) 

b = operation.getText(); 
t = b; 
b = 0; 
storedNumbe = s * t; 
lastOperator = "?"; 
operation.getText() = storedNumbe; 
a =        (int) storedNumbe; 

else if (lastOperator == '/' && equaled ) 

b = operation.getText(); 
t = b; 
b = 0; 
storedNumbe = s / t; 
lastOperator = "?"; 
operation.getText()= storedNumbe; 
a = storedNumbe; 


// GridBagLayout メソッド 
private void addComponent(GridBagLayout layout, Component component, int row, int col, int width, int height) 

GridBagConstraints constraints = new GridBagConstraints(); 
constraints.fill = GridBagConstraints.BOTH; 
constraints.insets = new Insets(10, 2, 10, 2); 
constraints.weightx = 100; 
constraints.weighty = 100; 
constraints.gridx = col; 
constraints.gridy = row; 
constraints.gridwidth = width; 
constraints.gridheight = height; 
layout.setConstraints(component, constraints); 
if (component instanceof JButton) 
((JButton)component).addActionListener(this); 
getContentPane().add(component); 

//windows 
public static void main(String[] args) 

Main calc = new Main(); 
calc.setSize(290, 400); 
calc.setVisible(true); 
}     public void actionPerformed(ActionEvent e) { 
        throw new UnsupportedOperationException("Not supported yet."); 
    } 

解决方案 »

  1.   

    中文,日文混用,我有一个让楼LZ参考一下,是Core Java上的
      

  2.   


     1. import java.awt.*;
      2. import java.awt.event.*;
      3. import javax.swing.*;
      4.
      5. public class Calculator
      6. {
      7.    public static void main(String[] args)
      8.    {
      9.       CalculatorFrame frame = new CalculatorFrame();
     10.      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     11.      frame.setVisible(true);
     12.   }
     13. }
     14.
     15. /**
     16.    A frame with a calculator panel.
     17. */
     18. class CalculatorFrame extends JFrame
     19. {
     20.    public CalculatorFrame()
     21.    {
     22.       setTitle("Calculator");
     23.       CalculatorPanel panel = new CalculatorPanel();
     24.       add(panel);
     25.       pack();
     26.    }
     27. }
     28.
     29. /**
     30.    A panel with calculator buttons and a result display.
     31. */
     32. class CalculatorPanel extends JPanel
     33. {
     34.    public CalculatorPanel()
     35.    {
     36.       setLayout(new BorderLayout());
     37.
     38.       result = 0;
     39.       lastCommand = "=";
     40.       start = true;
     41.
     42.       // add the display
     43.
     44.       display = new JButton("0");
     45.       display.setEnabled(false);
     46.       add(display, BorderLayout.NORTH);
     47.
     48.       ActionListener insert = new InsertAction();
     49.       ActionListener command = new CommandAction();
     50.
     51.       // add the buttons in a 4 x 4 grid
     52.
     53.       panel = new JPanel();
     54.       panel.setLayout(new GridLayout(4, 4));
     55.
     56.       addButton("7", insert);
     57.       addButton("8", insert);
     58.       addButton("9", insert);
     59.       addButton("/", command);
     60.
     61.       addButton("4", insert);
     62.       addButton("5", insert);
     63.       addButton("6", insert);
     64.       addButton("*", command);
     65.
     66.       addButton("1", insert);
     67.       addButton("2", insert);
     68.       addButton("3", insert);
     69.       addButton("-", command);
     70.
     71.       addButton("0", insert);
     72.       addButton(".", insert);
     73.       addButton("=", command);
     74.       addButton("+", command);
     75.
     76.       add(panel, BorderLayout.CENTER);
     77.    }
     78.
     79.    /**
     80.       Adds a button to the center panel.
     81.       @param label the button label
     82.       @param listener the button listener
     83.    */
     84.    private void addButton(String label, ActionListener listener)
     85.    {
     86.       JButton button = new JButton(label);
     87.       button.addActionListener(listener);
     88.       panel.add(button);
     89.    }
     90.
     91.    /**
     92.       This action inserts the button action string to the
     93.       end of the display text.
     94.    */
     95.    private class InsertAction implements ActionListener
     96.    {
     97.       public void actionPerformed(ActionEvent event)
     98.       {
     99.          String input = event.getActionCommand();
    100.          if (start)
    101.          {
    102.             display.setText("");
    103.             start = false;
    104.          }
    105.          display.setText(display.getText() + input);
    106.       }
    107.    }
    108.
    109.    /**
    110.       This action executes the command that the button
    111.       action string denotes.
    112.    */
    113.    private class CommandAction implements ActionListener
    114.    {
    115.       public void actionPerformed(ActionEvent event)
    116.       {
    117.         String command = event.getActionCommand();
    118.
    119.         if (start)
    120.         {
    121.            if (command.equals("-"))
    122.            {
    123.               display.setText(command);
    124.               start = false;
    125.            }
    126.            else
    127.               lastCommand = command;
    128.         }
    129.         else
    130.         {
    131.            calculate(Double.parseDouble(display.getText()));
    132.            lastCommand = command;
    133.            start = true;
    134.          }
    135.       }
    136.    }
    137.
    138.    /**
    139.       Carries out the pending calculation.
    140.       @param x the value to be accumulated with the prior result.
    141.    */
    142.    public void calculate(double x)
    143.    {
    144.       if (lastCommand.equals("+")) result += x;
    145.       else if (lastCommand.equals("-")) result -= x;
    146.       else if (lastCommand.equals("*")) result *= x;
    147.       else if (lastCommand.equals("/")) result /= x;
    148.       else if (lastCommand.equals("=")) result = x;
    149.       display.setText("" + result);
    150.    }
    151.
    152.    private JButton display;
    153.    private JPanel panel;
    154.    private double result;
    155.    private String lastCommand;
    156.    private boolean start;
    157. }