Java计算器源代码.docx
《Java计算器源代码.docx》由会员分享,可在线阅读,更多相关《Java计算器源代码.docx(37页珍藏版)》请在冰豆网上搜索。
Java计算器源代码
Java计算器源代码
该计算器有两个主要部分,标准计算器和科学计算器,科学计算器功能尚未完成。
用的知识:
按钮,菜单,布局,面板,窗口,事件,对话框,标签,单选钮。
有兴趣的读者可以完成剩下的功能。
组成:
三个源程序:
Cal.java,BPanel.java,KPanel.java
//////////////////////////////////////////////////////////////////
////////////////////Cal.java/////////////////////////////////
///////////////////////////////////////////////////////////////////packagecal;
importjava.awt.CardLayout;
importjava.awt.Color;
importjava.awt.Dimension;
importjava.awt.FlowLayout;
importjava.awt.Font;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.InputEvent;
importjava.awt.event.ItemEvent;
importjava.awt.event.ItemListener;
importjava.awt.event.KeyEvent;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjavax.swing.ButtonGroup;
importjavax.swing.ImageIcon;
importjavax.swing.JDialog;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JMenu;
importjavax.swing.JMenuBar;
importjavax.swing.JMenuItem;
importjavax.swing.JPanel;
importjavax.swing.JRadioButtonMenuItem;
importjavax.swing.KeyStroke;
publicclassCalextendsJFrame{
/**
*
*/
privatestaticfinallongserialVersionUID=1L;
privateJMenuBarmb=null;
privateJMenum1=null;
privateJMenum2=null;
privateJMenum4=null;
privateJMenuItemmt1=null;
publicstaticJMenuItemmt2=null;
privateJRadioButtonMenuItemmt3=null;
privateJRadioButtonMenuItemmt4=null;
privateJMenuItemmt6=null;
privateJMenuItemmt7=null;
privateBPanelbp=newBPanel();
privateKPanelkp=newKPanel();
privateJPanelp=newJPanel();
privatestaticbooleancard=true;
publicstaticbooleanfz=false;
publicstaticStringstr=newString();
privateJPanelp1=newJPanel();
privateCardLayoutc1=newCardLayout();
privateJLabelb=newJLabel();
staticCalcalc;
finalJDialogd1=newJDialog();
finalJDialogd2=newJDialog();
publicstaticColorbc=newColor(255,204,204);
publicstaticColorqc=Color.cyan;
privateFontf=newFont("宋体",Font.BOLD,15);
privateCal(){
super("计算器");
this.setLocation(400,250);
this.setForeground(qc);
this.setBackground(bc);
this.setIconImage(newImageIcon("jsq.jpg").getImage());
JFrame.setDefaultLookAndFeelDecorated(true);
p=(JPanel)this.getContentPane();
p.setLayout(newFlowLayout());
p.add(b);
p.add(p1);
p1.setLayout(c1);
p1.add("card1",bp);
p1.add("card2",kp);
this.setJMenuBar(getMb());
initDialog1();
initDialog2();
this.pack();
this.setResizable(false);
this.setVisible(true);
}
protectedvoidprocessWindowEvent(WindowEvente){
if(e.getID()==WindowEvent.WINDOW_CLOSING)
System.exit(0);
}
privateJMenuBargetMb(){
mb=newJMenuBar();
mb.setPreferredSize(newDimension(110,35));
mb.setForeground(qc);
mb.setBackground(bc);
mb.add(getM1());
mb.add(getM2());
mb.add(getM4());
returnmb;
}
privateJMenugetM1(){
m1=newJMenu("编辑(F)");
m1.setFont(f);
m1.setBackground(bc);
m1.add(getMt1());
m1.add(getMt2());
returnm1;
}
privateJMenugetM2(){
m2=newJMenu("类型(L)");
m2.setFont(f);
m2.setBackground(bc);
m2.add(getMt3());
m2.add(getMt4());
ButtonGroupbg=newButtonGroup();
bg.add(mt3);
mt3.setSelected(true);
bg.add(mt4);
returnm2;
}
privateJMenugetM4(){
m4=newJMenu("帮助(H)");
m4.setFont(f);
m4.setBackground(bc);
m4.add(getMt6());
m4.add(getMt7());
returnm4;
}
privateJMenuItemgetMt1(){
mt1=newJMenuItem("复制");
mt1.setFont(f);
mt1.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_C,InputEvent.CTRL_MASK));
mt1.setPreferredSize(newDimension(130,30));
mt1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
try{
if(card)
{
if(bp.getTextField().getSelectedText().isEmpty())
str=bp.getTextField().getText();
elsestr=bp.getTextField().getSelectedText();
}
}catch(Exceptiontx){
str=bp.getTextField().getText();
}
if(!
str.isEmpty()&&!
str.equals("0"))
{mt2.setEnabled(true);
BPanel.jp2.setEnabled(true);}
}
});
returnmt1;}
privateJMenuItemgetMt2(){
mt2=newJMenuItem("粘贴");
mt2.setFont(f);
mt2.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_V,InputEvent.CTRL_MASK));
mt2.setPreferredSize(newDimension(130,30));
mt2.setEnabled(false);
mt2.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
if(card)
{bp.getTextField().setText(str);
fz=true;
}
//esle
//kp.getTextField().setText(str);
}
});
returnmt2;}
privateJRadioButtonMenuItemgetMt3(){
mt3=newJRadioButtonMenuItem("标准计算器");
mt3.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_B,InputEvent.CTRL_MASK));
mt3.setFont(f);
mt3.setPreferredSize(newDimension(130,30));
mt3.addItemListener(newItemListener(){
publicvoiditemStateChanged(ItemEvente){
c1.first(p1);
}
});
returnmt3;}
privateJRadioButtonMenuItemgetMt4(){
mt4=newJRadioButtonMenuItem("科学计算器");
mt4.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_K,InputEvent.CTRL_MASK));
mt4.setFont(f);
mt4.setPreferredSize(newDimension(130,30));
mt4.addItemListener(newItemListener(){
publicvoiditemStateChanged(ItemEvente){
c1.last(p1);
}
});
returnmt4;}
privateJMenuItemgetMt6(){
mt6=newJMenuItem("关于计算器");
mt6.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_G,InputEvent.CTRL_MASK));
mt6.setFont(f);
mt6.setPreferredSize(newDimension(130,30));
mt6.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
d1.setVisible(true);
}
});
returnmt6;}
privateJMenuItemgetMt7(){
mt7=newJMenuItem("帮助");
mt7.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_H,InputEvent.CTRL_MASK));
mt7.setFont(f);
mt7.setPreferredSize(newDimension(130,30));
mt7.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
d2.setVisible(true);
}
});
returnmt7;}
publicvoidinitDialog1(){
d1.setTitle("关于计算器");
JDialog.setDefaultLookAndFeelDecorated(true);
Strings1="计算器由标准计算器和科学计算器组成";
Strings2="j2se版本:
jdk1.6。
本人QQ:
1286908118";
Fontfont=newFont("宋体",Font.BOLD,20);
JLabell1=newJLabel(s1);
JLabell2=newJLabel(s2);
JPaneldp=(JPanel)d1.getContentPane();
dp.setLayout(newGridLayout(2,1));
dp.setBackground(Color.pink);
dp.setForeground(Color.pink);
l1.setForeground(Color.blue);
l1.setFont(font);
dp.add(l1);
l2.setForeground(Color.red);
l2.setFont(font);
dp.add(l2);
d1.setSize(400,200);
d1.setFont(f);
d1.setLocation(500,300);
d1.setResizable(false);
d1.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
d1.setVisible(false);
}
});
}
publicvoidinitDialog2(){
d2.setTitle("帮助");
Strings1="复制快捷键为ctrl+c";
Strings2="粘贴快捷键为ctrl+v";
Fontfont=newFont("宋体",Font.BOLD,20);
JLabell1=newJLabel(s1);
JLabell2=newJLabel(s2);
JPaneldp=(JPanel)d2.getContentPane();
dp.setLayout(newGridLayout(2,1));
dp.setBackground(Color.pink);
dp.setForeground(Color.pink);
l1.setFont(font);
l1.setForeground(Color.blue);
dp.add(l1);
l2.setFont(font);
l1.setForeground(Color.blue);
dp.add(l2);
d2.setSize(newDimension(400,240));
d2.setResizable(false);
d2.setFont(f);
d2.setLocation(500,300);
JDialog.setDefaultLookAndFeelDecorated(true);
d2.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
d2.setVisible(false);
}
});
}
publicstaticvoidmain(String[]args){
calc=newCal();
}
}
//////////////////////////////////////////////////////////////////
////////////////////BPanel.java//////////////////////////////
///////////////////////////////////////////////////////////////////
packagecal;
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Dimension;
importjava.awt.Font;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.InputEvent;
importjava.awt.event.KeyEvent;
importjava.awt.event.MouseAdapter;
importjava.awt.event.MouseEvent;
importjavax.swing.JButton;
importjavax.swing.JMenuItem;
importjavax.swing.JPanel;
importjavax.swing.JPopupMenu;
importjavax.swing.JTextField;
importjavax.swing.KeyStroke;
importjavax.swing.SwingUtilities;
publicclassBPanelextendsJPanelimplementsActionListener{
/**
*
*/
privatestaticfinallongserialVersionUID=1L;
JTextFieldtf=null;
privateJButtons1=null;
privateJButtons2=null;
privateJButtons3=null;
privateJButtons4=null;
privateJButtons5=null;
privateJButtons6=null;
privateJButtons7=null;
privateJButtons8=null;
privateJButtons9=null;
privateJButtons0=null;
privateJButtonf1=null;
privateJButtonf2=null;
privateJButtonf3=null;
privateJButtonf4=null;
privateJButtonf5=null;
privateJButtonf6=null;
privateJButtonc1=null;
privateJButtonc2=null;
privateJButtond1=null;
privateJButtond2=null;
privateJPopupMenujpm=null;
privateJMenuItemjp1=null;
publicstaticJMenuItemjp2=null;
privateJPanelpp=newJPanel();
privatestaticbooleanadd=false,
sub=false,mul=false,
div=false,qy=false,sz=false,jg=false,
v1=false,v2=false;
privatestaticdoublevalue1=0d;
privatestaticdoublevalue2=0d;
privateintnum=0;
privateFontf=newFont("宋体",Font.BOLD,16);
privateFontsign=newFont("宋体",Font.BOLD,15);
privateColorc=newColor(10,170,10);
publicBPanel(){//BPanel的构造方法
initButton();
initPopupMenu();
initTextField();
this.setLayout(newBorderLayout());
this.add("North",tf);
this.add