Original: Java Swing development of the simplest calculator source code
Source code: Http://www.zuidaima.com/share/1550463496948736.htm
A simple calculator written in Java Swing
/*** @author www.zuidaima.com*/package com.zuidaima.swing;import java.awt.borderlayout;import java.awt.EventQueue; Import Javax.swing.jframe;import Javax.swing.jpanel;import Javax.swing.border.emptyborder;import Javax.swing.jmenubar;import Javax.swing.jmenu;import Javax.swing.jmenuitem;import Javax.swing.KeyStroke;import Java.awt.event.keyevent;import Java.awt.event.inputevent;import Javax.swing.jseparator;import Javax.swing.jtextfield;import Javax.swing.swingconstants;import Javax.swing.jbutton;import Java.awt.event.actionlistener;import Java.awt.event.actionevent;public class Jsqframe extends JFrame{private jpanelcontentpane;/** * Calculation Results */private JTextField txtjisuanjieguo;/** * First number */double shua;/** * Second number */double SHUB;DOUBL E shu;//f/** * Launch the application. */public static void Main (string[] args) {Eventqueue.invokelater (new Runnable () {public void run () {Try{jsqframe frame = New Jsqframe (); frame.setvisible (true);} catch (Exception e) {e.printstacktrace ();}}});} /** * Create the FramE. */public Jsqframe () {setresizable (false); Settitle ("\u8ba1\u7b97\u5668"); Setdefaultcloseoperation (JFrame.EXIT_ On_close); setbounds (100, 100, 362, 347); JMenuBar MenuBar = new JMenuBar (); Setjmenubar (MenuBar); JMenu mnnewmenu = new JMenu ("\u7f16\u8f91 (e)"); Mnnewmenu.setmnemonic (' E '); Menubar.add (Mnnewmenu); JMenuItem Mntmnewmenuitem = new JMenuItem ("\u590d\u5236 (C)"); Mntmnewmenuitem.setaccelerator (KeyStroke.getKeyStroke (Keyevent.vk_c, Inputevent.ctrl_mask)); Mnnewmenu.add (Mntmnewmenuitem); JMenuItem mntmnewmenuitem_1 = new JMenuItem ("\u7c98\u8d34 (V)"); Mntmnewmenuitem_1.setaccelerator ( Keystroke.getkeystroke (Keyevent.vk_v, Inputevent.ctrl_mask)); Mnnewmenu.add (mntmnewmenuitem_1); JMenu mnnewmenu_1 = new JMenu ("\u67e5\u770b"); Menubar.add (mnnewmenu_1); JMenuItem mntmnewmenuitem_3 = new JMenuItem ("\u6807\u51c6\u578b (T)"); Mntmnewmenuitem_3.setaccelerator ( Keystroke.getkeystroke (keyevent.vk_t, 0)); Mnnewmenu_1.add (mntmnewmenuitem_3); JMenuItem mntmnewmenuitem_4 = new JMenuItem ("\u79d1\u5b66\u578b (S)"); MntmNewmenuitem_4.setaccelerator (Keystroke.getkeystroke (keyevent.vk_s, 0)); Mnnewmenu_1.add (MntmNewMenuItem_4); Jseparator separator_1 = new Jseparator (); Mnnewmenu_1.add (separator_1); JMenuItem mntmnewmenuitem_5 = new JMenuItem ("\u6570\u5b57\u5206\u7ec4 (I)"); Mntmnewmenuitem_5.setaccelerator ( Keystroke.getkeystroke (keyevent.vk_i, 0)); Mnnewmenu_1.add (mntmnewmenuitem_5); JMenu mnnewmenu_2 = new JMenu ("\u5e2e\u52a9 (h)"); Mnnewmenu_2.setmnemonic (' H '); Menubar.add (mnnewmenu_2); JMenuItem mntmnewmenuitem_6 = new JMenuItem ("\u5e2e\u52a9\u4e3b\u9898 (H)"); Mntmnewmenuitem_6.setaccelerator ( Keystroke.getkeystroke (keyevent.vk_h, 0)); Mnnewmenu_2.add (mntmnewmenuitem_6); Jseparator separator = new Jseparator (); Mnnewmenu_2.add (separator); JMenuItem mntmnewmenuitem_7 = new JMenuItem ("\u5173\u4e8e\u8ba1\u7b97\u5668 (A)"); Mntmnewmenuitem_7.setaccelerator ( Keystroke.getkeystroke (keyevent.vk_a, 0)); Mnnewmenu_2.add (mntmnewmenuitem_7); contentpane = new JPanel (); Contentpane.setborder (New Emptyborder (5, 5, 5, 5)); Setcontentpane(ContentPane); contentpane.setlayout (null); Txtjisuanjieguo = new JTextField (); Txtjisuanjieguo.sethorizontalalignment (swingconstants.right); Txtjisuanjieguo.settooltiptext ("\u8BA1\u7B97\ u7ed3\u679c "); Txtjisuanjieguo.settext (" 0. "); Txtjisuanjieguo.setbounds (0, 334), Contentpane.add (Txtjisuanjieguo); Txtjisuanjieguo.setcolumns (20); JButton btn7 = new JButton ("7"); Btn7.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent arg0) {Txtjisuanjieguo.settext ("7");}}); Btn7.setbounds (Contentpane.add) (BTN7); JButton Btnchu = new JButton ("4"); Btnchu.addactionlistener (new ActionListener () {public void actionperformed ( ActionEvent arg0) {txtjisuanjieguo.settext ("4");}}); Btnchu.setbounds (119), Contentpane.add (Btnchu); JButton btn1 = new JButton ("1"); Btn1.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent arg0) {//todo Press 1txtjisuanjieguo.settext ("1");}); Btn1.setbounds (178), Contentpane.add (BTN1); JButton btn0 = NEW JButton ("0"); Btn0.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent arg0) { Txtjisuanjieguo.settext ("0");}); Btn0.setbounds (236), Contentpane.add (btn0); JButton btn8 = new JButton ("8"); Btn8.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent arg0) {Txtjisuanjieguo.settext ("8");}}); Btn8.setbounds (102, (), Contentpane.add (BTN8); JButton btn5 = new JButton ("5"); Btn5.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent arg0) {Txtjisuanjieguo.settext ("5");}}); Btn5.setbounds (102, 119, N, Notoginseng); Contentpane.add (BTN5); JButton btn2 = new JButton ("2"); Btn2.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent arg0) {Txtjisuanjieguo.settext ("2");}}); Btn2.setbounds (102, 178, N, Notoginseng); Contentpane.add (BTN2); JButton Btnzhengyufu = new JButton ("+/-") btnzhengyufu.setbounds (102, 236, vol, Notoginseng); Contentpane.add (Btnzhengyufu); JButton btn9 = new JButton ("9"); Btn9.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent arg0) {txtjisuanjieguo.settext ("9");}}); Btn9.setbounds (164, (), Contentpane.add (BTN9); JButton btn6 = new JButton ("6"); Btn6.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent arg0) {Txtjisuanjieguo.settext ("6");}}); Btn6.setbounds (164, 119, N, Notoginseng); Contentpane.add (BTN6); JButton btn3 = new JButton ("3"); Btn3.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent arg0) {Txtjisuanjieguo.settext ("3");}}); Btn3.setbounds (164, 178, N, Notoginseng); Contentpane.add (BTN3); JButton Btndian = new JButton ("."); Btndian.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent arg0) { Txtjisuanjieguo.settext ("" +txtjisuanjieguo.gettext () + ".");}); Btndian.setbounds (164, 236, N, Notoginseng); Contentpane.add (Btndian); JButton button_11 = new JButton ("/"); Button_11.setbounds (226, +, n); Contentpane.add (Button_11); JButton Btn7cheng = new JButton ("*"); Btn7cheng.setbounds (226, 119, 52, Notoginseng); Contentpane.add (Btn7cheng); JButton Btnjian = new JButton ("-") btnjian.setbounds (226, 178, N, Notoginseng); Contentpane.add (Btnjian); JButton Btnjia = new JButton ("+"); Btnjia.addactionlistener (new ActionListener () {public void actionperformed ( ActionEvent arg0) {//+++++txtjisuanjieguo.settext ("" +txtjisuanjieguo.gettext () + "+");}}); Btnjia.setbounds (226, 236, N, Notoginseng); Contentpane.add (Btnjia); JButton Btnquyu = new JButton ("%"); Btnquyu.setbounds (288, 119, N, Notoginseng); Contentpane.add (Btnquyu); JButton Btndaoshu = new JButton ("1/x"); Btndaoshu.setbounds (288, 178, N, Notoginseng); Contentpane.add (Btndaoshu); JButton Btndeng = new JButton ("="); Btndeng.setbounds (288, 236, N, Notoginseng); Contentpane.add (Btndeng); JButton btnsqrt = new JButton ("Sqr"), Btnsqrt.setbounds (288, (), Contentpane.add (BTNSQRT); JButton btnclear = new JButton ("clear"); Btnclear.addactionlistener (new ActionListener () {public void actionperformed ( ActionEvent arg0) {//todo empty Txtjisuanjieguo.settext ("0.");}); Btnclear.setbounds (251, +,,); contentPane.add (Btnclear);}}
Java Swing Development simplest calculator source code