java运算器代码 java算术运算符代码( 二 )


private JTextField jtf;
public Calculator() {
//对图形组件实例化
jf=new JFrame("任静的计算器1.0:JAVA版");
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(){
System.exit(0);
}
});
allButtons=new JButton[16];
clearButton=new JButton("清除");
jtf=new JTextField(25);
jtf.setEditable(false);
String str="123+456-789*0.=/";
for(int i=0;iallButtons.length;i++){
allButtons[i]=new JButton(str.substring(i,i+1));
}
}
public void init(){
//完成布局
jf.setLayout(new BorderLayout());
JPanel northPanel=new JPanel();
JPanel centerPanel=new JPanel();
JPanel southPanel=new JPanel();
northPanel.setLayout(new FlowLayout());
centerPanel.setLayout(new GridLayout(4,4));
southPanel.setLayout(new FlowLayout());
northPanel.add(jtf);
for(int i=0;i16;i++){
centerPanel.add(allButtons[i]);
}
southPanel.add(clearButton);
jf.add(northPanel,BorderLayout.NORTH);
jf.add(centerPanel,BorderLayout.CENTER);
jf.add(southPanel,BorderLayout.SOUTH);
addEventHandler();
}
//添加事件监听
public void addEventHandler(){
jtf.addActionListener(this);
for(int i=0;iallButtons.length;i++){
allButtons[i].addActionListener(this);
}
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Calculator.this.jtf.setText("");
}
});
}
//事件处理
public void actionPerformed(ActionEvent e) {
//在这里完成事件处理使计算器可以运行
String action=e.getActionCommand();
if(action=="+"||action=="-"||action=="*"||action=="/"){
}
}
public void setFontAndColor(){
Font f=new Font("宋体",Font.BOLD,24);
jtf.setFont(f);
jtf.setBackground(new Color(0x8f,0xa0,0xfb));
for(int i=0;i16;i++){
allButtons[i].setFont(f);
allButtons[i].setForeground(Color.RED);
}
}
public void showMe(){
init();
setFontAndColor();
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new Calculator().showMe();
}
}
求计算器Java代码 。。。import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
class JPanelEa1 extends JFrame implements MenuListener,ActionListener
{
JPanel mainp,p1,p2,p3,p4;
JTextField jt1;
JMenuItem mnuCopy;
JMenuItem mnuPaste;
JButton bM;
boolean isDouble = false;// 是否为实数
int opFlag = -1;
static double t1 = 0, t2 = 0, t3 = 0, result = 0;
static int opflag1 = -1, opflag2 = -1, flag = 0, resflag = 1;
int preOp, currentOp = 0;// 标准位
double op1 = 0, op2 = 0;// 操作数
double n3;
StringBuffer buf = new StringBuffer(20);
StringBuffer copyBoard = new StringBuffer(20);// 剪贴板
StringBuffer memory = new StringBuffer(20);// M系列
StringBuffer str = new StringBuffer();
public JPanelEa1()
{
p1=new JPanel();
p1.setLayout(new GridLayout(2,1,10,10));
JMenuBar mnuNotepad=new JMenuBar();
JMenu mnuEdit=new JMenu("编辑(E)");
mnuEdit.setMnemonic(KeyEvent.VK_E);
JMenu mnuCheck=new JMenu("查看(V)");
mnuCheck.setMnemonic(KeyEvent.VK_V);
JMenu mnuHelp=new JMenu("帮助(H)");
mnuCopy=new JMenuItem("复制(C)");
mnuPaste=new JMenuItem("粘贴(P)");
JMenuItem mnuVisit=new JMenuItem("查看帮助(V)");
JMenuItem mnuAbout=new JMenuItem("关于计算器(A)");
JSeparator sep=new JSeparator();
jt1=new JTextField("0.");
jt1.setEnabled(false);
jt1.setHorizontalAlignment(JTextField.RIGHT);

推荐阅读