23rd Java How to compile the source code share of the calculator

Source: Internet
Author: User

Hello everyone, today put a day off, bored I simply knocked the source code of the calculator, found that the previous things are almost forgotten, so while still a little memory, first try to knock back the feeling of the previous ....

Well, don't say much, first look at the original code:

First create a main class:

public class test{

public static void Main (string[] args) {

Calculator calc=new Calculator ();
Calc. Paintingframeui ();

}


}

In a few new listeners:

The first one:

Import javax.swing.*; (to lead the package, because the package name is too many, you can use "*" a stroke of ....) )
Import java.awt.event.*;


public class Stringlistener implements actionlistener{
============= Construction gets textfield===================
JTextField jtx = null;

Public Stringlistener (JTextField textarea) {
Jtx = textarea;
}
============= operator Listener ========================

public void actionperformed (ActionEvent e) {
String Command=e.getactioncommand ();
if (command.equals ("=")) {
String content = Jtx.gettext ();
int result1 = Content.indexof ("+");
int result2 = Content.indexof ("-");
int RESULT3 = Content.indexof ("*");
int result4 = Content.indexof ("/");
Jtx.settext ("");
============= began to Judge ============================
if (result1>0) {
String value1 = content.substring (0, RESULT1);
String value2 = content.substring (result1+1, Content.length ());
Float F1 = float.parsefloat (value1);
float F2 = float.parsefloat (value2);
Jtx.settext ("" + (F1+F2));
}else if (result2>0) {
String value1 = content.substring (0, RESULT2);
String value2 = content.substring (result2+1, Content.length ());
Float F1 = float.parsefloat (value1);
float F2 = float.parsefloat (value2);
Jtx.settext ("" + (F1-F2));
}else if (result3>0) {
String value1 = content.substring (0, RESULT3);
String value2 = content.substring (result3+1, Content.length ());
Float F1 = float.parsefloat (value1);
float F2 = float.parsefloat (value2);
Jtx.settext ("" + (F1*F2));
}else if (result4>0) {
String value1 = content.substring (0, RESULT4);
String value2 = content.substring (result4+1, Content.length ());
Float F1 = float.parsefloat (value1);
float F2 = float.parsefloat (value2);
Jtx.settext ("" + (F1/F2));
}
}else{
Jtx.settext (Jtx.gettext () +command);
}
}


}

Second Listener event:

Import javax.swing.*;
Import java.awt.event.*;


public class MyListener implements actionlistener{
=============== Construction gets textfield================
JTextField jtx = null;

Public MyListener (JTextField textarea) {
Jtx = textarea;
}


=============== Monitor Digital Controls ======================
public Boolean one=true;
public void actionperformed (ActionEvent e) {
String Command=e.getactioncommand ();
if (one) {
Jtx.settext (Jtx.gettext () +command);
}
}
}

Last package:

Import javax.swing.*;
Import java.awt.event.*;
Import java.awt.*;

public class calculator{
Public String number[] ={"1", "2", "3", "+", "4", "5", "6", "-", "7", "8", "9", "*", "0", ".", "/", "="};
public void Paintingframeui () {
===========draw Main frame==========================
JFrame frame=new JFrame ();
Frame.setsize (242,285);
Frame.setlocationrelativeto (NULL);
Frame.setdefaultcloseoperation (3);
Frame.settitle ("Calculator");


===========set layout================================
FlowLayout layout=new FlowLayout (0,0,0);
Frame.setlayout (layout);

===========to instance TextField control=============
JTextField textarea=new JTextField (20);
Textarea.sethorizontalalignment (Jtextfield.right);
Frame.add (textarea);
JPanel JP = new JPanel ();
Dimension dimen = new Dimension (225,225);
Jp.setpreferredsize (dimen);
Frame.add (JP);
GridLayout gl = new GridLayout (bis);
Jp.setlayout (GL);


===========add listenre==============================
ActionListener action_listener=new MyListener (TEXTAREA);
ActionListener string_listener=new Stringlistener (TEXTAREA);


===========to instance control=======================
for (int i=0;i<16;i++) {
JButton button=new JButton (Number[i]);
Jp.add (button);
if (i==3 | | i==7 | | i==11 | | i==15 | | i==14 | | i==13) {
Button.addactionlistener (String_listener);
}else{
Button.addactionlistener (Action_listener);
}
}


===========showframe=================================
Frame.setvisible (TRUE);
}
}


Well, see here I believe you are not too understand, but it's okay, do it yourself to know the effect



Because of the time, today's code will be written here, and now continue with your daily goals and plans:

1, daily exercise fingering an hour;

2, every day to write a blog or log;

3, every day class 15 minutes before entering the classroom;

4, give yourself to encourage a word-------not too concerned about the eyes of others, the most important thing is to look at their own eyes, others feel good, prove you two hobbies very much like, if he think your vision is not good, also proves that you two hobbies are not the same;







23rd Java How to compile the source code share of the calculator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.