我想要一个用java的程序

只要java编的就可以不需要太复杂。最好有窗体什么的... 只要java编的就可以 不需要太复杂。最好有窗体什么的 展开
 我来答
kingmove713
2006-12-26 · 超过51用户采纳过TA的回答
知道小有建树答主
回答量:155
采纳率:0%
帮助的人:157万
展开全部
//呵呵,其实是我并不熟窗体这一方面,找人以前代码你看下,好像是网上下的
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class calu extends JFrame
{public calu() /*构造方法*/
{super("计数器");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initTextPanel(); /*文本框*/
initControlPanel(); /*控制键*/
initKeyPanel(); /*数字和运算符*/
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
pane.add(TextPanel,BorderLayout.NORTH);
pane.add(ControlPanel,BorderLayout.CENTER);
pane.add(KeyPanel,BorderLayout.SOUTH);
pack();
try
{UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception ex)
{;} /*设置Windons观感*/
SwingUtilities.updateComponentTreeUI(this);
setResizable(false);
setVisible(true);
}

private void initTextPanel() /*设置文本框*/
{ TextPanel=new JPanel();
TextPanel.setLayout(new FlowLayout());
Resultarea =new JTextField("0",25);
Resultarea.setEditable(false); /*设置不可编辑*/
Color color=Color.white;
Resultarea.setBackground(color); /*颜色*/
Resultarea.setHorizontalAlignment(JTextField.RIGHT); /*设置显示位置*/
TextPanel.add(Resultarea);
}

private void initControlPanel() /*设置控制键*/
{ControlPanel=new JPanel();
ControlPanel.setLayout(new GridLayout(1,3,4,4));
JButton b1=new JButton("Backspace"); /*Backspace键*/
b1.setFont(font1);
b1.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{String s1=Resultarea.getText();
int l=s1.length();
Resultarea.setText(s1.substring(0 ,l-1));
}
});

ControlPanel.add(b1);
JButton b2=new JButton("CE"); /*CE键*/
b2.setFont(font1);
b2.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{Resultarea.setText("0");
isNew=true;
}
});
ControlPanel.add(b2);

JButton b3=new JButton("C"); /*C键*/
b3.setFont(font1);
b3.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{Resultarea.setText("0");
pnum="";
operation="";
isNew=true;
}
});
ControlPanel.add(b3);
}

private void initKeyPanel() /*设置数字键和运算符键*/
{String key[] = {"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};
KeyPanel = new JPanel();
KeyPanel.setLayout(new GridLayout(4,5,4,4));
for(int i=0;i<20;i++)
{String label = key[i];
JButton b = new JButton(label);
b.setActionCommand(key[i]);
b.setFont(font2);
KeyPanel.add(b);
b.addActionListener(new ActionListener() /*无名监听器*/
{public void actionPerformed(ActionEvent e)
{Key_actionPerformed(e);
}
});
}
}
public void Key_actionPerformed(ActionEvent e) /*数字键和运算符键无名监听器*/
{String s=(e.getActionCommand());
String st=Resultarea.getText();
if(s.equals("0")) /*输入数为0*/
{if(st.equals("0"))
return;
else
{if(!isNew)
Resultarea.setText(st+"0");
else
Resultarea.setText("0");
}
isNew=false;
return;
}

if(s.equals("+/-")) /*输入数为+/-*/
{double k=Double.parseDouble(st);
{if(k!=0)
display(-k);

}
return;
}

if(s.equals("1")||s.equals("2")||s.equals("3")||s.equals("4")||s.equals("5")||s.equals("6")||s.equals("7")||s.equals("8")||s.equals("9")) /*输入1-9*/
{if(!isNew)
Resultarea.setText(st+s);
else
{ Resultarea.setText(s);
isNew=false;
}
return;
}

if(s.equals(".")) /*输入小数点*/
{if(Resultarea.getText().indexOf(".")==-1) /*不存在小数点*/
{if(isNew)
{Resultarea.setText("0.");
isNew=false;
}
else
Resultarea.setText(st+".");
}
return;
}

isNew=true; /*按下运算符,输入新的数*/
if(s.equals("="))
{compute(s);
operation="";
}
else
{if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/")) /*二目运算符号*/
{if(operation.equals(""))
{pnum=Resultarea.getText();
operation=s;
}
else
compute(s);
}
else /*一目运算*/
{//Count count1=new Count(Double.parseDouble(st));
if(s.equals("sqrt"))
{
// display(count1.sqrt());

}
else
{if(s.equals("1/x"))
{if(st.equals("0"))
{Resultarea.setText("除数不能为0.");
operation="";
pnum="";
}
else
; // display(count1.reciprocal()); /*求倒数*/
}
else
display(Double.parseDouble(st)/100); /*输入%,使当前显示的值除于100*/
}
}
}
}

private void compute(String s)
{if(s.equals("="))
{if(operation.equals(""))
return;
}
double data1=Double.parseDouble(pnum);
double data2=Double.parseDouble(Resultarea.getText());
// Count count2=new Count(data1,data2); /*加减乘除计算*/
if(operation.equals("+")) ;
//display((count2.plus()));
else
{if(operation.equals("-")) ;
// display((count2.minus()));
else
{if(operation.equals("*"))
display((count2.multiply()));
else
if(operation.equals("/"))
{if(data2==0)
{Resultarea.setText("除数不能为0");
operation="";
pnum="";
return;
}
else
display((count2.divide()));
}
}
}
operation=""; /*符号为当前符*/
pnum=Resultarea.getText();/*运算数为当前文本数*/
}

public void display(double result) /*显示输出方法*/
{int a=(int)result;
if(a==result)
Resultarea.setText(String.valueOf(a));
else
Resultarea.setText(String.valueOf(result));
if(Resultarea.getText().equals("NaN"))
Resultarea.setText("输入函数无效");
}

private JPanel TextPanel; /*文本框棉板*/
private JTextField Resultarea; /*文本框*/
private JPanel ControlPanel; /*控制键键面板*/
private JPanel KeyPanel; /*数字键和运算符键面板*/
private Font font1=new Font("Dialog",0, 10); /*控制键字体*/
private Font font2 = new Font("Dialog",0,10); /*数字键和符号键字体*/
private String pnum=""; /*前操作数*/
private boolean isNew=true; /*控制是否是新数*/
private String operation=""; /*运算符*/
}

class tester /*测试类*/
{
public static void main(String[] args)
{
new calu();
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式