用java语言,编写一个计算器 20
原题在这里,不用太复杂只要这些功能就好目前在学的时awt,swing.基于swing的.设计一个图形界面的计算器应用程序,完成普通的算术运算。设计要求:计算器可以完成加法...
原题在这里,不用太复杂 只要这些功能就好 目前在学的时awt,swing.基于swing的.
设计一个图形界面的计算器应用程序,完成普通的算术运算。
设计要求:计算器可以完成加法、减法、乘法、除法和取余运算。(其余功能可以自行添加)
设计要点:本程序主要练习窗口的布局,以及按钮事件的触发。
邮箱417362433@qq.com 展开
设计一个图形界面的计算器应用程序,完成普通的算术运算。
设计要求:计算器可以完成加法、减法、乘法、除法和取余运算。(其余功能可以自行添加)
设计要点:本程序主要练习窗口的布局,以及按钮事件的触发。
邮箱417362433@qq.com 展开
6个回答
展开全部
有一个现成的.你看看吧
import java.awt.event.ActionEvent;
public class Application extends JFrame {
protected String str = "";
protected boolean isChar = true;
protected boolean isEqual = false;
protected JTextField textField;
public Application() {
Listener listerner = new Listener(this);
getContentPane().setLayout(null);
JButton button = new JButton("7");
button.addActionListener(listerner);
button.setBounds(12, 69, 43, 27);
getContentPane().add(button);
textField = new JTextField();
textField.setText("0");
textField.setEditable(false);
textField.setHorizontalAlignment(JTextField.RIGHT);
textField.setBounds(12, 22, 377, 27);
getContentPane().add(textField);
textField.setColumns(10);
JButton button_1 = new JButton("8");
button_1.addActionListener(listerner);
button_1.setBounds(103, 69, 43, 27);
getContentPane().add(button_1);
JButton button_2 = new JButton("9");
button_2.addActionListener(listerner);
button_2.setBounds(182, 69, 43, 27);
getContentPane().add(button_2);
JButton button_3 = new JButton("4");
button_3.addActionListener(listerner);
button_3.setBounds(12, 106, 43, 27);
getContentPane().add(button_3);
JButton button_4 = new JButton("5");
button_4.addActionListener(listerner);
button_4.setBounds(103, 106, 43, 27);
getContentPane().add(button_4);
JButton button_5 = new JButton("6");
button_5.addActionListener(listerner);
button_5.setBounds(182, 106, 43, 27);
getContentPane().add(button_5);
JButton button_6 = new JButton("1");
button_6.addActionListener(listerner);
button_6.setBounds(12, 143, 43, 27);
getContentPane().add(button_6);
JButton button_7 = new JButton("2");
button_7.addActionListener(listerner);
button_7.setBounds(103, 143, 43, 27);
getContentPane().add(button_7);
JButton button_8 = new JButton("3");
button_8.addActionListener(listerner);
button_8.setBounds(182, 143, 43, 27);
getContentPane().add(button_8);
JButton button_9 = new JButton("+");
button_9.addActionListener(listerner);
button_9.setBounds(269, 72, 43, 27);
getContentPane().add(button_9);
JButton button_10 = new JButton("-");
button_10.addActionListener(listerner);
button_10.setBounds(346, 72, 43, 27);
getContentPane().add(button_10);
JButton button_11 = new JButton("*");
button_11.addActionListener(listerner);
button_11.setBounds(269, 109, 43, 27);
getContentPane().add(button_11);
JButton button_12 = new JButton("/");
button_12.addActionListener(listerner);
button_12.setBounds(346, 109, 43, 27);
getContentPane().add(button_12);
JButton button_13 = new JButton("=");
button_13.addActionListener(listerner);
button_13.setBounds(346, 143, 43, 27);
getContentPane().add(button_13);
JButton button_14 = new JButton("0");
button_14.addActionListener(listerner);
button_14.setBounds(103, 180, 43, 27);
getContentPane().add(button_14);
JButton btnReset = new JButton("reset");
btnReset.addActionListener(listerner);
btnReset.setBounds(269, 180, 118, 27);
getContentPane().add(btnReset);
JButton button_15 = new JButton(".");
button_15.addActionListener(listerner);
button_15.setBounds(269, 146, 43, 27);
getContentPane().add(button_15);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(442, 260);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new Application();
}
}
class Listener implements ActionListener {
private Application app = null;
public Listener(Application app) {
this.app = app;
}
public void actionPerformed(ActionEvent e) {
String value = e.getActionCommand();
if (value.matches("[0-9.]")) {
if (app.isChar) {
app.textField.setText("");
app.isChar = false;
}
if (app.isEqual && app.str.matches("[0-9.]*")) {
app.str = "";
app.isEqual = false;
}
app.str += value;
app.textField.setText(app.textField.getText() + value);
} else if (value.matches("[\\+\\-\\*/]")) {
if (!app.str.substring(app.str.length() - 1)
.matches("[\\+\\-\\*/]")) {
app.str += value;
app.isChar = true;
}
} else if ("=".equals(value)) {
app.isEqual = true;
if (app.str.substring(app.str.length() - 1).matches("[\\+\\-]")) {
app.str += "0";
} else if (app.str.substring(app.str.length() - 1)
.matches("[\\*/]")) {
app.str += "1";
}
Interpreter bsh = new Interpreter();
String obj = null;
try {
obj = bsh.eval(app.str).toString();
} catch (Exception exception) {
System.out.println(exception.getMessage());
}
System.out.println(app.str);
app.textField.setText(obj);
app.str = obj;
app.isChar = true;
} else {
app.str = "";
app.textField.setText("0");
}
}
}
import java.awt.event.ActionEvent;
public class Application extends JFrame {
protected String str = "";
protected boolean isChar = true;
protected boolean isEqual = false;
protected JTextField textField;
public Application() {
Listener listerner = new Listener(this);
getContentPane().setLayout(null);
JButton button = new JButton("7");
button.addActionListener(listerner);
button.setBounds(12, 69, 43, 27);
getContentPane().add(button);
textField = new JTextField();
textField.setText("0");
textField.setEditable(false);
textField.setHorizontalAlignment(JTextField.RIGHT);
textField.setBounds(12, 22, 377, 27);
getContentPane().add(textField);
textField.setColumns(10);
JButton button_1 = new JButton("8");
button_1.addActionListener(listerner);
button_1.setBounds(103, 69, 43, 27);
getContentPane().add(button_1);
JButton button_2 = new JButton("9");
button_2.addActionListener(listerner);
button_2.setBounds(182, 69, 43, 27);
getContentPane().add(button_2);
JButton button_3 = new JButton("4");
button_3.addActionListener(listerner);
button_3.setBounds(12, 106, 43, 27);
getContentPane().add(button_3);
JButton button_4 = new JButton("5");
button_4.addActionListener(listerner);
button_4.setBounds(103, 106, 43, 27);
getContentPane().add(button_4);
JButton button_5 = new JButton("6");
button_5.addActionListener(listerner);
button_5.setBounds(182, 106, 43, 27);
getContentPane().add(button_5);
JButton button_6 = new JButton("1");
button_6.addActionListener(listerner);
button_6.setBounds(12, 143, 43, 27);
getContentPane().add(button_6);
JButton button_7 = new JButton("2");
button_7.addActionListener(listerner);
button_7.setBounds(103, 143, 43, 27);
getContentPane().add(button_7);
JButton button_8 = new JButton("3");
button_8.addActionListener(listerner);
button_8.setBounds(182, 143, 43, 27);
getContentPane().add(button_8);
JButton button_9 = new JButton("+");
button_9.addActionListener(listerner);
button_9.setBounds(269, 72, 43, 27);
getContentPane().add(button_9);
JButton button_10 = new JButton("-");
button_10.addActionListener(listerner);
button_10.setBounds(346, 72, 43, 27);
getContentPane().add(button_10);
JButton button_11 = new JButton("*");
button_11.addActionListener(listerner);
button_11.setBounds(269, 109, 43, 27);
getContentPane().add(button_11);
JButton button_12 = new JButton("/");
button_12.addActionListener(listerner);
button_12.setBounds(346, 109, 43, 27);
getContentPane().add(button_12);
JButton button_13 = new JButton("=");
button_13.addActionListener(listerner);
button_13.setBounds(346, 143, 43, 27);
getContentPane().add(button_13);
JButton button_14 = new JButton("0");
button_14.addActionListener(listerner);
button_14.setBounds(103, 180, 43, 27);
getContentPane().add(button_14);
JButton btnReset = new JButton("reset");
btnReset.addActionListener(listerner);
btnReset.setBounds(269, 180, 118, 27);
getContentPane().add(btnReset);
JButton button_15 = new JButton(".");
button_15.addActionListener(listerner);
button_15.setBounds(269, 146, 43, 27);
getContentPane().add(button_15);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(442, 260);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new Application();
}
}
class Listener implements ActionListener {
private Application app = null;
public Listener(Application app) {
this.app = app;
}
public void actionPerformed(ActionEvent e) {
String value = e.getActionCommand();
if (value.matches("[0-9.]")) {
if (app.isChar) {
app.textField.setText("");
app.isChar = false;
}
if (app.isEqual && app.str.matches("[0-9.]*")) {
app.str = "";
app.isEqual = false;
}
app.str += value;
app.textField.setText(app.textField.getText() + value);
} else if (value.matches("[\\+\\-\\*/]")) {
if (!app.str.substring(app.str.length() - 1)
.matches("[\\+\\-\\*/]")) {
app.str += value;
app.isChar = true;
}
} else if ("=".equals(value)) {
app.isEqual = true;
if (app.str.substring(app.str.length() - 1).matches("[\\+\\-]")) {
app.str += "0";
} else if (app.str.substring(app.str.length() - 1)
.matches("[\\*/]")) {
app.str += "1";
}
Interpreter bsh = new Interpreter();
String obj = null;
try {
obj = bsh.eval(app.str).toString();
} catch (Exception exception) {
System.out.println(exception.getMessage());
}
System.out.println(app.str);
app.textField.setText(obj);
app.str = obj;
app.isChar = true;
} else {
app.str = "";
app.textField.setText("0");
}
}
}
展开全部
package Calculater;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JFrame;
public class MyCalculater extends JFrame {
JTextField text;
public MyCalculater(){
text=new JTextField(10);
text.setEditable(false);
text.setBorder(BorderFactory.createLineBorder(Color.red,2));
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel numPa=new JPanel();
JPanel opePa=new JPanel();
Container c=getContentPane();
numPa.setLayout(new GridLayout(4,3,2,2));
opePa.setLayout(new GridLayout(4,1,2,2));
JButton numButt[]=new JButton[12];
JButton opeButt[]=new JButton[8];
for(int i=0;i<10;i++){
numButt[i]=new JButton(new Integer(i).toString());
}
numButt[10]=new JButton(".");
numButt[11]=new JButton("CE");
opeButt[0]=new JButton("+");
opeButt[1]=new JButton("-");
opeButt[2]=new JButton("x");
opeButt[3]=new JButton("/");
opeButt[4]=new JButton("sqr");
opeButt[5]=new JButton("pow");
opeButt[6]=new JButton("+/-");
opeButt[7]=new JButton("=");
for(int i=0;i<12;i++){
numPa.add(numButt[i]);
}
ButtonListener bu=new ButtonListener();
for(int i=0;i<12;i++){
numButt[i].addActionListener(bu);
}
for(int i=0;i<8;i++){
opePa.add(opeButt[i]);
}
for(int i=0;i<8;i++){
opeButt[i].addActionListener(bu);
}
c.add(text,BorderLayout.NORTH);
c.add(numPa,BorderLayout.WEST);
c.add(opePa,BorderLayout.EAST);
pack();
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new MyCalculater();
}
class ButtonListener implements ActionListener{
private String s2="";
private boolean deter=false;
private double total=0.0;
private String totalStirng="";
int flag=0;
public void actionPerformed(ActionEvent e) {
String s1=text.getText();
String s=e.getActionCommand();
if(s.equals(".")){
text.setText(s1+s);
}
else if(s.equals("+/-")){
text.setText("-"+s1);
}
else if(Character.isDigit(s.charAt(0))){
if(flag==1){
text.setText(s);
flag--;
}else{
text.setText(s1+s);
}
}
else if(s.equals("CE")){
text.setText("0");
total=0.0;
totalStirng="";
s1="0";
s2="";
s="";
flag=0;
deter=false;
}
else {
if(!deter){
deter=true;
totalStirng=text.getText();
flag++;
s2=s;
}
else {
double s0=Double.parseDouble(totalStirng);
double s10=Double.parseDouble(s1);
if(s2.equals("+")){
total=s0+s10;
}
else if(s2.equals("-")){
total=s0-s10;
}
else if(s2.equals("x")){
total=s0*s10;
}
else if(s2.equals("/")){
total=s0/s10;
}
else if(s2.equals("pow")){
total=Math.pow(s0, s10);
}
else if(s2.equals("sqr")){
total=Math.sqrt(s10);
}
totalStirng=Double.toString(total);
text.setText(totalStirng);
s2=s;
if(flag==0){
flag++;
}
}
}
}
}
}
import javax.swing.JButton;
import javax.swing.BorderFactory;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JFrame;
public class MyCalculater extends JFrame {
JTextField text;
public MyCalculater(){
text=new JTextField(10);
text.setEditable(false);
text.setBorder(BorderFactory.createLineBorder(Color.red,2));
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel numPa=new JPanel();
JPanel opePa=new JPanel();
Container c=getContentPane();
numPa.setLayout(new GridLayout(4,3,2,2));
opePa.setLayout(new GridLayout(4,1,2,2));
JButton numButt[]=new JButton[12];
JButton opeButt[]=new JButton[8];
for(int i=0;i<10;i++){
numButt[i]=new JButton(new Integer(i).toString());
}
numButt[10]=new JButton(".");
numButt[11]=new JButton("CE");
opeButt[0]=new JButton("+");
opeButt[1]=new JButton("-");
opeButt[2]=new JButton("x");
opeButt[3]=new JButton("/");
opeButt[4]=new JButton("sqr");
opeButt[5]=new JButton("pow");
opeButt[6]=new JButton("+/-");
opeButt[7]=new JButton("=");
for(int i=0;i<12;i++){
numPa.add(numButt[i]);
}
ButtonListener bu=new ButtonListener();
for(int i=0;i<12;i++){
numButt[i].addActionListener(bu);
}
for(int i=0;i<8;i++){
opePa.add(opeButt[i]);
}
for(int i=0;i<8;i++){
opeButt[i].addActionListener(bu);
}
c.add(text,BorderLayout.NORTH);
c.add(numPa,BorderLayout.WEST);
c.add(opePa,BorderLayout.EAST);
pack();
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new MyCalculater();
}
class ButtonListener implements ActionListener{
private String s2="";
private boolean deter=false;
private double total=0.0;
private String totalStirng="";
int flag=0;
public void actionPerformed(ActionEvent e) {
String s1=text.getText();
String s=e.getActionCommand();
if(s.equals(".")){
text.setText(s1+s);
}
else if(s.equals("+/-")){
text.setText("-"+s1);
}
else if(Character.isDigit(s.charAt(0))){
if(flag==1){
text.setText(s);
flag--;
}else{
text.setText(s1+s);
}
}
else if(s.equals("CE")){
text.setText("0");
total=0.0;
totalStirng="";
s1="0";
s2="";
s="";
flag=0;
deter=false;
}
else {
if(!deter){
deter=true;
totalStirng=text.getText();
flag++;
s2=s;
}
else {
double s0=Double.parseDouble(totalStirng);
double s10=Double.parseDouble(s1);
if(s2.equals("+")){
total=s0+s10;
}
else if(s2.equals("-")){
total=s0-s10;
}
else if(s2.equals("x")){
total=s0*s10;
}
else if(s2.equals("/")){
total=s0/s10;
}
else if(s2.equals("pow")){
total=Math.pow(s0, s10);
}
else if(s2.equals("sqr")){
total=Math.sqrt(s10);
}
totalStirng=Double.toString(total);
text.setText(totalStirng);
s2=s;
if(flag==0){
flag++;
}
}
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
和你大概说一下思路吧!这样的代码百度一下很多的!
先创建一个Container这个的对象!这个就相当于是一个容器,然后使用布局管理器推荐使用网格布局!计算器么上面肯定要有文本框显示的!new一个JtestField的对象放入容器中,然后就用事件监听,监听窗体上面的每一个按键!
先创建一个Container这个的对象!这个就相当于是一个容器,然后使用布局管理器推荐使用网格布局!计算器么上面肯定要有文本框显示的!new一个JtestField的对象放入容器中,然后就用事件监听,监听窗体上面的每一个按键!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
网上很多现成代码,稍微搜索一下就有了,无需提问。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询