當前位置:編程學習大全網 - 編程語言 - 求壹份JAVA計算機程序設計報告

求壹份JAVA計算機程序設計報告

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.*;

import javax.swing.border.*;

import java.util.*;

@SuppressWarnings("serial")

public class ComputerPad extends JFrame

implements ActionListener

{

private NumberButton[] numberButton;

private OperationButton[] operationButton;

private JButton dotButton;

private JButton signButton;

private JButton backspaceButton;

private JButton reciprocalButton;

private JButton equalButton;

private JButton clearZeroButton;

JPanel panel;

JTextField resultShow;

operationButton = new OperationButton[4];

operationButton[0] = new OperationButton("+");

operationButton[1] = new OperationButton("-");

operationButton[2] = new OperationButton("*");

operationButton[3] = new OperationButton("/");

LinkedList<String> dataList;

// 等號進行默認設置

boolean equalButtonIsPressed = false;

public ComputerPad(){

super("yuelou計算器");

dataList = new LinkedList<String>();

numberButton = new NumberButton[10];

for(int i = 0; i <= 9; i++){

numberButton[i] = new NumberButton(i);

numberButton[i].addActionListener(this);

}

operationButton = new OperationButton[4];

for(int i = 0;i < 4; i++){

operationButton[i] = new OperationButton(operators[i]);

operationButton[i].addActionListener(this);

}

//符號進行初始設置

dotButton = new JButton(".");

signButton = new JButton("+/-");

equalButton = new JButton("=");

reciprocalButton = new JButton("1/x");

backspaceButton = new JButton("退格");

clearZeroButton = new JButton("AC");

//設置顏色

clearZeroButton.setForeground(Color.red);

backspaceButton.setForeground(Color.red);

reciprocalButton.setForeground(Color.red);

equalButton.setForeground(Color.blue);

signButton.setForeground(Color.blue);

dotButton.setForeground(Color.blue);

clearZeroButton.addActionListener(this);

backspaceButton.addActionListener(this);

reciprocalButton.addActionListener(this);

equalButton.addActionListener(this);

signButton.addActionListener(this);

dotButton.addActionListener(this);

resultShow = new JTextField(10);

resultShow.setHorizontalAlignment(JTextField.RIGHT);

resultShow.setForeground(Color.blue);

resultShow.setFont(new Font("TimesRoman", Font.PLAIN, 15));

resultShow.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));

resultShow.setBackground(Color.white);

resultShow.setEditable(false);

panel = new JPanel();

// 設置四行五列

panel.setLayout(new GridLayout(4,5));

//面板中的第壹行

for(int i = 1; i < 4; i++)

{

p.add(numberButton[i]);

}

p.add(operationButton[0]);

p.add(clearButton);

//面板中的第2行

for(int i = 4; i < 7; i++)

{

p.add(numberButton[i]);

}

p.add(operationButton[1]);

p.add(bkspaceButton)

//面板中的第3行

for(int i = 7; i < 10; i++)

{

p.add(numberButton[i]);

}

p.add(operationButton[2]);

p.add(reciprocalButton);

//面板中的第4行

p.add(numberButton[0]);

p.add(signButton);

p.add(dotButton);

p.add(operationButton[3]);

p.add(equalButton);

add(panel, BorderLayout.CENTER);

add(resultShow, BorderLayout.NORTH);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

setBounds(100, 50, 340, 180);

setResizable(false);

validate();

}

public void actionPerformed(ActionEvent e) {

//如果按下的是“數字按鈕”

if(e.getSource() instanceof NumberButton){

NumberButton b = (NumberButton)e.getSource();

//字符串中沒有字符時

if(dataList.size() == 0){

int number = b.getNumber();

dataList.add("" + number);

resultShow.setText("" + number);

equalButtonIsPressed = false;

}

//字符串中有壹個字符時,並且在此時沒有按過等號

else if(dataList.size() == 1 && equalButtonIsPressed == false){

int number = b.getNumber();

String num = (String)dataList.getFirst();

String s = num.concat("" + number);

//把S放在字符串的第壹個位置,下標為零

dataList.set(0, s);

//把S輸出到窗口

resultShow.setText(s);

}//字符串中有壹個字符時,並且在此時按過等號

else if(dataList.size() == 1 && equalButtonIsPressed == true){

int number = b.getNumber();

//把字符串中的第壹個移出

dataList.removeFirst();

dataList.add("" + number);

equalButtonIsPressed = false;

resultShow.setText("" + number);

}

else if(dataList.size() == 2){

int number = b.getNumber();

dataList.add("" + number);

resultShow.setText("" + number);

}else if(dataList.size() == 3){

int number = b.getNumber();

String num = (String)dataList.getLast();

String s = num.concat("" + number);

dataList.set(2, s);

resultShow.setText(s);

}

}

//如果按下的是“各種操作符按鈕”

else if(e.getSource() instanceof OperationButton){

OperationButton b = (OperationButton)e.getSource();

if(dataList.size() == 1){

String fuhao = b.getOperator();

dataList.add(fuhao);

}

else if(dataList.size() == 2){

String fuhao = b.getOperator();

dataList.set(1, fuhao);

}

else if(dataList.size() == 3){

String fuhao = b.getOperator();

String number1 = (String)dataList.getFirst();

String number2 = (String)dataList.getLast();

// 對加減乘除進行符號提取

String operator = (String)dataList.get(1);

// 對加減乘除進行符號提取

double n1 = Double.parseDouble(number1);

double n2 = Double.parseDouble(number2);

//對數據進行初始化設置

double n = 0;

//進行運算

if(operator.equals("+")){

n = n1 + n2;

}

else if(operator.equals("-")){

n = n1 - n2;

}

else if(operator.equals("*")){

n = n1 * n2;

}

else if(operator.equals("/")){

n = n1 / n2;

}

// 輸出接果 並對輸入的字符串進行輸入

dataList.clear();

dataList.add("" + n);

dataList.add(fuhao);

resultShow.setText("" + n);

}

}

//如果按下的是“等號按鈕”

else if(e.getSource() == equalButton){

equalButtonIsPressed = true;

//鏈表內有兩個字符串時

if(dataList.size() == 2)

{

// 對加減乘除進行符號提取(詳細註釋同上)

String operator =

(String)dataList.get(1);

String num1 =

(String)dataList.getFirst();

double n1 =

Double.parseDouble(num1);

double n = 0;

if(operator.equals("+"))

{

n = n1 + n1;

}

else if(operator.equals("-"))

{

n = n1 - n1;

}

else if(operator.equals("*"))

{

n = n1 * n1;

}

else if(operator.equals("/"))

{

n = n1 / n1;

}

//清理鏈表

dataList.clear();

dataList.add("" + n);

resultShow.setText(""+n);

}

//鏈表內有三個字符串時

else if(dataList.size() == 3)

{

String number1 = (String)dataList.getFirst();

String number2 = (String)dataList.getLast();

String operator = (String)dataList.get(1);

double n1 = Double.parseDouble(number1);

double n2 = Double.parseDouble(number2);

double n = 0;

if(operator.equals("+")){

n = n1 + n2;

}

else if(operator.equals("-")){

n = n1 - n2;

}

else if(operator.equals("*")){

n = n1 * n2;

}

else if(operator.equals("/")){

n = n1 / n2;

}

dataList.clear();

dataList.add("" + n);

resultShow.setText("" + n);

}

}

//如果按下的是“小數點按鈕”

else if(e.getSource() == dotButton){

if(dataList.size() == 0)

{

//默認輸出“0.”

dataList.add("0.");

}

else if(dataList.size() == 1)

{

String num1 = dataList.getFirst();

//判斷以前是否有過輸入小數點,為-1時為沒有加點

if(num1.indexOf('.') == -1)

{

String s = num1.concat(".");

dataList.set(0,s);

}

}

else if(dataList.size() == 2)

{

dataList.add("0.");

}

else if(dataList.size() == 3)

{

String num2 = dataList.getLast();

if(num2.indexOf('.') == -1)

{

String s = num2.concat(".");

dataList.set(2,s);

}

}

}

//如果按下的是“退格按鈕”

else if(e.getSource() == backspaceButton){

if(dataList.size() == 1)

{

String num1 = dataList.getFirst();

String s ;

if(num1.length() > 1)

{

//截取字符串中的子串

s = num1.substring(0,num1.length()-1);

}

else

{

s = "0";

}

dataList.set(0,""+s);

resultShow.setText(s);

}

else if(dataList.size() == 3)

{

String num2 = dataList.getLast();

String s ;

if(num2.length() > 1)

{

s = num2.substring(0,num2.length()-1);

}

else

{

s = "0";

}

dataList.set(2,""+s);

resultShow.setText(s);

}

}

//如果按下的是“正負號按鈕”

else if(e.getSource() == signButton){

if(dataList.size() == 1)

{

String num1 = dataList.getFirst();

double n1 = Double.parseDouble(num1);

n1 = -1 * n1;

dataList.set(0,""+n1);

resultShow.setText(""+n1);

}

else if(dataList.size() == 3)

{

String num2 = dataList.getLast();

double n2 = Double.parseDouble(num2);

n2 = -1 * n2;

dataList.set(2,""+n2);

resultShow.setText(""+n2);

}

}

//如果按下的是“倒數按鈕”

else if(e.getSource() == reciprocalButton){

if(dataList.size() == 1)

{

String num1 = dataList.getFirst();

double n1 = Double.parseDouble(num1);

n1 = 1 / n1;

dataList.set(0,""+n1);

resultShow.setText(""+n1);

}

else if(dataList.size() == 3)

{

String num2 = dataList.getLast();

double n2 = Double.parseDouble(num2);

n2 = 1 / n2;

dataList.set(2,""+n2);

resultShow.setText(""+n2);

}

}

//如果按下的是“清零按鈕”

else if(e.getSource() == clearZeroButton){

dataList.clear();

resultShow.setText("0");

}

}

public static void main(String[] args){

new ComputerPad();

}

}

這是主類 ~妳建立壹個類

還有兩個分類

我在發給妳

記得把我的名字該妳的就可以拉

import javax.swing.*;

import java.awt.*;

public class NumberButton

extends JButton

{

private int number;

NumberButton(int number)

{

super("" + number);

this.number = number;

setForeground(Color.blue);

setFont(new Font("Serif", Font.BOLD,15));

}

public int getNumber()

{

return number;

}

}

///////////

import java.awt.Color;

import java.awt.Font;

import javax.swing.JButton;

public class OperationButton

extends JButton

{

private String operator;

OperationButton(String operator)

{

super(operator);

this.operator = operator;

setForeground(Color.RED);

setFont(new Font("Serif", Font.BOLD,15));

}

public String getOperator()

{

return operator;

}

}

  • 上一篇:漢語誰發明的?
  • 下一篇:寒武紀公司簡介
  • copyright 2024編程學習大全網