Advertisement
resacr

Kalkulator Ringkas

Jan 11th, 2016
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.92 KB | None | 0 0
  1. import java.awt.EventQueue;
  2. import javax.swing.JFrame;
  3. import javax.swing.JPanel;
  4. import javax.swing.UIManager;
  5. import javax.swing.border.EmptyBorder;
  6. import javax.swing.JTextField;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.ActionEvent;
  9. import javax.swing.JButton;
  10. import java.awt.Color;
  11. import javax.swing.JLabel;
  12. import javax.swing.ImageIcon;
  13. import javax.swing.SwingConstants;
  14.  
  15. @SuppressWarnings("serial")
  16. public class Frm extends JFrame {
  17.  
  18.     private JPanel contentPane;
  19.     private JTextField txt1;
  20.     private JTextField txt2;
  21.     private JTextField txtHasil;
  22.     double hasil;
  23.     private JButton Tambah;
  24.     private JButton Kurang;
  25.     private JButton Kali;
  26.     private JButton Bagi;
  27.     private JButton Refresh;
  28.     private JLabel lblIcon;
  29.  
  30.     /**
  31.      * Create the frame.
  32.      */
  33.     public Frm() {
  34.         setTitle("Kalkulator Ringkas");
  35.         setResizable(false);
  36.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  37.         setBounds(100, 100, 358, 278);
  38.         contentPane = new JPanel();
  39.         contentPane.setBackground(Color.GRAY);
  40.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  41.         setContentPane(contentPane);
  42.         contentPane.setLayout(null);
  43.  
  44.         txt1 = new JTextField();
  45.         txt1.setBounds(12, 25, 233, 28);
  46.         contentPane.add(txt1);
  47.         txt1.setColumns(10);
  48.  
  49.         txt2 = new JTextField();
  50.         txt2.setColumns(10);
  51.         txt2.setBounds(12, 65, 233, 28);
  52.         contentPane.add(txt2);
  53.  
  54.         txtHasil = new JTextField();
  55.         txtHasil.setColumns(10);
  56.         txtHasil.setBounds(12, 177, 233, 28);
  57.         contentPane.add(txtHasil);
  58.  
  59.         Tambah = new JButton("+");
  60.         Tambah.addActionListener(new ActionListener() {
  61.             public void actionPerformed(ActionEvent arg0) {
  62.                 hasil = Double.parseDouble(txt1.getText())
  63.                         + Double.parseDouble(txt2.getText());
  64.                 txtHasil.setText("" + hasil);
  65.             }
  66.         });
  67.         Tambah.setBounds(57, 105, 61, 24);
  68.         contentPane.add(Tambah);
  69.  
  70.         Kurang = new JButton("-");
  71.         Kurang.addActionListener(new ActionListener() {
  72.             public void actionPerformed(ActionEvent arg0) {
  73.                 hasil = Double.parseDouble(txt1.getText())
  74.                         - Double.parseDouble(txt2.getText());
  75.                 txtHasil.setText("" + hasil);
  76.             }
  77.         });
  78.         Kurang.setBounds(130, 105, 61, 24);
  79.         contentPane.add(Kurang);
  80.  
  81.         Kali = new JButton("x");
  82.         Kali.addActionListener(new ActionListener() {
  83.             public void actionPerformed(ActionEvent arg0) {
  84.                 hasil = Double.parseDouble(txt1.getText())
  85.                         * Double.parseDouble(txt2.getText());
  86.                 txtHasil.setText("" + hasil);
  87.             }
  88.         });
  89.         Kali.setBounds(57, 141, 61, 24);
  90.         contentPane.add(Kali);
  91.  
  92.         Bagi = new JButton("/");
  93.         Bagi.addActionListener(new ActionListener() {
  94.             public void actionPerformed(ActionEvent arg0) {
  95.                 hasil = Double.parseDouble(txt1.getText())
  96.                         / Double.parseDouble(txt2.getText());
  97.                 txtHasil.setText("" + hasil);
  98.             }
  99.         });
  100.         Bagi.setBounds(130, 141, 61, 24);
  101.         contentPane.add(Bagi);
  102.  
  103.         Refresh = new JButton("Refresh");
  104.         Refresh.addActionListener(new ActionListener() {
  105.             public void actionPerformed(ActionEvent arg0) {
  106.                 txt1.setText("");
  107.                 txt2.setText("");
  108.                 txtHasil.setText("");
  109.                 txt1.requestFocus();
  110.             }
  111.         });
  112.         Refresh.setBounds(12, 217, 94, 24);
  113.         contentPane.add(Refresh);
  114.  
  115.         lblIcon = new JLabel("Blankon Linux");
  116.         lblIcon.setHorizontalTextPosition(SwingConstants.CENTER);
  117.         lblIcon.setHorizontalAlignment(SwingConstants.CENTER);
  118.         lblIcon.setVerticalTextPosition(SwingConstants.BOTTOM);
  119.         lblIcon.setForeground(new Color(0, 0, 0));
  120.         lblIcon.setIcon(new ImageIcon(Frm.class
  121.                 .getResource("/Kalkulator/64x64.png")));
  122.         lblIcon.setBounds(249, 77, 107, 85);
  123.         contentPane.add(lblIcon);
  124.     }
  125.  
  126.     /**
  127.      * Launch the application.
  128.      */
  129.     public static void main(String[] args) {
  130.         EventQueue.invokeLater(new Runnable() {
  131.             public void run() {
  132.                 try {
  133.                     UIManager.setLookAndFeel(UIManager
  134.                             .getSystemLookAndFeelClassName());
  135.                     Frm frame = new Frm();
  136.                     frame.setVisible(true);
  137.                 } catch (Exception e) {
  138.                     e.printStackTrace();
  139.                 }
  140.             }
  141.         });
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement