17 July 2011

Java Examples JDK6 + Useful Packages

JAVA EXAMPLES JDK6.0 USEFUL PACKAGES
Some Useful Packages Used Here
I just  want to share my work/project while i was a student way back 2years ago. 
My project was a lesson system. 

LESSON SYSTEM
Simple Approach
  • add question(admin)
  • user take a question
  • Result in Average
  • my question saves in a textfile
It has the similarities in taking exam in CISCO. I used JDK6.0 here so in running low version here some of the components doesnt support yet,for now im sure it is already above version in 6.0, JCREATOR for the Editing and Compiling.

import java.util
tools for time calendars

import java.awt
it contains container of the component layouts (FloatLayout, BorderLayout, GridLayout)

import javax.swing
Contains the core Swing components, including most of the model interfaces and support
classes.

import javax.swing.border
I like this cause it is the same FRAME Component in Visual Basic

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.SwingUtilities;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import java.awt.Toolkit;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.JOptionPane;
import java.awt.TextField;
import java.text.SimpleDateFormat;
import javax.swing.border.TitledBorder;

SCREENSHOTS

It has
  • Tab component ( images on each tab )
  • Radio Button
  • Title Icon
  • Button Image
  • GIF images
  • Mouse Over Images
  • Loading Bar in successful login
  • Selection Menu
  • Timer
  • File Based 
  • Simple Encryption (ceasar encryption method)
  • and more cool components!  :D






One sample code:
myLogin.java

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.SwingUtilities;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import java.awt.Toolkit;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.JOptionPane;
import java.awt.TextField;
import java.text.SimpleDateFormat;
import javax.swing.border.TitledBorder;




public class myLogin extends JFrame implements ActionListener{
String mabzusers="Admin";
String mabzpass="mabz";
javax.swing.Timer timer;
     String s1;
mabzFancyButton b1;
mabzFancyButton b2;
mabzFancyButton b3;

     SimpleDateFormat timef = new SimpleDateFormat("HH:mm:ss - MM/dd/yyyy");
     
private JPanel _jpLogin = new JPanel();
private JLabel _jlImg = new JLabel("");
private JPanel _jpImg = new JPanel();
private ImageIcon img = new ImageIcon("IMAGES/mabzheader.gif");
private JLabel _jlUser = new JLabel("Username: ");
private JLabel _jlPass = new JLabel("Password: ");
private JTextField _txtUser = new JTextField(10);
//private TextField _txtPass = new TextField(10);
private JPasswordField _txtPass = new JPasswordField();
private JButton _jbLogin = new JButton("Login",new ImageIcon("IMAGES/log.gif"));
private JButton _jbCancel = new JButton("Cancel",new ImageIcon("IMAGES/cancel.gif"));
private JLabel _date = new JLabel("");

public myLogin(){
//_txtPass.setEchoCharacter('m');
_txtPass.setEchoChar('m');
b1 = new mabzFancyButton(
new ImageIcon("images/log_out.gif"),
new ImageIcon("images/log_over.gif"),
new ImageIcon("images/log_over.gif"));
b2 = new mabzFancyButton(
new ImageIcon("images/cancel_out.gif"),
new ImageIcon("images/cancel_over.gif"),
new ImageIcon("images/cancel_over.gif"));

b3 = new mabzFancyButton(
new ImageIcon("images/info_over.gif"),
new ImageIcon("images/info.gif"),
new ImageIcon("images/info.gif"));
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);


Calendar cal = Calendar.getInstance ();
Date now = new Date ();
cal.setTime (now);
final String footer = String.valueOf(cal.getTime());
//_date = new JLabel(footer);
Container c = this.getContentPane();
_jbLogin.setMnemonic('L');
_jbCancel.setMnemonic('C');


c.setLayout(new FlowLayout(FlowLayout.CENTER));
_jlImg.setIcon(img);

_jpImg.setLayout(new FlowLayout());
_jpImg.add(_jlImg);

c.add(_jpImg);
setIconImage(new ImageIcon("IMAGES/signin.gif").getImage());
String _myfoot = "© Copyright and Copyleft 2009 MabZiCLe ";
JLabel foot = new JLabel(_myfoot);

foot.setForeground(new Color(200,200,200));
_jpLogin.setBackground(Color.white);
_jpLogin.setLayout(new GridLayout(4,2,5,5));
_jpLogin.add(_jlUser); _jpLogin.add(_txtUser);
_jpLogin.add(_jlPass); _jpLogin.add(_txtPass);
// _jpLogin.add(_jbLogin); _jpLogin.add(_jbCancel);
_jpLogin.add(b1);_jpLogin.add(b2);
_jpLogin.add(_date); _jpLogin.add(b3);

TitledBorder titled = new TitledBorder("Security");
_jpLogin.setBorder(titled);


_jbCancel.setSelected(true);
_jbCancel.addActionListener(this);
_jbLogin.addActionListener(this);



c.add(_jpLogin);

c.add(foot);

setResizable(false);
timer = new javax.swing.Timer(1000, new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                 s1 = timef.format(new Date(System.currentTimeMillis()));
               
                 s1 = "Time: " +s1;
  
                 _date.setText(s1);
                 
             }
         });
         timer.start();
          setTitle("MabZiCle Collections Inc.");
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae){

if (ae.getSource()==b1){
String p=_txtPass.getText();
String u=_txtUser.getText();
if (p.equals("") || u.equals("")){
JOptionPane.showMessageDialog(null,"Supply the missing field","MabZiCLe",JOptionPane.WARNING_MESSAGE);
}
if(p.equalsIgnoreCase(mabzpass) && (u.equalsIgnoreCase(mabzusers)) || u.equalsIgnoreCase("User") ){
JOptionPane.showMessageDialog(null,"good");
AdminMenu ap=new AdminMenu(u);
ap.setVisible(true);
ap.centerMe();
//successfull both pasword n username
if(u.equalsIgnoreCase("admin")){
this.setVisible(false);
// AdminMenu ap=new AdminMenu(u);
// ap.setVisible(true);
// ap.centerMe();

}
//goto admin window
else{ //you must take a lesson ;
this.setVisible(false);
UserPage up=new UserPage(u);
up.setVisible(true);
up.centerMe();
}

}
else {


JOptionPane.showMessageDialog(null,"Username/Password Does Not Match!","Login Failed",JOptionPane.WARNING_MESSAGE);
}



}
else if(ae.getSource()==b2){
//just quit..ahmmm
System.exit(0);
}
else{
JOptionPane.showMessageDialog(null,"Information");
Author mabz = new Author();
mabz.centerMe();
mabz.setVisible(true);
}
}
public void centerMe(){
Dimension sz= Toolkit.getDefaultToolkit().getScreenSize();
int sw=sz.width;
int sh=sz.height;

Dimension fz = this.getSize();
int x=(sw-fz.width)/2;
int y=(sh-fz.height)/2;

if(x<0){
x=0;
fz.width=sw;

}
if(y<0){
y=0;
fz.height=sh;
}
this.setLocation(x,y);
}



}







All One-Package Samples Here





Unlock Your Creativity | Unwinds
Advertisement


Featured Offers:
Advertisement