Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import javax.script.ScriptEngine;
- import javax.script.ScriptEngineManager;
- import javax.script.ScriptException;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JTextField;
- import javax.swing.JButton;
- import java.awt.Color;
- import java.awt.SystemColor;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- public class Calculator extends JFrame {
- private JPanel contentPane;
- private JTextField console;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Calculator frame = new Calculator();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public Calculator() {
- setResizable(false);
- setTitle("calculator");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 369, 475);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- console = new JTextField();
- console.setEditable(false);
- console.setBounds(21, 21, 315, 61);
- contentPane.add(console);
- console.setColumns(10);
- JButton button = new JButton("1");
- button.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- String gettext=console.getText();
- console.setText( gettext+"1");
- }
- });
- button.setBackground(new Color(240, 240, 240));
- button.setBounds(21, 120, 56, 40);
- contentPane.add(button);
- JButton button_1 = new JButton("2");
- button_1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- String gettext=console.getText();
- console.setText( gettext+"2");
- }
- });
- button_1.setBounds(104, 122, 56, 40);
- contentPane.add(button_1);
- JButton button_2 = new JButton("3");
- button_2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- String gettext=console.getText();
- console.setText( gettext+"3");
- }
- });
- button_2.setBounds(170, 122, 56, 38);
- contentPane.add(button_2);
- JButton btnNewButton = new JButton("4");
- btnNewButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- String gettext=console.getText();
- console.setText( gettext+"4");
- }
- });
- btnNewButton.setBounds(236, 122, 64, 40);
- contentPane.add(btnNewButton);
- JButton button_3 = new JButton("5");
- button_3.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- String gettext=console.getText();
- console.setText( gettext+"5");
- }
- });
- button_3.setBackground(SystemColor.menu);
- button_3.setBounds(21, 191, 56, 40);
- contentPane.add(button_3);
- JButton button_5 = new JButton("6");
- button_5.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- String gettext=console.getText();
- console.setText( gettext+"6");
- }
- });
- button_5.setBounds(104, 192, 56, 38);
- contentPane.add(button_5);
- JButton button_6 = new JButton("7");
- button_6.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- String gettext=console.getText();
- console.setText( gettext+"7");
- }
- });
- button_6.setBounds(180, 191, 64, 40);
- contentPane.add(button_6);
- JButton button_7 = new JButton("8");
- button_7.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- String gettext=console.getText();
- console.setText( gettext+"8");
- }
- });
- button_7.setBackground(SystemColor.menu);
- button_7.setBounds(254, 191, 56, 40);
- contentPane.add(button_7);
- JButton button_8 = new JButton("9");
- button_8.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- String gettext=console.getText();
- console.setText( gettext+"9");
- }
- });
- button_8.setBounds(21, 262, 56, 40);
- contentPane.add(button_8);
- JButton button_9 = new JButton("0");
- button_9.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- String gettext=console.getText();
- console.setText( gettext+"0");
- }
- });
- button_9.setBounds(104, 263, 56, 38);
- contentPane.add(button_9);
- JButton button_10 = new JButton("+");
- button_10.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- String gettext=console.getText();
- console.setText( gettext+"+");
- }
- });
- button_10.setBackground(SystemColor.menu);
- button_10.setBounds(21, 335, 56, 40);
- contentPane.add(button_10);
- JButton button_11 = new JButton("-");
- button_11.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- String gettext=console.getText();
- console.setText( gettext+"-");
- }
- });
- button_11.setBounds(104, 337, 56, 40);
- contentPane.add(button_11);
- JButton button_12 = new JButton("*");
- button_12.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- String gettext=console.getText();
- console.setText( gettext+"*");
- }
- });
- button_12.setBounds(170, 337, 56, 38);
- contentPane.add(button_12);
- JButton button_13 = new JButton("/");
- button_13.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- String gettext=console.getText();
- console.setText( gettext+"/");
- }
- });
- button_13.setBounds(236, 337, 64, 40);
- contentPane.add(button_13);
- JButton button_14 = new JButton("=");
- button_14.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- ScriptEngineManager c=new ScriptEngineManager();
- ScriptEngine r=c.getEngineByName("JavaScript");
- String result=console.getText();
- try {
- console.setText(r.eval(result).toString());
- } catch (ScriptException e) {
- // TODO Auto-generated catch block
- console.setText("Eror");
- }
- }
- });
- button_14.setBackground(SystemColor.menu);
- button_14.setBounds(21, 386, 56, 40);
- contentPane.add(button_14);
- JButton btnAc = new JButton("Ac");
- btnAc.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- console.setText(null);
- }
- });
- btnAc.setBounds(104, 388, 56, 40);
- contentPane.add(btnAc);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement