Friday, October 8, 2010

program code

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;

public class HostelAllocation extends JFrame implements ActionListener{
static String[]gender= {"Male","Famale"};
static String[] yr={"Yearo","Cont","Associate","Finale","Guarantor"};
static String[] hostel={"Mpingwe","Hylid","Nyika","Kapeni","Ndirande"};
static String[] faculty={"Applied","Commerce","Engineering ","Edu & Media St","Built Envirnment"};

static String[]roomNumber= {"2","3","2","3"};

JComboBox myBox = new JComboBox(yr);
JComboBox myBox2 = new JComboBox(hostel);
JComboBox myBox3 = new JComboBox(faculty);
JComboBox myBox4 = new JComboBox(gender);
static JComboBox myBox6 = new JComboBox(gender);








JPanel panel1 = new JPanel();

JTextField textfield1=new JTextField(20);
JTextField textfield2=new JTextField(10);
JTextField textfield3=new JTextField(10);
JTextField textfield4=new JTextField(10);
JTextField textfield5=new JTextField(15);

JPanel mypanel= new JPanel();
JLabel mylabel=new JLabel("WELCOME TO MALAWI POLYTECHNIC HOSTEL BOOKING SYSTEM");

JLabel mylabel2=new JLabel("NAME:");
JLabel mylabel3=new JLabel("REG. #:");
JLabel mylabel4=new JLabel("FACULTY:");
JLabel mylabel5=new JLabel("YEAR:");
JLabel mylabel6=new JLabel("DATE:");
JLabel mylabel7=new JLabel("TIME:");
JLabel mylabel8=new JLabel("HOSTEL:");
JLabel mylabel9=new JLabel("GENDER:");
JLabel mylabel20=new JLabel("ROOM #:");

static JButton button1=new JButton("SAVE");
JButton button2=new JButton("CONFIRM");
JButton button3=new JButton("CLEAR");
static HostelAllocation machira;
public HostelAllocation (){
super("Hostel Allocation System");
setContentPane(mypanel);




ImageIcon forwardIcon= new ImageIcon("D:\\VIN\\POLY.jpg");
JButton button4 = new JButton(forwardIcon);
button4.setToolTipText("POLY");
mypanel.add(button4);

mypanel.add(mylabel);

JScrollPane ss= new JScrollPane(panel1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
mypanel.setLayout(new GridBagLayout());
mylabel2.setFont(new Font("ALGERIAN",Font.BOLD,22));
mylabel3.setFont(new Font("ALGERIAN",Font.BOLD,22));
mylabel4.setFont(new Font("Algerian",Font.BOLD,22));
mylabel5.setFont(new Font("Algerian",Font.BOLD,22));
mylabel6.setFont(new Font("Algerian",Font.BOLD,22));
mylabel7.setFont(new Font("Algerian",Font.BOLD,22));
mylabel8.setFont(new Font("Algerian",Font.BOLD,22));
mylabel9.setFont(new Font("Algerian",Font.BOLD,22));
mylabel20.setFont(new Font("Algerian",Font.BOLD,22));
textfield1.setFont(new Font("Venderna",Font.BOLD,15));
textfield2.setFont(new Font("Venderna",Font.BOLD,15));
textfield3.setFont(new Font("Venderna",Font.BOLD,15));
textfield4.setFont(new Font("Venderna",Font.BOLD,15));
textfield5.setFont(new Font("Venderna",Font.BOLD,15));





mylabel.setFont(new Font("BROADWAY",Font.BOLD,24));

GridBagLayout mypanel1GridBagLayout=new GridBagLayout();
GridBagConstraints constraints =new GridBagConstraints();

mypanel.setLayout(mypanel1GridBagLayout);
mypanel.setBackground(Color.blue);

addItem(mypanel,button4,0,0,2,1,GridBagConstraints.CENTER);

addItem(mypanel,mylabel,0,1,2,1,GridBagConstraints.WEST);



mylabel.setBackground(Color.RED);

textfield1.setBackground(Color.white);



addItem(mypanel,mylabel2,0,2,1,1,GridBagConstraints.EAST);
addItem(mypanel,textfield1,1,2,1,1,GridBagConstraints.WEST);
textfield1 .setBackground(Color.ORANGE);

addItem(mypanel,textfield3,1,3,1,1,GridBagConstraints.WEST);
textfield3 .setBackground(Color.ORANGE);
addItem(mypanel,mylabel3,0,3,1,1,GridBagConstraints.EAST);
addItem(mypanel,mylabel4,0,4,1,1,GridBagConstraints.EAST);

addItem(mypanel,mylabel5,0,5,1,1,GridBagConstraints.EAST);
addItem(mypanel,mylabel6,0,6,1,1,GridBagConstraints.EAST);
addItem(mypanel,textfield4,1,5,3,3,GridBagConstraints.WEST);
textfield4 .setBackground(Color.ORANGE);

addItem(mypanel,button1,0,10,1,1,GridBagConstraints.EAST);
button1 .setBackground(Color.GREEN);
button1.addMouseListener(new Save());

addItem(mypanel,button3,1,10,1,1,GridBagConstraints.CENTER);
button3.addActionListener(this);


button3 .setBackground(Color.GREEN);
addItem(mypanel,button2,2,10,1,1,GridBagConstraints.WEST);
button2.addActionListener(new ConfirmListener());

button2 .setBackground(Color.GREEN);
addItem(mypanel,myBox,1,5,1,1,GridBagConstraints.WEST);
myBox .setBackground(Color.white);
addItem(mypanel,myBox2,1,7,1,1,GridBagConstraints.WEST);
myBox2 .setBackground(Color.white);

addItem(mypanel,myBox3,1,4,1,1,GridBagConstraints.WEST);
myBox3 .setBackground(Color.YELLOW);
myBox2.addActionListener(new Box2Listener());
addItem(mypanel,mylabel8,0,7,1,1,GridBagConstraints.EAST);
addItem(mypanel,myBox4,1,8,1,1,GridBagConstraints.WEST);
myBox4 .setBackground(Color.YELLOW);
mylabel.setForeground(Color.RED);
mylabel2.setForeground(Color.YELLOW);
mylabel3.setForeground(Color.YELLOW);
mylabel4.setForeground(Color.YELLOW);
mylabel5.setForeground(Color.YELLOW);
mylabel6.setForeground(Color.YELLOW);
mylabel7.setForeground(Color.YELLOW);
mylabel8.setForeground(Color.yellow);

mylabel9.setForeground(Color.YELLOW);
mylabel20.setForeground(Color.YELLOW);













addItem(mypanel,mylabel9,0,8,1,1,GridBagConstraints.EAST);
setSize(750,750);
setLocationRelativeTo(null);
setResizable(true);
setVisible(true);
setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);

}




public void actionPerformed(ActionEvent evnt){
if(evnt.getSource()==button3){
clear (); }
}

public void clear() {
textfield1.setText("");
textfield3.setText("");
textfield4.setText("");

}



private void addItem(JPanel p, JComponent c, int x,
int y, int width, int height, int align)
{
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = x;
gc.gridy = y;
gc.gridwidth = width;
gc.gridheight = height;
gc.weightx = 100.0;
gc.weighty = 100.0;
gc.insets = new Insets(5, 5, 5, 5);
gc.anchor = align;
gc.fill = GridBagConstraints.NONE;
p.add(c, gc);
}


public class ConfirmListener implements ActionListener{
public void actionPerformed(ActionEvent evt){

JFrame aFrame = new JFrame("Ezra Machira");
JPanel myPanel = new JPanel(new GridBagLayout());



addItem(myPanel, new JLabel("Nationality:"), 0, 6,2, 1,
GridBagConstraints.EAST);


addItem(myPanel, new JLabel("Nationality:"), 0, 7,2, 1,
GridBagConstraints.EAST);


// addItem(mypanel,button4,0,0,2,1,GridBagConstraints.CENTER);

addItem(mypanel,mylabel,0,1,2,1,GridBagConstraints.WEST);



mylabel.setBackground(Color.RED);

textfield1.setBackground(Color.white);



addItem(mypanel,mylabel2,0,2,1,1,GridBagConstraints.EAST);
addItem(mypanel,textfield1,1,2,1,1,GridBagConstraints.WEST);
textfield1 .setBackground(Color.ORANGE);

addItem(mypanel,textfield3,1,3,1,1,GridBagConstraints.WEST);
textfield3 .setBackground(Color.ORANGE);
addItem(mypanel,mylabel3,0,3,1,1,GridBagConstraints.EAST);
addItem(mypanel,mylabel4,0,4,1,1,GridBagConstraints.EAST);

addItem(mypanel,mylabel5,0,5,1,1,GridBagConstraints.EAST);
addItem(mypanel,mylabel6,0,6,1,1,GridBagConstraints.EAST);
addItem(mypanel,textfield4,1,5,3,3,GridBagConstraints.WEST);
textfield4 .setBackground(Color.ORANGE);

addItem(mypanel,button1,0,10,1,1,GridBagConstraints.EAST);
button1 .setBackground(Color.GREEN);
button1.addMouseListener(new Save());

addItem(mypanel,button3,1,10,1,1,GridBagConstraints.CENTER);
button3.addActionListener(this);


button3 .setBackground(Color.GREEN);
addItem(mypanel,button2,2,10,1,1,GridBagConstraints.WEST);
button2.addActionListener(new ConfirmListener());

button2 .setBackground(Color.GREEN);
addItem(mypanel,myBox,1,5,1,1,GridBagConstraints.WEST);
myBox .setBackground(Color.white);
addItem(mypanel,myBox2,1,7,1,1,GridBagConstraints.WEST);
myBox2 .setBackground(Color.white);

addItem(mypanel,myBox3,1,4,1,1,GridBagConstraints.WEST);
myBox3 .setBackground(Color.white);
myBox2.addActionListener(new Box2Listener());
addItem(mypanel,mylabel8,0,7,1,1,GridBagConstraints.EAST);
addItem(mypanel,myBox4,1,8,1,1,GridBagConstraints.WEST);
myBox4 .setBackground(Color.white);
mylabel.setForeground(Color.RED);
mylabel2.setForeground(Color.YELLOW);
mylabel3.setForeground(Color.YELLOW);
mylabel4.setForeground(Color.YELLOW);
mylabel5.setForeground(Color.YELLOW);
mylabel6.setForeground(Color.YELLOW);
mylabel7.setForeground(Color.YELLOW);
mylabel8.setForeground(Color.yellow);

mylabel9.setForeground(Color.YELLOW);
mylabel20.setForeground(Color.YELLOW);









aFrame.setSize(800,500);
aFrame.setContentPane(myPanel);
myPanel.setBackground(Color.orange);
setDefaultCloseOperation(aFrame.EXIT_ON_CLOSE);
aFrame.setVisible(true);

}}








public class Save extends MouseAdapter{
public void mouseClicked(MouseEvent e)

{
JFrame aFrame = new JFrame();
JFileChooser fChooser = new JFileChooser();
int result = fChooser.showSaveDialog(aFrame);
if(result==JFileChooser.APPROVE_OPTION){
File outFile = null;
outFile=fChooser.getSelectedFile();


if (outFile.exists())
JOptionPane.showMessageDialog(null, "File already exists");
else

try{
FileOutputStream fileStream = new FileOutputStream(outFile);
DataOutputStream dataStream = new DataOutputStream(fileStream);
dataStream.writeUTF(textfield1.getText());
dataStream.writeUTF(textfield2.getText());
dataStream.writeUTF(textfield3.getText());
dataStream.writeUTF(textfield4.getText());
dataStream.writeUTF(textfield5.getText());
dataStream.writeInt(myBox.getSelectedIndex());
dataStream.writeInt(myBox2.getSelectedIndex());
dataStream.writeInt(myBox3.getSelectedIndex());
dataStream.writeInt(myBox4.getSelectedIndex());




JLabel mylabel=new JLabel("WELCOME TO MALAWI POLYTECHNIC HOSTEL BOOKING SYSTEM");
JLabel mylabel2=new JLabel("NAME:");
JLabel mylabel3=new JLabel("REG. #:");
JLabel mylabel4=new JLabel("FACULTY:");
JLabel mylabel5=new JLabel("YEAR:");
JLabel mylabel6=new JLabel("DATE:");
JLabel mylabel7=new JLabel("TIME:");
JLabel mylabel8=new JLabel("HOSTEL:");
JLabel mylabel9=new JLabel("GENDER:");
JLabel mylabel20=new JLabel("ROOM #:");




}
catch(IOException event){
JOptionPane.showMessageDialog(null, "Save with file name");

}
}
}
}
public class Box2Listener implements ActionListener{
public void actionPerformed(ActionEvent event){
String hostel = (String)myBox2.getSelectedItem();

String[]kapeni= {"22","5","56"};
String[]mpingwe= {"22","75","78"};

if(hostel.equals("Nyika")){
Nyika();

}
else if(hostel.equals("Mpingwe")){
mypanel.remove(myBox6);
String[]nyika= {"22","75","78", "22","75","78", "22","75","78", "22","75","78", "22","75","78", };
myBox6 = new JComboBox(nyika);

addItem(mypanel,mylabel20,0,9,1,1,GridBagConstraints.EAST);
addItem(mypanel,myBox6,1,9,1,1,GridBagConstraints.WEST);
machira.pack();
}
else if(hostel.equals("Kapeni")){
Kapeni();
}
else if(hostel.equals("Ndirande")){
Ndirande();

}
else if(hostel.equals("Hylid")){
mypanel.remove(myBox6);
Hylid();
machira.pack();
}



}
}
public void Nyika(){
mypanel.remove(myBox6);
String[]nyika= {"242","45","56"};
myBox6 = new JComboBox(nyika);

addItem(mypanel,mylabel20,0,9,1,1,GridBagConstraints.EAST);
addItem(mypanel,myBox6,1,9,1,1,GridBagConstraints.WEST);
machira.pack();
}

public void Ndirande(){
mypanel.remove(myBox6);
String[]ndirande= {"22","75","78", "22","75","78", "22","75","78", "22","75","78", "22","75","78", };
myBox6 = new JComboBox(ndirande);

addItem(mypanel,mylabel20,0,9,1,1,GridBagConstraints.EAST);
addItem(mypanel,myBox6,1,9,1,1,GridBagConstraints.WEST);
machira.pack();
}


public void Kapeni() {
mypanel.remove(myBox6);
String[]kapen= {"22","75","78", "22","75","78", "22","75","78", "22","75","78", "22","75","78", };
myBox6 = new JComboBox(kapen);
//JScrollPane scroll = new JScrollPane(myBox);
addItem(mypanel,mylabel20,0,9,1,1,GridBagConstraints.EAST);
addItem(mypanel,myBox6,1,9,1,1,GridBagConstraints.WEST);
machira.pack();
}
public void Hylid(){
mypanel.remove(myBox6);
String[]nyika= {"G1","G2","78", "22","75","78", "22","75","78", "22","75","78", "22","75","78", };
myBox6 = new JComboBox(nyika);
//JScrollPane scroll = new JScrollPane(myBox);
addItem(mypanel,mylabel20,0,9,1,1,GridBagConstraints.EAST);
addItem(mypanel,myBox6,1,9,1,1,GridBagConstraints.WEST);
machira.pack();
}

public static void main (String [] args){
machira=new HostelAllocation();
machira.pack();
}
}

No comments:

Post a Comment