import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SewaMobil implements ActionListener {
public JLabel labelKode, labelSewa, labelHrg, labelHasil;
public JTextField textField1, textField2, textField3, textField4, textField5;
public JButton button1, button2;
public SewaMobil() {
labelKode = new JLabel("Kode Barang");
labelKode.setLocation(10, 25);
labelKode.setSize(labelKode.getPreferredSize());
textField1 = new JTextField(10);
textField1.setLocation(85, 25);
textField1.setSize(textField1.getPreferredSize());
labelSewa = new JLabel("Lama Sewa");
labelSewa.setLocation(10, 60);
labelSewa.setSize(labelSewa.getPreferredSize());
textField2 = new JTextField(10);
textField2.setLocation(85, 60);
textField2.setSize(textField2.getPreferredSize());
labelHasil = new JLabel("Nama Mobil");
labelHasil.setLocation(10, 105);
labelHasil.setSize(labelHasil.getPreferredSize());
textField3 = new JTextField(10);
textField3.setLocation(85, 105);
textField3.setSize(textField3.getPreferredSize());
textField3.setEditable (false);
labelHrg = new JLabel("Harga Sewa");
labelHrg.setLocation(10, 125);
labelHrg.setSize(labelHrg.getPreferredSize());
textField4 = new JTextField(10);
textField4.setLocation(85, 125);
textField4.setSize(textField3.getPreferredSize());
textField4.setEditable (false);
textField5 = new JTextField(10);
textField5.setBackground(Color.RED);
textField5.setLocation(85, 145);
textField5.setSize(textField3.getPreferredSize());
textField5.setEditable (false);
button1 = new JButton("Click");
button1.setBackground(Color.BLACK);
button1.setForeground(Color.WHITE);
button1.setLocation(245, 40);
button1.setSize(button1.getPreferredSize());
button1.addActionListener(this);
button1.setMnemonic('P');
button2 = new JButton("Close");
button2.setBackground(Color.BLACK);
button2.setForeground(Color.WHITE);
button2.setLocation(245, 80);
button2.setSize(button2.getPreferredSize());
button2.addActionListener(this);
button2.setMnemonic('P');
}
public void createAndShowGUI() {
JFrame frame = new JFrame("Aplikasi Sewa Mobil");
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(labelKode);
frame.getContentPane().add(textField1);
frame.getContentPane().add(labelSewa);
frame.getContentPane().add(textField2);
frame.getContentPane().add(labelHasil);
frame.getContentPane().add(textField3);
frame.getContentPane().add(labelHrg);
frame.getContentPane().add(textField4);
frame.getContentPane().add(textField5);
frame.getContentPane().add(button1);
frame.getContentPane().add(button2);
frame.setBounds(0 ,0 , 400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
int Lmsw=0, Hrga =0, hasil=0;
String Cod="";
String nam="";
if (event.getSource() == button1){
Cod= textField1.getText();
if (Cod.equals("KD-001")){
Hrga=200000;
nam="Kijang inova";
textField3.setText(new String().valueOf(nam));
textField4.setText(new String().valueOf(Hrga));
}
else if (Cod.equals("KD-002")){
Hrga=100000;
nam="Kijang TOYOTA";
textField3.setText(new String().valueOf(nam));
textField4.setText(new String().valueOf(Hrga));
}
}
else if (event.getSource() == button2){
JOptionPane.showMessageDialog(null, "Thanks to try my program. see you..");
System.exit(0);
}
try {
Hrga= Integer.parseInt(textField4.getText());
Lmsw= Integer.parseInt(textField2.getText());
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
JButton btn = (JButton) event.getSource();
if (btn == button1) {
hasil= Lmsw * Hrga;
}
textField5.setText(new String().valueOf("TOTAL :" + hasil));
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
SewaMobil mobil = new SewaMobil();
mobil.createAndShowGUI();
}
});
}
}