Selasa, 16 April 2013

Contoh Coding Menu Pada Java


Package apk.model;

Import java.awt.*;
Import java.awt.event.ActionEvent;
Import java.awt.event.ActionListener;
Import java.awt.event.WindowAdapter;
Import java.awt.event.WindowEvent;
Import java.beans.PropertyVetoException;
Import java.io.InputStream;
Import java.sq;.SQLException;
Import java.util.HashMap;

Import javax.swing.*;
Import javax.swing.SwingConstants;
Import apk.model.Login;
Import apk.gui.Laporan;
Import apk.gui.TransaksiBayar;
Import apk.gui.Login;
Import.apk.gui.FormPembayaran;
Public class MenuPembayaran extends JFrame {
Private static final long serialVersionUID = 1L;
Private JFrame self = this;
JPanel panelJudulAtas=new JPanel();
JMenuBar menuBar=new JMenuBar();
JMenu menuFile=new JMneu();
JMenuItem about=new JMenuItem();
JMenuItem fileKeluar=new JMenuItem();
JMenu menuInput=new JMenu();
JMenuItem inputPegawai=new JMenuItem();
JMenu menuProses=new JMenu();
JMenuItem transaksi=new JMenuItem();
JMenuItem prosesKembali=new JMenuItem();
JDesktopPane desktop=new JDesktopPane();
JMenu menuLaporan=new Jmenu();

L-1

JMenu laporanPerNip=new JMenuItem();
JMenuItem laporanSemua=new JMenuItem();
Public MenuPembayaran() {
SetTitle(“Aplikasi Penjualan Tas dan Sepatu”);
setJMenuBar(menuBar);
setContentPane(desktop);
displayGUI();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
setSize(screen.width,screen.height-10);
this.getContentPane().add(panelJudulAtas);
ImageIcon ic = new ImageIcon(“images/gambarTas.jpg”);
JLabel label = new JLabel(ic);
Label.setBounds(new Rectagle(10,30, 1200, 650));
addWindowListener(new WindowAdapter() {
public void windowOpened(WindowEvent arg0) {
if(!LoginState.getInstance().isLoggedIn()) {
new Login(self).setVisible(true);
}
}
});
panelJudulAtas.setBackground(Color.WHITE);
panelJudulAtas.setBounds(new Rectangle(0, 0, 1500, 1000));
panelJudulAtas.setLayout(null);
panelJudulAtas.add(label);
}
Public void displayGUI(){
prosesMenu();
menuFile.setText(“Exit”);
fileKeluar.setText(“Keluar”);
menuInput.stText(“Input Data”);

L-2

Inputbarang.setText(“Data barang”);
menuProses.setText(“Transaksi”);
transaksi.setText(“Pembayaran”);
menuLaporan.setText(“Laporan”);
laporanSemua.setText(“Penjualan keseluruhan”);
laporanPer.setText(“Transaksi Pembeli”);
menuBar.add(menuInput);
menuBar.add(menuProses);
menuBar.add(menuLaporan);
menuBar.add(menuFile);
menuFile.add(fileKeluar);
menuInput.add(inputbarang);
menuProses.add(transaksi);
menuLaporan.add(laporanSemua);
menuLaporan.addSeparator();
menuLaporan.add(laporanPer);
}
Public void prosesMenu(){
fileKeluar.addActionListener( new ActionListener()
{
Public void actionPerformed(ActionEvent e)
{
Int keluar=JOptionPane.showConfirmDialog(null,”Keluar dari aplikasi ini?”,”Exit”,JOptionPane.YES_NO_OPTION);
If(keluar==))
{
System.exit(0);
}
}
}
);
inputbarang.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){

L-3

desktop.removeAll();
FormPembayaran data=new FormPembayaran();
data.setSize(desktop.getSize().width,desktop.getSize().height);
desktop.add(dataPegawai);
data.show();
try {
data.setSelected(true);
} cacth (PropertyVetoException el) {
El.printStackTrace();
}
}
});
transaksi.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
desktop.removeAll();
TransaksiBayar data=new TransaksiBayar();
data.setSize(desktop.getSize().width,desktop.getSize().height);
desktop.add(data);
data.show();
try {
data.setSelected(true);
} catch (PropertyVetoException el) {
El.printStackTrace();
}
}
});
laporanPer.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
desktop.removeAll();
Laporan data=new Laporan();
data.setSize(desktop.getSize().width,desktop.getSize().height);
desktop.add(data);
data.show();
try {
data.setSelected(true);
} cacth (PropertyVetoException el) {
El,printStackTrace();
}

L-4

}
});
laporanSemua.addActionListener(new ActionListener(){
public void actionPerformed(Action Event e){
lapPengembalianMenuItemActionPerformed(e);
}
});
}
Private void lapPengembalianMenuItemActionPerformed(ActionEven evt)
{
try {
InputStream is =
getClass().getResourceAsStream(“/laporan/Lapsem.jasper”);
JasperPrint jp = jasperFillManager.fillReport(is, new HashMap(),
DBConnect.getInstance().getConnection());
report rp = new report(jp);
rp.setVisible(true);
} catch (JRException ex) {
JOptionPane.showMessageDialog(this, “Error”+
ex.getMessage(), “Error”, JOptionPane.ERROR_MESSAGE);
} catch (SQLException ex) {
JOptionPane showMessageDialog(this, “Error”+
ex.getMessage(), “ Error”, JOptionPane.ERROR_MESSAGE);
}
}
}
MainMenu.java
package.apk.model;
public class MainMenu {
private static final long serialVersionUID = 1L
public static void main (String[]args){
new MenuPembayaran().setVisible(true);

L-5

}
}
Login.java
package apk.gui;
import jav.awt.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriveManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;
import javax.swing.*;
public class Login extends JDialog implements ActionListener{
private static final long serialVersionUID = 1L
private JLabel usernameLabel, passwordLabel;
private JTextField usernameField;
private JButton loginBtn, cancelBtn;
public Login(JFrame parent) {
super(parent, true);
initComponents();
setLocationRelativeTo(parent);
System.out.println(“Login dialog”);
setResizable(false);
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
}
private void initComponents() {
setTitle(“Silahkan Login”);
setLayout(new BorderLayout());

L-6

JPanel topPanel = new JPanel();
topPanel.setLayout(new GridBagLayout());
usernameLabel = new JLabel(“Nama : “);
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstrain.LINE_STAR;
c.gridx = 0;
c.gridy = 0;
topPanel.add(usernameLabel,c);
usenameField = new JTextField();
usernameField.setPreferredSize(new Dimension(150,20));
c = new GridBagConstrains();
c.anchor = GridBagConstrains.LINE_START;
c.insets = new Insets(0, 10, 0, 0);
c.gridx = 1;
c.gridy = 0;
topPanel.add(usernameField,c);
passwordLabel = new JLabel(“Kata Sandi :”);
c = new GridBagConstraints();
c.anchor = GridBagConstraints.LINE_START;
c.gridx = 0;
c.gridy = 1;
topPanel.add(passwordLabel,c);
passwordField = new JPassword();
passwordField.setPreferredSize(new Dimension(150,20));
c = new GridBagConstraints();
c.anchor = GridBagConstraints.LINE_START;
c.insets = new Insets(0, 10, 0, 0,);
c.gridx = 1;
c.gridy = 1;
topPanel.add(passwordFIeld, c);
add(topPanel, BorderLayout.CENTER);
JPanel btnPanel = new JPanel();
btnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
ImageIcon ic = new ImageIcon(„images/m083.gif”);
loginBtn = new JButton(“LOGIN”):
loginBtn.addActionListener(this);
ImageIcon ic2 = new ImageIcon(“images/m025.gif”);
cancelBtn = new JButton(“CANCEL”);
cancelBtn.addActionListener(this);
btn Panel.add(loginBtn);
btn Panel.add(cancelBtn);

L-7

Add(btnPanel,BorderLayout.SOUTH);
//add key listener
KeyHandler keyHandler = new KeyHandler();
usernameField.addKeyListener(keyHandler);
passwordField.addKeyListener(keyHandler);
loginBtn.addKeyListener(keyHandler);
//focus listener
MyFocusAdapter fAd = new MyFocusAdapter();
usernameField.addFocusListener(fAd);
passwordField.addFocusListener(fAd);
pack();
}
public void actionPerformed(ActionEvent evn) {
If(evt.getSource() == loginBtn) {
validateLogin();
} else if(evt.getSource() == cancelBtn) {
setVisible(false);
dispose();
System.exit(0);
}}
public boolean validateUser(String username, String password)
{ Boolean valid = false;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String sql = “ SELECT user_name,password FROM login WHERE
user_name = “”+username+”” AND password = “”+ password+””;
try
{
Class.forName(“com.mysql.jdbc.Driver”);
con =
DriverManager.getConnection(“jdbc;mysql;//localhost/apj”,”root”,”ipank”);
Stmt = con.createStatement();

L-8

rs = stmt.executeQuery(sql);
if(rs.next())
valid = true;
}
catch (ClassNotFoundException e) {
e.printStacktrace();
}
catch(SQLException e) {
e.printStackTrace();
}
finally {
try { rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return valid;
}
private void validateLogin() {
//validasi
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if(username.trim().lenght() == 0) {
JOptionPane.showMessageDialog(this, “Nama pengguna tidak boleh kosong”, “Peringatan”, JOptionPane.WARNING_MESSAGE);
usernameField.requestFocus();

L-9

return;
}
If(password.trim().lenght() == 0) {
JOptionPane.showMessageDialog(this, “Kata sandi tidak boleh kosong”, “Peringatan”,JOptionPane. WARNING_MESSAGE);
passwordField.requestFocus();
return;
}
If(validateUser(username,password)) {
LoginState.getInstance().setLoggedIn(true);
setVisible(false);
dispose();
} else {
JOptionPane.showMessageDialog(this, “Nama pengguna atau
Kata sandi salah”. “Login Gagal”, JOptionPane.ERROR_MESSAGE);
usernameField.requestFocus();
return;
}}

Tidak ada komentar:

Posting Komentar