Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Calculator Mini Program with java gui

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge of this article "how to achieve calculator Mini Program with java gui", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to achieve Mini Program Calculator with java gui" article.

Effect picture:

Code:

Package gui; / * Import the required package * * / import java.awt.*; / / this is a very important package in java's gui programming, import java.awt.event.*; / / used to handle event import java.util.Stack. / / stack, public class Calculator extends Frame implements ActionListener {/ *, which I use to handle operations, first declares a public class called Calculator, which inherits from the Frame class, and implements the ActionListener interface function * * / private static final long serialVersionUID = 1L; / / this is used to control the serialization of the version int frame_width = 1000 frame height = 400 / / set the length and width of the whole frame Panel panel_textfield,panel_number,panel_op,panel_other; / / the whole calculator layout I divided it into two panels, one is the upper input box area, the other is the lower button area, and then the button area is divided into left and right areas, so there are three panel Button [] number_buttons / / declare the number button (that is, the left side mentioned above) Button [] op_buttons; / / declare the operator button (that is, the right side) TextField textfield; / / input box public Calculator () {super ("calculator") / / initialize the instance domain parameters. The statement calling the constructor can only appear init () as the first statement of another constructor (usually a subclass constructor); / / the initialization method setLayout () written by yourself; / / set the layout management mode setBackground (); / / set the background setBounds () / / set location setFonts (); / / set font addButtons (); / / add button textfield.setEditable (false) / / set the input box to not be edited manually. You can only enter * / addWindowListener / / to add a window listener through the button to close the window when you press the close button, otherwise you can only stop debugging in ide to close the program (new WindowAdapter () {public void windowClosing (WindowEvent e) {System.exit (0)). }}); setVisible (true); / / set the frame to be visible, otherwise you can't see it if you draw the frame. Be sure to put it at the back and in front, if there are any changes to the form, you have to zoom and stretch the form to draw the weight of the window in order to have an effect. I've been stuck here for a long time. } public void init () {panel_textfield = new Panel (); / / instantiate a panel panel_number = new Panel (); / / instantiate a panel panel_op = new Panel (); / / instantiate a panel panel_other = new Panel (); / / instantiate a panel textfield = new TextField (frame_width) / instantiate a text input box setResizable (false); / / set the entire form to a non-scalable stretch add (panel_textfield); / / add an input box panel add (panel_other) to the form; / / add a button panel panel_textfield.add (textfield) to the form / / add input box panel_other.add (panel_number) in the input box panel; / / add number button panel panel_other.add (panel_op) in the following panel; / / add operator button panel} public void setLayout () {setLayout (new GridLayout) / / set the form layout to grid layout, 2 / 1 grid, the spacing between grids is 4 pixels panel_textfield.setLayout (null); / / input box panel is only one component, so set null panel_other.setLayout (new GridLayout) / / the lower panel is divided into the left numeric area and the right operator area, so the grid layout of 1x2 is set, with a spacing of 4 pixels panel_number.setLayout (new GridLayout); / / the digital area layout is set to the grid layout panel_op.setLayout (new GridLayout). / / Grid layout mode with operator area set to 3x1} public void setBackground () {/ / set background, there is nothing to say. Panel_textfield.setBackground (Color.red); panel_number.setBackground (Color.green); panel_op.setBackground (Color.blue);} public void setBounds () {/ / set component location, there is nothing to say. SetBounds (0,0, frame_width, frame_height); textfield.setBounds (0,0, frame_width, frame_height / 2) } public void addButtons () {String [] titles1 = {"/", "*", "-", / / the label values of the numeric area button "7", "8", "9", "4", "5", "6", "1", "2", "3", "0", ".", "c"} String [] titles2 = {"x", "+", "="}; / / the label value of the operator area button number_buttons = new Button [15]; / / apply for 15 button objects op_buttons = new Button [3]; / / apply for 3 button objects for (int I = 0; I

< this.number_buttons.length; i++) { number_buttons[i] = new Button(titles1[i]); panel_number.add(number_buttons[i]); // 往数字区中添加按钮 number_buttons[i].addActionListener(this); // 按钮的事件监听器,处理方法为this,也就是下面重载的actionPerformed()方法,这个方法必须被重载 } for(int i = 0; i < this.op_buttons.length; i++) { op_buttons[i] = new Button(titles2[i]); // 往操作符区中添加按钮 panel_op.add(this.op_buttons[i]); op_buttons[i].addActionListener(this); // 按钮的事件监听器,处理方法为this,也就是下面重载的actionPerformed()方法,这个方法必须被重载 } } @Override // 对ActionListener接口的此方法进行重载 public void actionPerformed(ActionEvent e) { Button button = (Button) e.getSource(); // 获得按钮来源 /** * 如果是数字键和操作符 , 则直接显示 **/ for(int i = 0; i < 14; i++) { if(button == number_buttons[i] || button == op_buttons[1]) { textfield.setText(textfield.getText() + button.getLabel()); return; } } /** * 如果是c,则清空 **/ if(button == number_buttons[14]) { textfield.setText(""); return; } /** * 如果是回退按钮 , 则清除最近的一个字符 **/ if(button == op_buttons[0]) { String s = textfield.getText(); if(s.length() >

0) textfield.setText (s.substring (0, s.length ()-1)); return;} / * * if =, the result * * / if (button = = op_buttons [2]) {textfield.setText (getResult ()); return;}} public String getResult () {/ * result * / String s = textfield.getText () / / first get the input string String num = ""; Stack nums = new Stack (); Stack ops = new Stack (); / * use regex to separate operands and operators, and then use the stack to calculate the result * * / for (int I = 0; I < s.length ()) {String temp = s.charAt (I) + "" If (temp.matches ("[0-9]") | | temp.matches ("[.]") {num + = temp;} else if (temp.matches ("* +]") | | temp.matches ("[-]") | temp.matches ("[/]")) {if (! num.equals (")) nums.push (Double.parseDouble (num) If (ops.isEmpty () | | cmpLevel (temp,ops.peek () {ops.push (temp);} else {Double num1 = nums.pop (); Double num2 = nums.pop (); String op2 = ops.pop (); nums.push (compute (num2,num1,op2)); Imam;} num = "" }} while (! ops.isEmpty ()) {if (! num.equals (")) {nums.push (compute (nums.pop (), Double.parseDouble (num), ops.pop (); num =";} else {Double num1 = nums.pop (); Double num2 = nums.pop (); nums.push (compute (num2,num1,ops.pop () }} return nums.pop (). ToString ();} / * calculates the two operands according to the operator and returns the result * * / public Double compute (double num1,double num2,String op) {if (op.equals ("+")) {return num1 + num2;} else if (op.equals ("-")) {return num1-num2 } else if (op.equals ("*")) {return num1 * num2;} else return num1 / num2;} / * compare the priority of the two operators * * / public boolean cmpLevel (String S1 line string S2) {if ("+") | | s1.equals ("-") {return false;} else {if (s2.equals ("+") | | s2.equals ("-") return true; return false }} / * set font for each component * * / public void setFonts () {panel_number.setFont (new Font (Font.PLAIN,24)); panel_op.setFont (new Font (Font.PLAIN,24)); panel_other.setFont (new Font (Font.PLAIN,24)); textfield.setFont (new Font (Font.PLAIN,48)) } / * main method * * / public static void main (String [] args) {new Calculator ();}} the above is about "how java gui implements calculator Mini Program". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report