†`Li†o`† Admin
Posts : 80 Points : 168 Reputation : 9 Birthday : 1995-11-16 Join date : 2011-08-31 Age : 28 Location : Mayapa Calamba Laguna Job/hobbies : Playing Computer Games.
| Subject: JAVA CALCULATE Mon Sep 17, 2012 2:14 pm | |
| - Quote :
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.regex.*; class Calculate extends JFrame{ JButton ADD; JPanel panel; JLabel label1,label2; final JTextField text1,text2; int cal; Calculate(){ label1 = new JLabel(); label1.setText("Enter First Number:"); text1 = new JTextField(20);
label2 = new JLabel(); label2.setText("Enter Second Number:"); text2 = new JTextField(20);
ADD=new JButton("Add"); panel=new JPanel(new GridLayout(3,2)); panel.add(label1); panel.add(text1); panel.add(label2); panel.add(text2); panel.add(ADD); add(panel); setVisible(true); pack(); setTitle("Calculate");
ADD.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ String value1=text1.getText(); String value2=text2.getText(); Pattern p=Pattern.compile("[A-Z,a-z,&%$#@!()*^]"); Matcher m1 = p.matcher(value1); Matcher m2 = p.matcher(value2); if(value1.length()==0){ JOptionPane.showMessageDialog(null,"Enter first number","Error",JOptionPane.ERROR_MESSAGE); } else if(m1.find()){ JOptionPane.showMessageDialog(null,"Enter second nuber","Error",JOptionPane.ERROR_MESSAGE); text1.setText(""); } else if(value2.length()==0){ JOptionPane.showMessageDialog(null,"Enter only number!","Error",JOptionPane.ERROR_MESSAGE); text2.setText(""); } else{ int n1=Integer.parseInt(value1); int n2=Integer.parseInt(value2); cal=n1+n2; JOptionPane.showMessageDialog(null,"Additon of two numbers is:"+Integer.toString(cal)); } } }); } public static void main(String arg[]){ new Calculate(); } }
| |
|