简单计算器(java)
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
JComboBox choiceFuhao;
JTextArea textShow;
JButton button;
OperatorListener operator; //监视ItemEvent时间的监听器
ComputerListener computer; //监视ActionEvent事件的监听器
choice=box;
}
public void setWorkTogether(ComputerListener computer){
workTogether=computer;
}
public void itemStateChanged(ItemEvent e) {
String fuhao=choice.getSelectedItem().toString();
简单计算器.java
package简单计算器;
publicclass简单计算器{
publicstaticvoidmain(String args[]){
WindowOperation win=newWindowOperation();
win.setBounds(100,100,390,360);
win.setTitle("简单计算器");
choiceFuhao.addItem(a[i]);
}
textShow=new JTextArea(9,30);
operator=new OperatorListener();
computer=new ComputerListener();
operator.setJCombobox(choiceFuhao);
import javax.swing.*;
public class OperatorListener implements ItemListener {
JComboBox choice;
ComputerListener workTogether;
public void setJCombobox(JComboBox box){
result=numbБайду номын сангаасr1/number2;
}
textShow.append(number1+" "+fuhao+" "+number2+"="+result+"\n");
}
catch(Exception exp){
textShow.append("\n请输入数字字符\n");
}
}
}
以上是《Java2实用教程(第四版)》第八章例子8(简单计算器)的完整代码。
add(button);
add(new JScrollPane(textShow));
}
OperatorListener.java
package简单计算器;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
try{
double number1=Double.parseDouble(inputNumberOne.getText());
double number2=Double.parseDouble(inputNumberTwo.getText());
double result=0;
if(fuhao.equals("+")){
inputNumberTwo=t;
}
public void setJTextArea(JTextArea area){
textShow=area;
}
public void setFuhao(String s){
fuhao=s;
}
public void actionPerformed(ActionEvent e){
workTogether.setFuhao(fuhao);
}
}
}
ComputerListener.java
package 简单计算器;
import java.awt.event.*;
import javax.swing.*;
public class ComputerListener implements ActionListener {
choiceFuhao.addItemListener(operator); //choiceFuhao是事件源,Operator是监视器
button.addActionListener(computer);
add(inputNumberOne);
add(choiceFuhao);
add(inputNumberTwo);
inputNumberTwo=new JTextField(5);
choiceFuhao=new JComboBox();
button=new JButton("计算");
choiceFuhao.addItem("选择运算符号:");
String [] a={"+","-","*","/"};
for(int i=0;i<a.length;i++){
operator.setWorkTogether(computer);
computer.setJTextFieldOne(inputNumberOne);
computer.setJTextFieldTwo(inputNumberTwo);
computer.setJTextArea(textShow);
JTextField inputNumberOne,inputNumberTwo;
JTextArea textShow;
String fuhao;
public void setJTextFieldOne(JTextField t){
inputNumberOne=t;
}
public void setJTextFieldTwo(JTextField t){
public WindowOperation(){
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init(){
setLayout(new FlowLayout());
inputNumberOne=new JTextField(5);
result=number1+number2;
}
else if(fuhao.equals("-")){
result=number1-number2;
}
else if(fuhao.equals("*")){
result=number1*number2;
}
else if(fuhao.equals("/")){
}
}
WindowOperation.java
package 简单计算器;
import java.awt.*;
import javax.swing.*;
import java.io.*;
public class WindowOperation extends JFrame{
JTextField inputNumberOne,inputNumberTwo;
JTextArea textShow;
JButton button;
OperatorListener operator; //监视ItemEvent时间的监听器
ComputerListener computer; //监视ActionEvent事件的监听器
choice=box;
}
public void setWorkTogether(ComputerListener computer){
workTogether=computer;
}
public void itemStateChanged(ItemEvent e) {
String fuhao=choice.getSelectedItem().toString();
简单计算器.java
package简单计算器;
publicclass简单计算器{
publicstaticvoidmain(String args[]){
WindowOperation win=newWindowOperation();
win.setBounds(100,100,390,360);
win.setTitle("简单计算器");
choiceFuhao.addItem(a[i]);
}
textShow=new JTextArea(9,30);
operator=new OperatorListener();
computer=new ComputerListener();
operator.setJCombobox(choiceFuhao);
import javax.swing.*;
public class OperatorListener implements ItemListener {
JComboBox choice;
ComputerListener workTogether;
public void setJCombobox(JComboBox box){
result=numbБайду номын сангаасr1/number2;
}
textShow.append(number1+" "+fuhao+" "+number2+"="+result+"\n");
}
catch(Exception exp){
textShow.append("\n请输入数字字符\n");
}
}
}
以上是《Java2实用教程(第四版)》第八章例子8(简单计算器)的完整代码。
add(button);
add(new JScrollPane(textShow));
}
OperatorListener.java
package简单计算器;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
try{
double number1=Double.parseDouble(inputNumberOne.getText());
double number2=Double.parseDouble(inputNumberTwo.getText());
double result=0;
if(fuhao.equals("+")){
inputNumberTwo=t;
}
public void setJTextArea(JTextArea area){
textShow=area;
}
public void setFuhao(String s){
fuhao=s;
}
public void actionPerformed(ActionEvent e){
workTogether.setFuhao(fuhao);
}
}
}
ComputerListener.java
package 简单计算器;
import java.awt.event.*;
import javax.swing.*;
public class ComputerListener implements ActionListener {
choiceFuhao.addItemListener(operator); //choiceFuhao是事件源,Operator是监视器
button.addActionListener(computer);
add(inputNumberOne);
add(choiceFuhao);
add(inputNumberTwo);
inputNumberTwo=new JTextField(5);
choiceFuhao=new JComboBox();
button=new JButton("计算");
choiceFuhao.addItem("选择运算符号:");
String [] a={"+","-","*","/"};
for(int i=0;i<a.length;i++){
operator.setWorkTogether(computer);
computer.setJTextFieldOne(inputNumberOne);
computer.setJTextFieldTwo(inputNumberTwo);
computer.setJTextArea(textShow);
JTextField inputNumberOne,inputNumberTwo;
JTextArea textShow;
String fuhao;
public void setJTextFieldOne(JTextField t){
inputNumberOne=t;
}
public void setJTextFieldTwo(JTextField t){
public WindowOperation(){
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init(){
setLayout(new FlowLayout());
inputNumberOne=new JTextField(5);
result=number1+number2;
}
else if(fuhao.equals("-")){
result=number1-number2;
}
else if(fuhao.equals("*")){
result=number1*number2;
}
else if(fuhao.equals("/")){
}
}
WindowOperation.java
package 简单计算器;
import java.awt.*;
import javax.swing.*;
import java.io.*;
public class WindowOperation extends JFrame{
JTextField inputNumberOne,inputNumberTwo;