JAVA程序用netbeans写一个计算器的程序,代码大概一百多行 50

是用netbeans的吗?还要有图形界面呢,可以用键盘输入数字也可以用鼠标点的... 是用netbeans的吗?还要有图形界面呢,可以用键盘输入数字也可以用鼠标点的 展开
 我来答
我是谁我在哪我要干什么呢
2012-05-31 · TA获得超过560个赞
知道小有建树答主
回答量:350
采纳率:100%
帮助的人:370万
展开全部
以前写的,很乱。仅供参考
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
public class T implements ActionListener{
private int append = 1;
JTextField jtf = new JTextField(10);
String s1="";
String s2="";
int n=0;
ArrayList<String> aList1=new ArrayList<String>();
ArrayList<String> aList2=new ArrayList<String>();
public T(){
JFrame jf = new JFrame("计算器");
jf.add(jtf,BorderLayout.NORTH);
jf.setSize(200,100 );
JPanel jp = new JPanel();
GridLayout gl = new GridLayout(5,4);
jp.setLayout(gl);

String[] lab = {"BackS","CE","C","+",
"7","8","9","-","4","5","6","*",
"1","2","3","/","0",".","+/-","="};
for(int i=0;i<lab.length;i++){
JButton jb = new JButton(lab[i]);
jp.add(jb);
jb.addActionListener(this);
}
jf.add(jp);
jf.setSize(300,300);
jtf.setEditable(false);
jf.setResizable(false);
jf.setLocation(300, 200);jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
String num1 = "0";
String operator = "+";
public void actionPerformed(ActionEvent ae) {
String comm = ae.getActionCommand();
if("0123456789".indexOf(comm)!=-1){
if(append==1){
n=0;
s2=s2+comm;
s1=s1+comm;
}
else{
aList1.clear();
append=1;
s2=s2+comm;
s1=comm;
}
jtf.setText(s1);
}else if("+-*/".indexOf(comm)!=-1){
n++;
if(n==1){
s1=s1+comm;
aList2.add(comm);
}
if(append==0)
append=1;
if(s2!=""){
aList1.add(s2);
}
s2="";
jtf.setText(s1);
}else if("=".equals(comm)){
aList1.add(s2);
double d1;
double d2;
int i=0;
while(aList2.contains("*")||aList2.contains("/")){
int flag=0;
if(aList2.size()>0&&"*".equals(aList2.get(i))){
d1 = Double.parseDouble(aList1.get(i));
d2 = Double.parseDouble(aList1.get(i+1));
d1=d1*d2;
aList1.remove(i);
System.out.println("RE:"+aList1);
aList1.set(i,d1+"");
System.out.println("Set:"+aList1);
aList2.remove(i);
System.out.println(aList2);
flag++;
System.out.println("*"+i);
}
if(i==aList2.size())
i--;
if(aList2.size()>0&&"/".equals(aList2.get(i))){
System.out.println("/"+i);
d1 = Double.parseDouble(aList1.get(i));
d2 = Double.parseDouble(aList1.get(i+1));
d1=d1/d2;
aList1.remove(i);
aList1.set(i,d1+"");
aList2.remove(i);
flag++;
System.out.println("/"+aList2);
}
if(flag==0)
i++;
}
while(aList2.contains("+")||aList2.contains("-")){
i=0;
if("+".equals(aList2.get(i))){
d1 = Double.parseDouble(aList1.get(i));
d2 = Double.parseDouble(aList1.get(i+1));
d1=d1+d2;
aList1.remove(i);
System.out.println("Re"+aList1);
aList1.set(i,d1+"");
System.out.println("Set:"+aList1);
aList2.remove(i);
System.out.println("alist2:"+aList2);
System.out.println(aList2.size());
}
if(aList2.size()>0&&"-".equals(aList2.get(i))){
d1 = Double.parseDouble(aList1.get(i));
d2 = Double.parseDouble(aList1.get(i+1));
d1=d1-d2;
aList1.remove(i);
aList1.set(i,d1+"");
aList2.remove(i);
}
}
s1=s1+comm+aList1.get(aList1.size()-1);
jtf.setText(s1);
s2="";
s1=aList1.get(aList1.size()-1);
append = 0;
}else if(".".equals(comm)){
String temp=s2;
if(temp.indexOf(".")==-1){
s1=s1+comm;
jtf.setText(s1);
s2=s2+comm;
append = 1;
}
}else if("+/-".equals(comm)){
String temp = jtf.getText();
if(temp.startsWith("-")){
jtf.setText(temp.substring(1));
}else{
jtf.setText("-"+temp);
}
}else if("CE".equals(comm)||"C".equals(comm)){
jtf.setText("0");
s2="";
s1="";
append = 0;
}else if("BackS".equals(comm)){
String temp = s1;
if(temp.length()>0){
if(s1.endsWith("+")||s1.endsWith("-")||s1.endsWith("*")||s1.endsWith("/")){
System.out.print("不可再移除!");
return ;
}
jtf.setText(temp.substring(0,
temp.length()-1));
s1=s1.substring(0, s1.length()-1);
s2=s2.substring(0,s2.length()-1);
}
else{
System.out.println("没有可以移除的!");
}
}
}
public static void main(String[] args) {
new T();
}
}
特种兵9043
2012-06-01
知道答主
回答量:38
采纳率:0%
帮助的人:10.1万
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator
{
public static void main(String[] args)
{
CalculatorFrame frame=new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
/* *
带有计算器面板的JFrame窗口
*/
class CalculatorFrame extends JFrame
{
public CalculatorFrame()
{
setTitle("Calculator");
Container contentPane=getContentPane();
CalculatorPanel panel=new CalculatorPanel();
contentPane.add(panel);
setSize(300,300);
}
}
/* *
带有计算器按钮和一个输出显示栏的JPanel面板
*/
class CalculatorPanel extends JPanel
{
private JLabel display;
private JPanel panel;
private double result;
private String lastCommand;
private boolean start;
public CalculatorPanel()
{
setLayout(new BorderLayout());
result=0;
lastCommand="=";
start=true;
//添加显示标签栏
display=new JLabel("0");
add(display,BorderLayout.NORTH);
ActionListener insert=new InsertAction();
ActionListener command=new CommandAction();
//在4x4网格中添加数字和运算符按钮
panel=new JPanel();
panel.setLayout(new GridLayout(4,4));
addButton("7",insert);
addButton("8",insert);
addButton("9",insert);
addButton("/",command);
addButton("4",insert);
addButton("5",insert);
addButton("6",insert);
addButton("*",command);
addButton("1",insert);
addButton("2",insert);
addButton("3",insert);
addButton("-",command);
addButton("0",insert);
addButton(".",insert);
addButton("=",command);
addButton("+",command);
add(panel,BorderLayout.CENTER);
}
private void addButton(String label,ActionListener listener)
{
JButton button=new JButton(label);
button.addActionListener(listener);
panel.add(button);
}
private class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String input=event.getActionCommand();
if(start)
{
display.setText("");
start=false;
}
display.setText(display.getText()+input);
}
}
private class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
String command=evt.getActionCommand();
if(start)
{
lastCommand=command;
}
else
{
calculate(Double.parseDouble(display.getText()));
lastCommand=command;
start=true;
}
}
}
public void calculate(double x)
{
if(lastCommand.equals("+"))result+=x;
else if(lastCommand.equals("-"))result-=x;
else if(lastCommand.equals("*"))result*=x;
else if(lastCommand.equals("/"))result/=x;
else if(lastCommand.equals("="))result=x;
display.setText(""+result);
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式