Java計算器原始碼該計算器有兩個主要部分,標準計算器和科學計算器,科學計算器功能尚未完成。用的知識:按鈕,選單,布局,面板,視窗,事件,對話框,標籤,單選鈕。對Java感興趣,想學習Java不知道怎么學,從事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();