Program Kalkulator
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class DemoTextField implements ActionListener {
public JLabel label1, label2, label3;
public JTextField textField1, textField2, textField3;
public JButton button1, button2, button3, button4;
public DemoTextField() {
label1 = new JLabel("Nilai A");
label1.setLocation(10, 10);
label1.setSize(label1.getPreferredSize());
textField1 = new JTextField(20);
textField1.setLocation(10, 25);
textField1.setSize(textField1.getPreferredSize());
label2 = new JLabel("Nilai B");
label2.setLocation(10, 45);
label2.setSize(label2.getPreferredSize());
textField2 = new JTextField(20);
textField2.setLocation(10, 60);
textField2.setSize(textField2.getPreferredSize());
label3 = new JLabel("Hasil perhitungan antara A dan B");
label3.setLocation(10, 90);
label3.setSize(label3.getPreferredSize());
textField3 = new JTextField(20);
textField3.setLocation(10, 105);
textField3.setSize(textField3.getPreferredSize());
textField3.setEditable(false);
button1 = new JButton("Tambah");
button1.setLocation(245, 10);
button1.setSize(button1.getPreferredSize());
button1.addActionListener(this);
button1.setMnemonic('T');
button2 = new JButton("Kurang");
button2.setLocation(245, 40);
button2.setSize(button1.getPreferredSize());
button2.addActionListener(this);
button2.setMnemonic('u');
button3 = new JButton("Kali");
button3.setLocation(245, 70);
button3.setSize(button1.getPreferredSize());
button3.addActionListener(this);
button3.setMnemonic('K');
button4 = new JButton("Bagi");
button4.setLocation(245, 100);
button4.setSize(button1.getPreferredSize());
button4.addActionListener(this);
button4.setMnemonic('B');
}
public void createAndShowGUI() {
//JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Demo JTextField");
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label1);
frame.getContentPane().add(textField1);
frame.getContentPane().add(label2);
frame.getContentPane().add(textField2);
frame.getContentPane().add(label3);
frame.getContentPane().add(textField3);
frame.getContentPane().add(button1);
frame.getContentPane().add(button2);
frame.getContentPane().add(button3);
frame.getContentPane().add(button4);
frame.setBounds(0, 0, 350, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
double a=0.0, b=0.0, c=0.0;
try {
a = Double.parseDouble(textField1.getText());
b = Double.parseDouble(textField2.getText());
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
JButton btn = (JButton) event.getSource();
if (btn == button1) {
c = a + b;
} else if (btn == button2) {
c = a - b;
} else if (btn == button3) {
c = a * b;
} else {
c = a / b;
}
textField3.setText(new String().valueOf(c));
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
DemoTextField app = new DemoTextField();
app.createAndShowGUI();
}
});
}
}
OUTPUT :
No comments:
Post a Comment