Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JTextField;
- import javax.swing.JButton;
- import java.awt.*;
- import java.awt.event.*;
- public class SimpleCalcForm extends JFrame{
- public SimpleCalcForm(){
- initComponent();
- }
- private void initComponent(){
- setSize(380, 220);
- setTitle("Kalkulator Sederhana");
- setLayout(null);
- setResizable(false);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- bil1Label = new JLabel();
- add(bil1Label);
- bil1Label.setText("Masukkan Bilangan Pertama : ");
- bil1Label.setBounds(20, 10, 170, 30);
- bil1TextField = new JTextField();
- add(bil1TextField);
- bil1TextField.setBounds(20, 45, 170, 30);
- bil2Label = new JLabel();
- add(bil2Label);
- bil2Label.setText("Masukkan Bilangan Kedua : ");
- bil2Label.setBounds(20, 80 , 170, 30);
- bil2TextField = new JTextField();
- add(bil2TextField);
- bil2TextField.setBounds(20, 110, 170, 30);
- addButton = new JButton();
- add(addButton);
- addButton.setText("+");
- addButton.setBounds(200, 45, 45, 30);
- addButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- addButtonActionPerformed(evt);
- }
- });
- subtractionButton = new JButton();
- add(subtractionButton);
- subtractionButton.setText("-");
- subtractionButton.setBounds(250, 45, 45, 30);
- subtractionButton.addActionListener(new java.awt.event.ActionListener(){
- public void actionPerformed(java.awt.event.ActionEvent evt){
- subtractionButtonActionPerformed(evt);
- }
- });
- multiplicationButton = new JButton();
- add(multiplicationButton);
- multiplicationButton.setText("X");
- multiplicationButton.setBounds(300, 45, 45, 30);
- multiplicationButton.addActionListener(new java.awt.event.ActionListener(){
- public void actionPerformed(java.awt.event.ActionEvent evt){
- multiplicationButtonActionPerformed(evt);
- }
- });
- divideButton = new JButton();
- add(divideButton);
- divideButton.setText("/");
- divideButton.setBounds(200, 80, 45, 30);
- divideButton.addActionListener(new java.awt.event.ActionListener(){
- public void actionPerformed(java.awt.event.ActionEvent evt){
- divideButtonActionPerformed(evt);
- }
- });
- modButton = new JButton();
- add(modButton);
- modButton.setText("%");
- modButton.setBounds(250, 80, 45, 30);
- modButton.addActionListener(new java.awt.event.ActionListener(){
- public void actionPerformed(java.awt.event.ActionEvent evt){
- modButtonActionPerformed(evt);
- }
- });
- clearButton = new JButton();
- add(clearButton);
- clearButton.setText("C");
- clearButton.setBounds(300, 80, 45, 30);
- clearButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- clearButtonActionPerformed(evt);
- }
- });
- exitButton = new JButton();
- add(exitButton);
- exitButton.setText("Exit");
- exitButton.setBounds(200, 115, 145, 25);
- exitButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- exitButtonActionPerformed(evt);
- }
- });
- equalLabel = new JLabel();
- add(equalLabel);
- equalLabel.setText("Hasil : ");
- equalLabel.setBounds(20, 140 , 200, 30);
- }
- private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
- bil1 = Double.parseDouble(bil1TextField.getText());
- bil2 = Double.parseDouble(bil2TextField.getText());
- result = (bil1+bil2);
- equalLabel.setText("Hasil : " + String.valueOf(result));
- }
- private void subtractionButtonActionPerformed(java.awt.event.ActionEvent evt) {
- bil1 = Double.parseDouble(bil1TextField.getText());
- bil2 = Double.parseDouble(bil2TextField.getText());
- result = (bil1-bil2);
- equalLabel.setText("Hasil : " + String.valueOf(result));
- }
- private void multiplicationButtonActionPerformed(java.awt.event.ActionEvent evt) {
- bil1 = Double.parseDouble(bil1TextField.getText());
- bil2 = Double.parseDouble(bil2TextField.getText());
- result = (bil1*bil2);
- equalLabel.setText("Hasil : " + String.valueOf(result));
- }
- private void divideButtonActionPerformed(java.awt.event.ActionEvent evt) {
- bil1 = Double.parseDouble(bil1TextField.getText());
- bil2 = Double.parseDouble(bil2TextField.getText());
- result = (bil1/bil2);
- equalLabel.setText("Hasil : " + String.valueOf(result));
- }
- private void modButtonActionPerformed(java.awt.event.ActionEvent evt) {
- bil1 = Double.parseDouble(bil1TextField.getText());
- bil2 = Double.parseDouble(bil2TextField.getText());
- result = (bil1%bil2);
- equalLabel.setText("Hasil : " + String.valueOf(result));
- }
- private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {
- System.exit(0);
- }
- private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {
- bil1TextField.setText(null);
- bil2TextField.setText(null);
- equalLabel.setText("Hasil : ");
- }
- public static void main(String[] args){
- new SimpleCalcForm().setVisible(true);
- }
- private JTextField bil2TextField;
- private JTextField bil1TextField;
- private JLabel bil1Label;
- private JLabel bil2Label;
- private JLabel equalLabel;
- private JButton addButton;
- private JButton subtractionButton;
- private JButton multiplicationButton;
- private JButton divideButton;
- private JButton modButton;
- private JButton clearButton;
- private JButton exitButton;
- double bil1,bil2,result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement