科学计算器VB程序代码 10

 我来答
757126906
2007-09-13
知道答主
回答量:2
采纳率:0%
帮助的人:0
展开全部
/**
* Calculator
* A shareware calculator
*
* @author {@link http://blog.csdn.net/hongweijin Bingbing Li}
*
* @recitation 0101 Matt Carlson
*
* @date 12/7/2005 12:08AM
*
*/

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

public class Calculator extends JFrame implements ActionListener
{
public static final int WIDTH = 800; // width of the window
public static final int HEIGHT = 600; // height of the window
public static final int BUTTON_WIDTH = 80; // width of the buttons
public static final int BUTTON_HEIGHT = 60; // height of the buttons

private CardLayout dealer; // card layout
private JPanel deckPanel; // used to card layout

private JTextField result; // the calculate result
private JCheckBoxMenuItem scientificMode; // the menu item of the mode

private Box vPanel1; // the first line buttons of the
// scientific mode.

private JButton mod; // the modular button
private JButton xey; // the button of Yth root of X
private JButton ms; // save button
private JButton mr; // release the stored value
private JButton mc; // clear the stored value

private double head = 0.0; // the first number of the equation
private double tail = 0.0; // the second number of the equation

private boolean substitution = true; // whether substituted the answer or not

private String operatedCommand = "NOTHING"; // the current operated command
private String preOperatedCommand = "NOTHING"; // remember the pre-operated command

private double variableMemory = 0.0; // the variable of the memory

private JButton jButtonR; // the register's button
private JButton jButtonE; // the edit's button

private JTextField nameField; // the field of the name
private JTextField countryField; // the field of the country
private JTextField zipField; // the field of the zip
private JTextField stateField; // the field of the state
private JTextField cityField ; // the field of the city
private JTextField streetAddressField; // the field of the address

private JTextField first; // the first part of the key
private JTextField second; // the second part of the key
private JTextField third; // the third part of the key
private JTextField fourth; // the fourth part of the key

public Calculator()
{
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Normal Calculator");

Container contentPre = getContentPane(); // the container of the window

dealer = new CardLayout(); // create a new card layout
deckPanel = new JPanel();
deckPanel.setLayout(dealer);

Box content = Box.createVerticalBox(); // create a new vertical Box

Box registration = Box.createVerticalBox();

/****************************************************
* menu
* file
*****************************************************/
JMenu fileMenu = new JMenu("File");
JMenuItem exitItemOfFile = new JMenuItem("Exit");
exitItemOfFile.addActionListener(this);
fileMenu.add(exitItemOfFile);
/**
* menu
* settings
*/
JMenu settingsMenu = new JMenu("Settings");
JMenuItem registrationInfo = new JMenuItem("Registration Info");
registrationInfo.addActionListener(this);
settingsMenu.add(registrationInfo);

scientificMode = new JCheckBoxMenuItem("Scientific Mode");
scientificMode.addActionListener(this);
settingsMenu.add(scientificMode);

JMenuBar jMenuBar = new JMenuBar();
jMenuBar.add(fileMenu);
jMenuBar.add(settingsMenu);
setJMenuBar(jMenuBar);

/****************************************************
* textFiled panel
*****************************************************/

result = new JTextField("0"); // the initiated value of the answer
result.setBackground(Color.CYAN); // set the back ground color with cyan
result.setHorizontalAlignment(JTextField.RIGHT); // set the horizontal alignment

content.add(result);

content.add(paintButtons());

deckPanel.add("cal", content); // add the calculators card to the layout

registration.add(paintRegistration()); // add the register window

deckPanel.add("reg", registration); // add the register window to the layout

contentPre.add(deckPanel); // add the cards to the container
}

/**
* paint the buttons of the two models.
*
*/
public Box paintButtons()
{
/****************************************************
* Buttons
*****************************************************/

/**
* line1
*/
vPanel1 = Box.createVerticalBox();

// add the backwards's button
JButton backwards = new JButton("1/X");
backwards.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
backwards.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
backwards.addActionListener(this);
vPanel1.add(backwards);

// add the factorial button
JButton factorial = new JButton("X!");
factorial.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
factorial.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
factorial.addActionListener(this);
vPanel1.add(factorial);

// add the square's button
JButton square = new JButton("<html>X<sup>2</sup></html>");
square.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
square.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
square.addActionListener(this);
square.setActionCommand("sqr");
vPanel1.add(square);

// add the square root's button
JButton squareRoot = new JButton("\u221a");
squareRoot.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
squareRoot.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
squareRoot.addActionListener(this);
vPanel1.add(squareRoot);

// add the power's button
JButton power = new JButton("<html>X<sup>Y</sup></html>");
power.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
power.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
power.addActionListener(this);
power.setActionCommand("pow");
vPanel1.add(power);

/**
* line2
*/
Box vPanel2 = Box.createVerticalBox();

// add the modular button
mod = new JButton("Mod");
mod.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
mod.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
mod.addActionListener(this);
vPanel2.add(mod);

// add the seven button
JButton seven = new JButton("7");
seven.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
seven.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
seven.addActionListener(this);
vPanel2.add(seven);

// add the four button
JButton four = new JButton("4");
four.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
four.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
four.addActionListener(this);
vPanel2.add(four);

// add the one button
JButton one = new JButton("1");
one.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
one.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
one.addActionListener(this);
vPanel2.add(one);

// add the zero button
JButton zero = new JButton("0");
zero.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
zero.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
zero.addActionListener(this);
vPanel2.add(zero);

/**
* line3
*/
Box vPanel3 = Box.createVerticalBox();

// add the Yth root of X button
xey = new JButton("XeY");
xey.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
xey.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
xey.addActionListener(this);
vPanel3.add(xey);

// add the eight button
JButton eight = new JButton("8");
eight.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
eight.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
eight.addActionListener(this);
vPanel3.add(eight);

// add the five button
JButton five = new JButton("5");
five.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
five.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
five.addActionListener(this);
vPanel3.add(five);

// add the two button
JButton two = new JButton("2");
two.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
two.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
two.addActionListener(this);
vPanel3.add(two);

// add the dot button
JButton dot = new JButton(".");
dot.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
dot.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
dot.addActionListener(this);
vPanel3.add(dot);

/**
* line4
*/
Box vPanel4 = Box.createVerticalBox();

// add the MS button
ms = new JButton("MS");
ms.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
ms.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
ms.addActionListener(this);
vPanel4.add(ms);

// add the nine button
JButton nine = new JButton("9");
nine.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
nine.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
nine.addActionListener(this);
vPanel4.add(nine);

// add the six button
JButton six = new JButton("6");
six.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
six.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
six.addActionListener(this);
vPanel4.add(six);

// add the three button
JButton three = new JButton("3");
three.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
three.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
three.addActionListener(this);
vPanel4.add(three);

// add the plusMinus button
JButton plusMinus = new JButton("\u00b1");
plusMinus.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
plusMinus.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
plusMinus.addActionListener(this);
vPanel4.add(plusMinus);

/**
* line5
*/
Box vPanel5 = Box.createVerticalBox();

// add the MR button
mr = new JButton("MR");
mr.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
mr.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
mr.addActionListener(this);
vPanel5.add(mr);

// add the division button
JButton division = new JButton("\u00F7");
division.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
division.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
division.addActionListener(this);
vPanel5.add(division);

// add the multiplication button
JButton multiplication = new JButton("\u00d7");
multiplication.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
multiplication.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
multiplication.addActionListener(this);
vPanel5.add(multiplication);

// add the subtract button
JButton subtract = new JButton("-");
subtract.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
subtract.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
subtract.addActionListener(this);
vPanel5.add(subtract);

// add the plus button
JButton plus = new JButton("+");
plus.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
plus.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
plus.addActionListener(this);
vPanel5.add(plus);

/**
* line6
*/
Box vPanel6 = Box.createVerticalBox();

// add the MC button
mc = new JButton("MC");
mc.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
mc.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
mc.addActionListener(this);
vPanel6.add(mc);

// add the C button
JButton c = new JButton("C");
c.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
c.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
c.addActionListener(this);
vPanel6.add(c);

// add a vertical strut
Component verticalStrut =
Box.createVerticalStrut(BUTTON_HEIGHT);
vPanel6.add(verticalStrut);

// add the enter button
JButton enter = new JButton("=");
enter.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT * 2));
enter.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT * 2));
enter.addActionListener(this);
vPanel6.add(enter);

/**
* Buttons panel
*/
Box buttonsPanel = Box.createHorizontalBox();

buttonsPanel.add(vPanel1);
buttonsPanel.add(vPanel2);
buttonsPanel.add(vPanel3);
buttonsPanel.add(vPanel4);
buttonsPanel.add(vPanel5);
buttonsPanel.add(vPanel6);

/**********************************************************
* the initial state is normal calculator
***********************************************************/
vPanel1.setVisible(false);
mod.setVisible(false);
xey.setVisible(false);
ms.setVisible(false);
mr.setVisible(false);
mc.setVisible(false);

return buttonsPanel;
}

/**
* paint the registration window.
*
*/
public Box paintRegistration()
{
Box registration = Box.createVerticalBox();
/*
* title
*/
JLabel titleRegistration = new JLabel("Bingbing's Calculator Registration");
registration.add(titleRegistration);

/*
* information
*/
JPanel information = new JPanel();
information.setLayout(new GridLayout(6, 2));
//Name
JLabel name = new JLabel("Name:");
nameField = new JTextField();
information.add(name);
information.add(nameField);
//Street Address
JLabel streetAddress = new JLabel("Street Address:");
streetAddressField = new JTextFi
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式