Parkir Kendaraan (Java)
Selasa, 23 Maret 2010
Anda bingung dalam membuat program parkir kendaraan dalam java beserta jam otomatisnya Sekarang anda tidak usah bingung lagi untuk membuat program parkir kendaraan dalam java. Berikut coding lengkapnya !!
import java.util.logging.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.table.DefaultTableModel;
public class parkirkendaraan extends JFrame{
private JScrollPane scrollPane;
private JTable table;
private JLabel labelNo_Kendaraan;
private JTextField textNo_Kendaraan;
private JTextField textJam;
private JButton buttonSubmit;
private JButton buttonBatal;
private Object[] obj;
private DefaultTableModel model;
private EventListener eventTable;
int currentHours;
int currentMinutes;
int currentSeconds;
String clock;
public static void main(String[]damian){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
Logger.getLogger(parkirkendaraan.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(parkirkendaraan.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(parkirkendaraan.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(parkirkendaraan.class.getName()).log(Level.SEVERE, null, ex);
} finally {
parkirkendaraan kendaraan = new parkirkendaraan();
kendaraan.setVisible(true);
}
}
});
}
public parkirkendaraan() {
setTitle("Kelompok (1) 2DB16");
model = new DefaultTableModel();
model.addColumn("No. Kenderaan");
model.addColumn("Jam Masuk Kendaraan");
initComponent();
setLocationRelativeTo(null);
initAction();
}
private void initComponent() {
this.getContentPane().setLayout(null);
setSize(400, 300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
scrollPane = new JScrollPane();
labelNo_Kendaraan = new JLabel();
textNo_Kendaraan = new JTextField();
textJam = new JTextField();
buttonSubmit = new JButton();
buttonBatal = new JButton();
table = new JTable();
table.setModel(model);
scrollPane.setViewportView(table);
scrollPane.setBounds(10, 60, 350, 150);
labelNo_Kendaraan.setText("No. Kendaraan");
labelNo_Kendaraan.setBounds(new Rectangle(10, 10, 100, 20));
labelNo_Kendaraan.setHorizontalTextPosition(SwingConstants.CENTER);
//memasukkan nilai ke textJam
textNo_Kendaraan.setBounds(new Rectangle(90, 10, 272, 20));
buttonSubmit.setText("Masuk");
buttonSubmit.setBounds(200, 230, 100, 25);
buttonSubmit.setHorizontalTextPosition(SwingConstants.CENTER);
buttonBatal.setText("Batal");
buttonBatal.setBounds(85, 230, 100, 25);
buttonBatal.setHorizontalTextPosition(SwingConstants.CENTER);
this.getContentPane().add(scrollPane);
this.getContentPane().add(labelNo_Kendaraan);
this.getContentPane().add(textJam);
this.getContentPane().add(textNo_Kendaraan);
this.getContentPane().add(buttonSubmit);
this.getContentPane().add(buttonBatal);
}
private void initAction() {
buttonSubmit.addActionListener(new Button_Masuk_Data());
buttonBatal.addActionListener(new Button_Batal_Data());
}
class Button_Masuk_Data implements ActionListener{
public void actionPerformed(ActionEvent e) {
Date myDate=new Date();
currentHours = myDate.getHours();
currentMinutes = myDate.getMinutes();
currentSeconds = myDate.getSeconds();
clock=currentHours+" : "+currentMinutes+" : "+currentSeconds;
textJam.setText(clock);
if(textNo_Kendaraan.getText().trim().equals("")){
JOptionPane.showMessageDialog(null, "No. Kendaraan masih kosong");
textNo_Kendaraan.requestFocusInWindow();
}else{
addListenerObjec(eventTable);
JOptionPane.showMessageDialog(null, "Data Berhasil Dimasukkan");
batal();
}
}
}
class Button_Batal_Data implements ActionListener{
public void actionPerformed(ActionEvent e) {
batal();
}
}
public void addListenerObjec(EventListener listener){
obj = new Object[]{textNo_Kendaraan.getText(), textJam.getText()};
model.addRow(obj);
}
private void batal(){
textNo_Kendaraan.setText("");
textNo_Kendaraan.requestFocusInWindow();
}
}
Outputnya :
Ingat nama sevannya harus sesuai dengan nama classnya yaitu parkirkendaraan.java
SELAMAT MENCOBA !!
0 komentar:
Posting Komentar