⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exercise14_8.java

📁 Introduction to java programming 一书中所有编程练习部分的源码
💻 JAVA
字号:
// Exercise14_8: Calculator
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Exercise14_8 extends JApplet implements ActionListener {
  private JTextField jtf = new JTextField(10);
  private boolean newNumber = true;
  private int result = 0;
  private String op = "=";

  public void init() {
    JPanel p = new JPanel();
    p.setLayout(new BorderLayout());

    JPanel westPanel = new JPanel();
    westPanel.setLayout(new GridLayout(5, 0));
    westPanel.add(new JButton("   "));
    westPanel.add(new JButton("MC"));
    westPanel.add(new JButton("MR"));
    westPanel.add(new JButton("MS"));
    westPanel.add(new JButton("M+"));

    Panel centerPanel = new Panel();
    centerPanel.setLayout(new BorderLayout());
    Panel p1 = new Panel();
    Panel p2 = new Panel();

    p1.setLayout(new FlowLayout(FlowLayout.RIGHT));
    p1.add(new JButton("Back"));
    p1.add(new JButton("CE"));
    p1.add(new JButton("C"));

    p2.setLayout(new GridLayout(4, 5));
    JButton bt;
    p2.add(bt = new JButton("7"));
    bt.addActionListener(this);
    p2.add(bt = new JButton("8"));
    bt.addActionListener(this);
    p2.add(bt = new JButton("9"));
    bt.addActionListener(this);
    p2.add(bt = new JButton("/"));
    bt.addActionListener(this);
    p2.add(bt = new JButton("sqrt"));
    bt.addActionListener(this);
    p2.add(bt = new JButton("4"));
    bt.addActionListener(this);
    p2.add(bt = new JButton("5"));
    bt.addActionListener(this);
    p2.add(bt = new JButton("6"));
    bt.addActionListener(this);
    p2.add(bt = new JButton("*"));
    bt.addActionListener(this);
    p2.add(bt = new JButton("%"));
    bt.addActionListener(this);
    p2.add(bt = new JButton("1"));
    bt.addActionListener(this);
    p2.add(bt = new JButton("2"));
    bt.addActionListener(this);
    p2.add(bt = new JButton("3"));
    bt.addActionListener(this);
    p2.add(bt = new JButton("-"));
    bt.addActionListener(this);
    p2.add(bt = new JButton("1/x"));
    bt.addActionListener(this);
    p2.add(bt = new JButton("0"));
    bt.addActionListener(this);
    p2.add(bt = new JButton("+/-"));
    bt.addActionListener(this);
    p2.add(bt = new JButton("."));
    p2.add(bt = new JButton("+"));
    bt.addActionListener(this);
    p2.add(bt = new JButton("="));
    bt.addActionListener(this);

    centerPanel.add(p2, BorderLayout.CENTER);
    centerPanel.add(p1, BorderLayout.NORTH);
    p.add(centerPanel, BorderLayout.CENTER);
    p.add(westPanel, BorderLayout.WEST);

    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(p, BorderLayout.CENTER);
    getContentPane().add(jtf, BorderLayout.NORTH);
  }

  public void actionPerformed(ActionEvent e) {
    String actionCommand = e.getActionCommand();
    if ('0' <= actionCommand.charAt(0) &&
      actionCommand.charAt(0) <= '9') {
      if (newNumber) {
        jtf.setText(actionCommand);
        newNumber = false;
      }
      else {
        jtf.setText(jtf.getText() + actionCommand);
      }
    }
    else
      if (newNumber) {
        if (actionCommand.equals("-")) {
          jtf.setText("-");
          newNumber = false;
        }
        else
          op = actionCommand;
      }
      else {
        execute();
        op = actionCommand;
      }
    }

  void execute() {
    int number = new Integer(jtf.getText()).intValue();
    System.out.println("number " + op);
    switch (op.charAt(0)) {
      case '+': result += number; break;
      case '-': result -= number; break;
      case '*': result *= number; break;
      case '/': result /= number; break;
      case '%': result %= number; break;
      case '=': result = number;
    }
    System.out.println("result "+result);
    jtf.setText(new Integer(result).toString());
    newNumber = true;
  }
  
    /**This main method enables the applet to run as an application*/
  public static void main(String[] args) {
    // Create a frame
    JFrame frame = new JFrame("Exercise14_8");

    // Create an instance of the applet
    Exercise14_8 applet = new Exercise14_8();

    // Add the applet instance to the frame
    frame.getContentPane().add(applet, BorderLayout.CENTER);

    // Invoke init() and start()
    applet.init();
    applet.start();

    // Display the frame
    frame.setSize(300, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -