Anda di halaman 1dari 19

prog 1 Write a program to make GUI Login page by using awt.

Provide username password and server field on login page. Server field is drop down box. Validate user Information from login.txt file. import java.lang.*; import java.awt.*; class Pro_1 extends Frame { public static void main(String[] a) { Pro_1 l=new Pro_1(); l.setVisible(true); } public Pro_1() { setLayout(new GridLayout(4,2)); Label lblName=new Label("User Name:"); Label lblPwd=new Label("Password:"); TextField tfName=new TextField(); TextField tfPwd=new TextField(); Button btnP1=new Button("Close"); Button btnReset=new Button("Reset"); Checkbox chkRemember=new Checkbox("Remember Me",false); add(lblName); add(tfName); add(lblPwd); add(tfPwd); add(new Label(" ")); add(chkRemember); add(btnP1); add(btnReset); } } prog 2 Make a calculator application import java.lang.*; import java.awt.*; class Pro_2 extends Frame { public static void main(String[] a) { Pro_2 calc=new Pro_2(); calc.setVisible(true); } public Pro_2() {

setLayout(new BorderLayout()); TextField txtDisp=new TextField("0."); Panel p2=new Panel(); p2.setLayout(new GridLayout(2,1)); p2.add(txtDisp); Panel p1=new Panel(); p1.setLayout(new GridLayout(1,3)); p1.add(new Button("BackSpace")); p1.add(new Button("CE")); p1.add(new Button("C")); p2.add(p1); add(p2, BorderLayout.NORTH); Panel p=new Panel(); GridLayout gl=new GridLayout(4,5); gl.setHgap(2); gl.setVgap(2); p.setLayout(gl); p.add(new Button("MC")); p.add(new Button("7")); p.add(new Button("8")); p.add(new Button("9")); p.add(new Button("/")); p.add(new Button("sqrt")); p.add(new Button("MR")); p.add(new Button("4")); p.add(new Button("5")); p.add(new Button("6")); p.add(new Button("*")); p.add(new Button("%")); p.add(new Button("MS")); p.add(new Button("1")); p.add(new Button("2")); p.add(new Button("3")); p.add(new Button("-")); p.add(new Button("1/x")); p.add(new Button("M+")); p.add(new Button("0")); p.add(new Button("+/-")); p.add(new Button(".")); p.add(new Button("+")); p.add(new Button("=")); add(p,BorderLayout.CENTER); } } 3 Write a GUI application to draw fill rectangle. Start changing the color of rectangle by clicking the Start button. Stop changing color of rectangle by clicking stop button. (Use thread to changing color. Use sleep method also) import java.lang.*;

import java.awt.*; import java.awt.event.*; class Rectangle_3 extends Frame implements ActionListener { static int R=0; static int G=0; static int B=0; Label lblColor; public static void main(String[] a) { Rectangle_3 Rect=new Rectangle_3(); } public Rectangle_3() { setBounds(0,0,700,700); setLayout(new GridLayout(2,1,500,500)); Panel p=new Panel(); Button btnChangeRed=new Button("Add Red"); p.add(btnChangeRed); Button btnChangeGreen=new Button("Add Green"); p.add(btnChangeGreen); Button btnChangeBlue=new Button("Add Blue"); p.add(btnChangeBlue); add(p); btnChangeRed.addActionListener(this); btnChangeBlue.addActionListener(this); btnChangeGreen.addActionListener(this); lblColor=new Label(" Color Change "); add(lblColor); setVisible(true); } public void actionPerformed(ActionEvent ae) { String str=ae.getActionCommand(); if(str.equals("Add Red")) { if(R>=255) R=0; else R=R+10; } else if(str.equals("Add Blue")) { if(B>=255) B=0; else B=B+10; } else if(str.equals("Add Green"))

{ if(G>=255) G=0; else G=G+10; } Color c=new Color(R,G,B); setBackground(c); } } 4 Write program to display component in GridBagLayout format. import java.awt.*; import java.util.*; import java.applet.Applet; public class Pro_4 extends Applet { protected void makebutton(String name, GridBagLayout gridbag, GridBagConstraints c) { Button button = new Button(name); gridbag.setConstraints(button, c); add(button); } public void init() { GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setFont(new Font("SansSerif", Font.PLAIN, 14)); setLayout(gridbag); c.fill = GridBagConstraints.BOTH; c.weightx = 1.0; makebutton("Button1", gridbag, c); makebutton("Button2", gridbag, c); makebutton("Button3", gridbag, c); c.gridwidth = GridBagConstraints.REMAINDER; //end row makebutton("Button4", gridbag, c); c.weightx = 0.0; //reset to the default makebutton("Button5", gridbag, c); //another row c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row makebutton("Button6", gridbag, c); c.gridwidth = GridBagConstraints.REMAINDER; //end row makebutton("Button7", gridbag, c); c.gridwidth = 1; //reset to the default c.gridheight = 2; c.weighty = 1.0; makebutton("Button8", gridbag, c); c.weighty = 0.0; //reset to the default c.gridwidth = GridBagConstraints.REMAINDER; //end row c.gridheight = 1; //reset to the default makebutton("Button9", gridbag, c);

makebutton("Button10", gridbag, c); setSize(300, 100); } public static void main(String args[]) { Frame f = new Frame("GridBag Layout Example"); Pro_4 ex1 = new Pro_4(); ex1.init(); f.add("Center", ex1); f.pack(); f.setSize(f.getPreferredSize()); f.show(); } } 5 Make a railway reservation GUI form. Calculate ticket amount by Class (like 1st class with AC, 2nd class with Ac, 2nd class with non AC and general). Also provide food facility or not. After filling information provide calculate button which shows the amount of railway ticket. (Hint Provide radio button for class and Checkbox for Food) import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; //import java.applet.*; //<applet code=Railways height=700 width=700></applet> class Railways extends Frame { JPanel up,down; JLabel appName,stFrom,stUpto,tcktClass,food,name,age,sr,dr,bearth,signature; public static void main(String[] a) { Railways tckt=new Railways(); } public Railways() { setSize(650,275); GridLayout mainPage=new GridLayout(2,1); setLayout(mainPage); up=new JPanel(); down=new JPanel(); add(up); add(down); appName=new JLabel("Applicant's Name:"); stFrom=new JLabel("Station From:"); stUpto=new JLabel("Station Upto:"); tcktClass=new JLabel("Class:"); food=new JLabel("Food Facility:"); name=new JLabel("Traveller's Name");

age=new JLabel("Age"); sr=new JLabel("Senior Citizen"); dr=new JLabel("Doctor"); bearth=new JLabel("Bearth"); signature=new JLabel("Signature"); up.setLayout(new GridLayout(5,2)); up.add(appName); up.add(new JTextField("")); up.add(stFrom); String st[] = new String[5]; st[0]="Ahmedabad"; st[1]="Gandhinagar"; st[2]="Surat"; st[3]="Nadiad"; st[4]="Vadodara"; up.add(new JComboBox(st)); up.add(stUpto); up.add(new JComboBox(st)); up.add(tcktClass); JPanel pRdio=new JPanel(); pRdio.add(new JRadioButton("First A.C.")); pRdio.add(new JRadioButton("Second A.C.")); pRdio.add(new JRadioButton("Second Sleeper")); up.add(pRdio); up.add(food); JPanel pChckbox=new JPanel(); pChckbox.add(new JCheckBox("Veg", true)); pChckbox.add(new JCheckBox("Non Veg")); up.add(pChckbox); down.setLayout(new GridLayout(4,5)); down.add(name); down.add(age); down.add(sr); down.add(dr); down.add(bearth); down.add(new JTextField("")); down.add(new JTextField("")); down.add(new JCheckBox("")); down.add(new JCheckBox("")); down.add(new JRadioButton("LB")); down.add(new JTextField("")); down.add(new JTextField("")); down.add(new JCheckBox("")); down.add(new JCheckBox("")); down.add(new JRadioButton("LB")); down.add(new JTextField("")); down.add(new JTextField("")); down.add(new JCheckBox("")); down.add(new JCheckBox(""));

down.add(new JRadioButton("LB")); setVisible(true); } } 6 Make a Student registration GUI page in java. Provide name, roll no, age field. User can not entered number in name textbox and string in age textbox. Provide add, edit functionality. Store data in file. import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; class Student { public static void main(String args[])throws IOException { Create e=new Create(); } } class Create implements ActionListener { JFrame jf=new JFrame(); JTextField txtstuname, txtsturollno, txtstuaddress,txtstudob,txtstuage, txtstudegree,txtstuspe,txtstuhobby; JLabel lblname,lblrollno,lbladdress,lbldob,lblage,lbldegree,lblspec,lblhobby; JButton badd,bcancle,bclear,bdelete; File f; ObjectOutputStream ous,fos; WriteData wr,rd; ObjectInputStream ois; Create() { try { f=new File("z:\\store.txt"); ous= new ObjectOutputStream(new FileOutputStream(f)); fos=new ObjectOutputStream(new FileOutputStream (new File("z:\\deleted.txt"))); ois=new ObjectInputStream(new FileInputStream(f)); } catch(Exception exc) { } wr=new WriteData();

FlowLayout flow=new FlowLayout(); jf.setLayout(flow); lblrollno=new JLabel("ENter the No"); lblname=new JLabel("ENter the Name"); lbladdress=new JLabel("ENter the Address"); lbldob=new JLabel("ENter the Date of birth"); lblage=new JLabel("ENter the Age"); lbldegree=new JLabel("ENter the Degree"); lblhobby=new JLabel("ENter the Hobby"); lblspec=new JLabel("ENter the Specialization");

txtstuname=new JTextField(12); txtsturollno=new JTextField(12); txtstuaddress=new JTextField(12); txtstudob=new JTextField(12); txtstuage=new JTextField(12); txtstudegree=new JTextField(12); txtstuspe=new JTextField(12); txtstuhobby=new JTextField(12); badd=new JButton("Add"); bcancle=new JButton("Cancel"); bclear=new JButton("Clear"); bdelete=new JButton("Delete"); jf.add(lblrollno); jf.add(txtsturollno); jf.add(lblname); jf.add(txtstuname); jf.add(lbladdress); jf.add(txtstuaddress); jf.add(lbldob); jf.add(txtstudob); jf.add(lblage); jf.add(txtstuage); jf.add(lbldegree); jf.add(txtstudegree); jf.add(lblhobby); jf.add(txtstuhobby);

jf.add(lblspec); jf.add(txtstuspe);

jf.add(badd); jf.add(bclear); jf.add(bdelete); jf.add(bcancle); badd.addActionListener(this); bclear.addActionListener(this); bcancle.addActionListener(this); bdelete.addActionListener(this); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); } class WriteData implements Serializable { String name,address,dob,degree,spe,hobby; int rollno,age; public void putData() { name=txtstuname.getText(); rollno=Integer.parseInt(txtsturollno.getText()); address=txtstuaddress.getText(); dob=txtstudob.getText(); age=Integer.parseInt(txtstuage.getText()); degree=txtstudegree.getText(); spe=txtstuspe.getText(); hobby=txtstuhobby.getText(); System.out.println("Name"+name); } public void printData() { System.out.println(""+rollno); System.out.println(""+name); System.out.println(""+address); System.out.println(""+dob); System.out.println(""+degree); System.out.println(""+spe); System.out.println(""+hobby);

} public void actionPerformed(ActionEvent e ) { String check; check=e.getActionCommand(); if (check=="Add") { try { wr.putData(); ous.writeObject(wr);

} catch(IOException io) { //System.out.println("error"); } wr.printData(); txtstuname.setText(""); txtsturollno.setText(""); txtstuaddress.setText(""); txtstudob.setText(""); txtstuage.setText(""); txtstudegree.setText(""); txtstuspe.setText(""); txtstuhobby.setText(""); } if(check=="Delete") { try { while(true) { rd = (WriteData)ois.readObject(); if(rd.rollno==101) { continue; }

else { fos.writeObject(rd); } } } catch(ClassNotFoundException ce) { } catch(IOException io) { }

} if(check=="Cancel") { txtstuname.setText(""); txtsturollno.setText(""); txtstuaddress.setText(""); txtstudob.setText(""); txtstuage.setText(""); txtstudegree.setText(""); txtstuspe.setText(""); txtstuhobby.setText(""); } if(check=="Clear") { txtstuname.setText(""); txtsturollno.setText(""); txtstuaddress.setText(""); txtstudob.setText(""); txtstuage.setText(""); txtstudegree.setText(""); txtstuspe.setText(""); txtstuhobby.setText(""); } }

} /* prog 7 Create menu driven application to provide 3 to 4 functionality of notepad application of windows.(Use Message, Confirm and Input Dialogs) */

import java.io.*; import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.KeyStroke; import static java.awt.event.InputEvent.*; class Notepad extends JFrame implements ActionListener { public static JTextArea text; private JFileChooser files; public static void main(String s[]) { JFrame notepad=new JFrame("Notepad"); JMenuBar menubar=new JMenuBar(); JMenu file,edit,format,view,help; text=new JTextArea("Hello,This is Alpesh From MCA-III"); JMenuItem newItem,open,save,saveas,cut,copy,paste,font,about; file=new JMenu("File"); file.setMnemonic('F'); edit=new JMenu("Edit"); edit.setMnemonic('E'); format=new JMenu("Format"); format.setMnemonic('o'); view=new JMenu("View"); view.setMnemonic('V'); help=new JMenu("Help"); help.setMnemonic('H'); newItem=file.add("New"); open=file.add("Open"); file.addSeparator(); save=file.add("Save"); saveas=file.add("Save As..."); cut=edit.add("Cut"); copy=edit.add("Copy"); paste=edit.add("Paste"); font=format.add("Font"); about=help.add("About Notepad"); cut.addActionListener(new Notepad()); copy.addActionListener(new Notepad()); paste.addActionListener(new Notepad());

newItem.addActionListener(new Notepad()); save.addActionListener(new Notepad()); open.addActionListener(new Notepad()); newItem.setAccelerator(KeyStroke.getKeyStroke('N',CTRL_DOWN_MASK )) ; open.setAccelerator(KeyStroke.getKeyStroke('O', CTRL_DOWN_MASK)); save.setAccelerator(KeyStroke.getKeyStroke('S', CTRL_DOWN_MASK)); menubar.add(file); menubar.add(edit); menubar.add(format); menubar.add(view); menubar.add(help); //text.setLineWrap(true); JScrollPane pane=new JScrollPane(text); pane.setWheelScrollingEnabled(true); notepad.setJMenuBar(menubar); Toolkit theKit = notepad.getToolkit(); Dimension window = theKit.getScreenSize(); notepad.setBounds(0,0,window.width/2,window.height/2); notepad.setDefaultCloseOperation(EXIT_ON_CLOSE); GridLayout grd=new GridLayout(1,1); Container content =notepad.getContentPane(); content.setLayout(grd); //notepad.add(grd); content.add(pane,0); notepad.setVisible(true); } public void actionPerformed(ActionEvent e) { BufferedOutputStream buf; if(e.getActionCommand().equals("Cut")) { text.cut(); } if(e.getActionCommand().equals("Paste")) { text.paste(); } if(e.getActionCommand().equals("Copy")) { text.copy(); } if(e.getActionCommand().equals("Save")) { files = new JFileChooser(System.getProperty("user.dir")); int result = files.showSaveDialog(Notepad.this);

if(result==0) { try { File myFile=files.getSelectedFile(); System.out.println(myFile.getName()); if(!myFile.exists()) { myFile.createNewFile(); buf=new BufferedOutputStream(new FileOutputStream(myFile)); buf.write(text.getText().getBytes()); buf.flush(); buf.close(); } } catch(IOException io) { System.out.println("Error occur while writing..."); } } } } } /* prog 8 Create applet which move car object on screen */

import import import import

java.lang.*; java.applet.*; java.awt.event.*; java.awt.*;

/* <applet code="Pro_8" height="500" width="500"> </applet> */

class Pro_8 extends Applet implements Runnable { Thread t; int X; int Y;

public void init() { t=new Thread(this,"MYCAR"); t.start(); } public void run() {

for(int i=1;i<=500;i++,X+=5,Y+=5) { repaint(); try { t.sleep(50); } catch(Exception e) { } } } public void Print(Graphics g) { g.setColor(Color.red); g.fillRect(180+X,100,200,50); g.setColor(Color.black); g.fillOval(250+X,150,30,30); g.fillOval(200+X,150,30,30); g.fillOval(300+X,150,30,30); g.fillOval(350+X,150,30,30); } } /* prog 9 Create Ball game application in applet. Ball should be move within the boundary of window. Applet provide menu for startgame and stopgame. If user clicks on startgame menu then ball starts to move. And stop on click of stopgame menu click. (Use thread to move a ball.) */ import java.awt.*; import java.io.*; import javax.swing.*; import java.applet.*; //<applet code=Pro_9.class height=500 width=500></applet>

public class Pro_9 extends Applet implements Runnable { Thread t; int X=45; int Y=500; Boolean down=true; public void init() { try { t=new Thread(this,"My Application"); t.start(); setSize(250,510); } catch(Exception e) { } } public void run() { for(int i=1;i>0;i++) { repaint(); try { t.sleep(20); } catch(Exception e) { } } } public void paint(Graphics g) { g.drawLine(0,10,250,10); g.drawLine(0,11,250,11); g.drawLine(0,500,250,500); g.drawLine(0,501,250,501); if(down) { if(X<=400) { g.setColor(Color.PINK);

g.fillOval(75,X,100,100); X=X+5; } else { down=false; Y=400; } } else { if(Y>10) { g.setColor(Color.ORANGE); g.fillOval(75,Y,100,100); Y=Y-5; } else { X=10; down=true; } } } } /* Create applet to calculate simple and compound interest from given principal. Add all detail by text box. Provide radio button for simple and compound interest selection. Base on this selection calculate interest on clicking of calculate button. Simple interest=PRN/100 Compound interest= (p(1-(r/100))^n) P */ /*Program 10 Create applet to calculate simple and compound interest from given principal. Add all detail by text box. Provide radio button for simple and compound interest selection. Base on this selection calculate interest on clicking of calculate button. Simple interest=PRN/100 Compound interest= (p(1-(r/100))^n) P */ import java.awt.*; import java.applet.Applet; import java.awt.event.*; /* <applet code="Demo10" width=300 height=300> </applet>

*/ public class Demo10 extends Applet implements ActionListener,ItemListener { TextField TextField TextField TextField t1; t2; t3; t4;

CheckboxGroup cg1=new CheckboxGroup(); CheckboxGroup cg2=new CheckboxGroup(); Checkbox c1; Checkbox c2; Label l1,l2,l3;

float a,b,c; public void init() { l1=new Label("Enter the p"); l2=new Label("Enter the r"); l3=new Label("Enter the n");

t1=new t2=new t3=new t4=new

TextField(" TextField(" TextField(" TextField("

"); "); "); ");

c1=new Checkbox("simple",cg1,false); c2=new Checkbox("compound",cg2,false);

add(l1);add(t1); add(l2);add(t2); add(l3);add(t3); add(c1); add(c2); add(b1); add(t4); c1.addItemListener(this); c2.addItemListener(this); } public void actionPerformed(ActionEvent ae) {

System.out.println(a+" "+b+" "+c); } public void itemStateChanged(ItemEvent ie) { Object o; String s1=(String)ie.getItem(); a=Float.parseFloat(t1.getText()); b=Float.parseFloat(t2.getText()); c=Float.parseFloat(t3.getText()); if(s1.equals("simple")) { Float f=((a*b*c)/100.0f); System.out.println(f); String s=f.toString(); t4.setText(s); } else { a(1-(b/100))^c) a; } } }

Anda mungkin juga menyukai