Advertisement
Guest User

Java gui

a guest
Jun 8th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.62 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.EventQueue;
  3.  
  4. import javax.script.ScriptEngine;
  5. import javax.script.ScriptEngineManager;
  6. import javax.script.ScriptException;
  7. import javax.swing.JFrame;
  8. import javax.swing.JPanel;
  9. import javax.swing.border.EmptyBorder;
  10. import javax.swing.JTextField;
  11. import javax.swing.JButton;
  12.  
  13. import java.awt.Color;
  14. import java.awt.SystemColor;
  15. import java.awt.event.ActionListener;
  16. import java.awt.event.ActionEvent;
  17.  
  18.  
  19. public class Calculator extends JFrame {
  20.  
  21.     private JPanel contentPane;
  22.     private JTextField console;
  23.  
  24.     /**
  25.      * Launch the application.
  26.      */
  27.     public static void main(String[] args) {
  28.         EventQueue.invokeLater(new Runnable() {
  29.             public void run() {
  30.                 try {
  31.                     Calculator frame = new Calculator();
  32.                     frame.setVisible(true);
  33.                 } catch (Exception e) {
  34.                     e.printStackTrace();
  35.                 }
  36.             }
  37.         });
  38.     }
  39.  
  40.     /**
  41.      * Create the frame.
  42.      */
  43.     public Calculator() {
  44.         setResizable(false);
  45.         setTitle("calculator");
  46.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  47.         setBounds(100, 100, 369, 475);
  48.         contentPane = new JPanel();
  49.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  50.         setContentPane(contentPane);
  51.         contentPane.setLayout(null);
  52.        
  53.         console = new JTextField();
  54.         console.setEditable(false);
  55.         console.setBounds(21, 21, 315, 61);
  56.         contentPane.add(console);
  57.         console.setColumns(10);
  58.        
  59.         JButton button = new JButton("1");
  60.         button.addActionListener(new ActionListener() {
  61.             public void actionPerformed(ActionEvent arg0) {
  62.                 String gettext=console.getText();
  63.                 console.setText( gettext+"1");
  64.             }
  65.         });
  66.         button.setBackground(new Color(240, 240, 240));
  67.         button.setBounds(21, 120, 56, 40);
  68.         contentPane.add(button);
  69.        
  70.         JButton button_1 = new JButton("2");
  71.         button_1.addActionListener(new ActionListener() {
  72.             public void actionPerformed(ActionEvent arg0) {
  73.                 String gettext=console.getText();
  74.                 console.setText( gettext+"2");
  75.             }
  76.         });
  77.         button_1.setBounds(104, 122, 56, 40);
  78.         contentPane.add(button_1);
  79.        
  80.         JButton button_2 = new JButton("3");
  81.         button_2.addActionListener(new ActionListener() {
  82.             public void actionPerformed(ActionEvent arg0) {
  83.                 String gettext=console.getText();
  84.                 console.setText( gettext+"3");
  85.             }
  86.         });
  87.         button_2.setBounds(170, 122, 56, 38);
  88.         contentPane.add(button_2);
  89.        
  90.         JButton btnNewButton = new JButton("4");
  91.         btnNewButton.addActionListener(new ActionListener() {
  92.             public void actionPerformed(ActionEvent arg0) {
  93.                 String gettext=console.getText();
  94.                 console.setText( gettext+"4");
  95.             }
  96.         });
  97.         btnNewButton.setBounds(236, 122, 64, 40);
  98.         contentPane.add(btnNewButton);
  99.        
  100.         JButton button_3 = new JButton("5");
  101.         button_3.addActionListener(new ActionListener() {
  102.             public void actionPerformed(ActionEvent arg0) {
  103.                 String gettext=console.getText();
  104.                 console.setText( gettext+"5");
  105.             }
  106.         });
  107.         button_3.setBackground(SystemColor.menu);
  108.         button_3.setBounds(21, 191, 56, 40);
  109.         contentPane.add(button_3);
  110.        
  111.         JButton button_5 = new JButton("6");
  112.         button_5.addActionListener(new ActionListener() {
  113.             public void actionPerformed(ActionEvent arg0) {
  114.                 String gettext=console.getText();
  115.                 console.setText( gettext+"6");
  116.             }
  117.         });
  118.         button_5.setBounds(104, 192, 56, 38);
  119.         contentPane.add(button_5);
  120.        
  121.         JButton button_6 = new JButton("7");
  122.         button_6.addActionListener(new ActionListener() {
  123.             public void actionPerformed(ActionEvent arg0) {
  124.                 String gettext=console.getText();
  125.                 console.setText( gettext+"7");
  126.             }
  127.         });
  128.         button_6.setBounds(180, 191, 64, 40);
  129.         contentPane.add(button_6);
  130.        
  131.         JButton button_7 = new JButton("8");
  132.         button_7.addActionListener(new ActionListener() {
  133.             public void actionPerformed(ActionEvent arg0) {
  134.                 String gettext=console.getText();
  135.                 console.setText( gettext+"8");
  136.             }
  137.         });
  138.         button_7.setBackground(SystemColor.menu);
  139.         button_7.setBounds(254, 191, 56, 40);
  140.         contentPane.add(button_7);
  141.        
  142.         JButton button_8 = new JButton("9");
  143.         button_8.addActionListener(new ActionListener() {
  144.             public void actionPerformed(ActionEvent arg0) {
  145.                 String gettext=console.getText();
  146.                 console.setText( gettext+"9");
  147.             }
  148.         });
  149.         button_8.setBounds(21, 262, 56, 40);
  150.         contentPane.add(button_8);
  151.        
  152.         JButton button_9 = new JButton("0");
  153.         button_9.addActionListener(new ActionListener() {
  154.             public void actionPerformed(ActionEvent arg0) {
  155.                 String gettext=console.getText();
  156.                 console.setText( gettext+"0");
  157.             }
  158.         });
  159.         button_9.setBounds(104, 263, 56, 38);
  160.         contentPane.add(button_9);
  161.        
  162.         JButton button_10 = new JButton("+");
  163.         button_10.addActionListener(new ActionListener() {
  164.             public void actionPerformed(ActionEvent arg0) {
  165.                 String gettext=console.getText();
  166.                 console.setText( gettext+"+");
  167.             }
  168.         });
  169.         button_10.setBackground(SystemColor.menu);
  170.         button_10.setBounds(21, 335, 56, 40);
  171.         contentPane.add(button_10);
  172.        
  173.         JButton button_11 = new JButton("-");
  174.         button_11.addActionListener(new ActionListener() {
  175.             public void actionPerformed(ActionEvent arg0) {
  176.                 String gettext=console.getText();
  177.                 console.setText( gettext+"-");
  178.             }
  179.         });
  180.         button_11.setBounds(104, 337, 56, 40);
  181.         contentPane.add(button_11);
  182.        
  183.         JButton button_12 = new JButton("*");
  184.         button_12.addActionListener(new ActionListener() {
  185.             public void actionPerformed(ActionEvent arg0) {
  186.                 String gettext=console.getText();
  187.                 console.setText( gettext+"*");
  188.             }
  189.         });
  190.         button_12.setBounds(170, 337, 56, 38);
  191.         contentPane.add(button_12);
  192.        
  193.         JButton button_13 = new JButton("/");
  194.         button_13.addActionListener(new ActionListener() {
  195.             public void actionPerformed(ActionEvent arg0) {
  196.                 String gettext=console.getText();
  197.                 console.setText( gettext+"/");
  198.             }
  199.         });
  200.         button_13.setBounds(236, 337, 64, 40);
  201.         contentPane.add(button_13);
  202.        
  203.         JButton button_14 = new JButton("=");
  204.         button_14.addActionListener(new ActionListener() {
  205.             public void actionPerformed(ActionEvent arg0) {
  206.                 ScriptEngineManager c=new ScriptEngineManager();
  207.                 ScriptEngine r=c.getEngineByName("JavaScript");
  208.                 String result=console.getText();
  209.                 try {
  210.                     console.setText(r.eval(result).toString());
  211.                 } catch (ScriptException e) {
  212.                     // TODO Auto-generated catch block
  213.                     console.setText("Eror");
  214.                 }
  215.             }
  216.         });
  217.         button_14.setBackground(SystemColor.menu);
  218.         button_14.setBounds(21, 386, 56, 40);
  219.         contentPane.add(button_14);
  220.        
  221.         JButton btnAc = new JButton("Ac");
  222.         btnAc.addActionListener(new ActionListener() {
  223.             public void actionPerformed(ActionEvent arg0) {
  224.                 console.setText(null);
  225.             }
  226.         });
  227.         btnAc.setBounds(104, 388, 56, 40);
  228.         contentPane.add(btnAc);
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement