Sunteți pe pagina 1din 21

JAVA SWING PROGRAMS

1. JAVA Program to create Login Application connect another


frame.
SwingEx.java
package swings;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingEx extends JFrame
{
private static final long serialVersionUID = 1L;
public SwingEx()
{
final JFrame frame=new JFrame("JPasswordField Demo");
JLabel lblUser=new JLabel("User Name : ");
final JTextField tfUser=new JTextField(20);
lblUser.setLabelFor(tfUser);
JLabel lblPassword=new JLabel("Password : ");
final JPasswordField pfPassword=new JPasswordField(20);
lblPassword.setLabelFor(pfPassword);
JButton btnCancel=new JButton("Cancel");
JButton btnClear=new JButton("Clear");
JButton btnLogin=new JButton("Submit");
btnCancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
System.exit(0);
}
});
btnLogin.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
String usr=tfUser.getText();
@SuppressWarnings("deprecation")
String pass=pfPassword.getText();
if(usr.equals("vivek") && pass.equals("12345"))
{

JOptionPane.showMessageDialog(null,"Login

Succesful.");

}
else
{
database operations Later");

MenuDemo md=new MenuDemo();


md.setVisible(true);
md.setSize(500,500);
frame.setVisible(false);

//System.out.println("Will be performing

JOptionPane.showMessageDialog(null,"Login
Failed\nInvalid User Name or Password");
}
}
});
btnClear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
tfUser.setText("");
pfPassword.setText("");
}
});
JPanel panel=new JPanel();
panel.setLayout(new FlowLayout());
panel.add(lblUser);
panel.add(tfUser);
panel.add(lblPassword);
panel.add(pfPassword);
panel.add(btnLogin);
panel.add(btnCancel);
panel.add(btnClear);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350,125);
frame.setResizable(false);
frame.getContentPane().add(panel);
frame.setVisible(true);
}

@SuppressWarnings("unused")
public static void main(String args[])
{
SwingEx s=new SwingEx();
}

MenuDemo.java
package swings;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.naming.*;
import javax.swing.*;
@SuppressWarnings("unused")
public class MenuDemo extends JFrame
{
private static final long serialVersionUID = 1L;
MenuDemo()
{
JMenuBar mb=new JMenuBar();
setJMenuBar(mb);
JMenu file=new JMenu("File");
JMenu edit=new JMenu("Edit");
JMenu format=new JMenu("Format");
JMenu Goto=new JMenu("Go To ");
mb.add(file);
mb.add(edit);
mb.add(format);
JMenuItem New=new JMenuItem("New");
JMenuItem open=new JMenuItem("Open File..");
JMenuItem save=new JMenuItem("Save");
JMenuItem saveas=new JMenuItem("Save As..");
JMenuItem cut=new JMenuItem("Cut");
JMenuItem copy=new JMenuItem("Copy");
JMenuItem paste=new JMenuItem("Paste");
JMenuItem font=new JMenuItem("Font");
JMenuItem forward=new JMenuItem("Forward");
JMenuItem backward=new JMenuItem("Backward");
JMenuItem exit=new JMenuItem("Exit");
JMenuItem pagesetup=new JMenuItem("Page Setup");
JMenuItem print=new JMenuItem("Print..");

Goto.add(forward);
Goto.add(backward);
file.add(New);
file.add(open);
open.addActionListener(new Open());
file.addSeparator();
file.add(save);
file.add(saveas);
file.addSeparator();
file.add(pagesetup);
file.add(print);
file.addSeparator();
file.add(exit);
edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.addSeparator();
edit.add(Goto);
format.add(font);
}

public static void main(String args[])


{
MenuDemo m=new MenuDemo();
m.setSize(500,500);
m.setVisible(true);
}

------------------------------------------------------------------O/P:

-------------------------------------------------------------------

2. Program to Create Editor in java


Editor.java
package swings;
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.io.*;
class Editor extends Frame
{
private static final long serialVersionUID = 1L;
String filename;
TextArea tx;
Clipboard clip = getToolkit().getSystemClipboard();
Editor()
{
setLayout(new GridLayout(1,1));
tx = new TextArea();
add(tx);
MenuBar mb = new MenuBar();
Menu F = new Menu("file");
MenuItem n = new MenuItem("New");
MenuItem o = new MenuItem("Open");
MenuItem s = new MenuItem("Save");
MenuItem e = new MenuItem("Exit");
n.addActionListener(new New());
F.add(n);
o.addActionListener(new Open());
F.add(o);
s.addActionListener(new Save());
F.add(s);
e.addActionListener(new Exit());
F.add(e);
mb.add(F);
Menu E = new Menu("Edit");
MenuItem cut = new MenuItem("Cut");
MenuItem copy = new MenuItem("Copy");
MenuItem paste = new MenuItem("Paste");
cut.addActionListener(new Cut());
E.add(cut);

copy.addActionListener(new Copy());
E.add(copy);
paste.addActionListener(new Paste());
E.add(paste);
mb.add(E);
setMenuBar(mb);

mylistener mylist = new mylistener();


addWindowListener(mylist);

class mylistener extends WindowAdapter


{
public void windowClosing (WindowEvent e)
{
System.exit(0);
}
}
class New implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
tx.setText(" ");
setTitle(filename);
}
}
class Open implements ActionListener
{
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e)
{
FileDialog fd = new FileDialog(Editor.this, "select File",FileDialog.LOAD);
fd.show();
if (fd.getFile()!=null)
{
filename = fd.getDirectory() + fd.getFile();
setTitle(filename);
ReadFile();
}
tx.requestFocus();
}
}

class Save implements ActionListener


{
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e)
{
FileDialog fd = new FileDialog(Editor.this,"Save File",FileDialog.SAVE);
fd.show();
if (fd.getFile()!=null)
{
filename = fd.getDirectory() + fd.getFile();
setTitle(filename);
try
{
DataOutputStream d = new DataOutputStream(new
FileOutputStream(filename));
String line = tx.getText();
BufferedReader br = new BufferedReader(new
StringReader(line));
while((line = br.readLine())!=null)
{
d.writeBytes(line + "\r\n");
d.close();
}
}
catch(Exception ex)
{
System.out.println("File not found");
}
tx.requestFocus();
}
}
}
class Exit implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
void ReadFile()
{
BufferedReader d;
StringBuffer sb = new StringBuffer();
try

d = new BufferedReader(new FileReader(filename));


String line;
while((line=d.readLine())!=null)
sb.append(line + "\n");
tx.setText(sb.toString());
d.close();

}
catch(FileNotFoundException fe)
{
System.out.println("File not Found");
}
catch(IOException ioe){}
}

class Cut implements ActionListener


{
public void actionPerformed(ActionEvent e)
{
String sel = tx.getSelectedText();
StringSelection ss = new StringSelection(sel);
clip.setContents(ss,ss);
tx.replaceRange(" ",tx.getSelectionStart(),tx.getSelectionEnd());
}
}
class Copy implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String sel = tx.getSelectedText();
StringSelection clipString = new StringSelection(sel);
clip.setContents(clipString,clipString);
}
}
class Paste implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Transferable cliptran = clip.getContents(Editor.this);
try
{
String sel = (String) cliptran.getTransferData(DataFlavor.stringFlavor);
tx.replaceRange(sel,tx.getSelectionStart(),tx.getSelectionEnd());

}
catch(Exception exc)
{
System.out.println("not string flavour");
}
}
}
@SuppressWarnings("deprecation")
public static void main(String args[])
{
Frame f = new Editor();
f.setSize(500,400);
f.setVisible(true);
f.show();
}
}

o/p:
----------------------------------------------------

----------------------------------------------------

3. Construct Student registration form using swing use


appropriate control.
Probst.java
package swings;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Probst
{
@SuppressWarnings({ "rawtypes", "unchecked" })
Probst()
{
JFrame frame=new JFrame("Strudent Record");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
JLabel lblname=new JLabel("Name \t: ");
final JTextField txtname=new JTextField(50);
JLabel lblqualify=new JLabel("Qualification : ");
Object data[]={"Select Qualification","B.E(Comp)","MCA","BCA","B.
Tech.","Bsc. IT","M.Tech."};
@SuppressWarnings({ })
final JComboBox cb=new JComboBox(data);
JLabel lblsuboff=new JLabel("Subject Offered");
JLabel lblsubsel=new JLabel("Subject Selected");
Object suboff[]={"c","c++","Core Java","WCD","C#","ASP.Net"};
final DefaultListModel model=new DefaultListModel();
final JList offlist=new JList(suboff);
offlist.setVisibleRowCount(2);
final JList sellist=new JList(model);
JScrollPane jsp1=new JScrollPane();
JScrollPane jsp=new JScrollPane(offlist);

JButton btnadd=new JButton(">>");


btnadd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
model.addElement(offlist.getSelectedValue());
}
});
JButton btnremove=new JButton("<<");
btnremove.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
int selectedIndex = sellist.getSelectedIndex();
if (selectedIndex != -1)
{
model.remove(selectedIndex);
}
}
});
JButton btnregister=new JButton("Register");
btnregister.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
String str=txtname.getText();
String str1=str.toUpperCase();
int a=str.compareTo(str1);

Name.");

Succesfully.");

if(str.equals(""))
{
JOptionPane.showMessageDialog(null,"Please Enter
}
else if(a==0)
{
JOptionPane.showMessageDialog(null,"Registred
}
else

JOptionPane.showMessageDialog(null,"Ensure the
Always in Block Letter");
}

Student Name is\n

}
});
JButton btncancel=new JButton("Cancel");
btncancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
System.exit(0);
}
});
JButton btnreset=new JButton("Reset");
btnreset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
txtname.setText("");
cb.setSelectedIndex(0);
model.clear();
}
});
frame.add(lblname);
Dimension size=lblname.getPreferredSize();
lblname.setBounds(25,15,size.width,size.height);
frame.add(txtname);
txtname.setBounds(120,15,180,20);
frame.add(lblqualify);
lblqualify.setBounds(25,40,80,size.height);
frame.add(cb);
cb.setBounds(120,40,150,20);
frame.add(lblsuboff);
lblsuboff.setBounds(30,70,100,20);
frame.add(lblsubsel);
lblsubsel.setBounds(200,70,100,20);

frame.add(offlist);
frame.add(jsp);
jsp.getViewport().add(offlist);
offlist.setBounds(25,90,100,70);
jsp.setBounds(25,90,100,70);
frame.add(btnadd);
btnadd.setBounds(140,93,50,20);
frame.add(btnremove);
btnremove.setBounds(140,125,50,20);
frame.add(sellist);
frame.add(jsp1);
jsp1.getViewport().add(sellist);
sellist.setBounds(200,90,100,70);
jsp1.setBounds(200,90,100,70);
frame.add(btnregister);
btnregister.setBounds(25,170,85,20);
frame.add(btncancel);
btncancel.setBounds(125,170,80,20);
frame.add(btnreset);
btnreset.setBounds(220,170,80,20);

frame.setVisible(true);
frame.setSize(325,230);
frame.setResizable(false);

@SuppressWarnings("unused")
public static void main(String[] args)
{
Probst st=new Probst();
}
}

O/P:
-------------------------------------------------------------------

-------------------------------------------------------------------

4. Create GUI application to accept basic salary details from


user like basic salary, HRA, DA, PF, I-TAX to find total salary
and salary in hand.
LePs2.java
package swings;
import javax.swing.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class LePs2 extends JFrame
{
double hra=0;
double da=0;
double tsal=0;
double bsal=0;
double sihand=0;
double pf=0;
double itax=0;
LePs2()
{
this.setLayout(null);
JLabel lblname=new JLabel("Name");
JTextField txtname=new JTextField(20);
JLabel lblbsal=new JLabel("Basic Salary");
final JTextField txtbsal=new JTextField(20);
JLabel lbltsal=new JLabel("Total salary");
final JTextField txttsal=new JTextField(20);
txttsal.setEditable(false);
JLabel lblhra=new JLabel("HRA");
final JCheckBox cbhra=new JCheckBox();
JLabel lblda=new JLabel("DA");
final JCheckBox cbda=new JCheckBox();
JLabel lblpf=new JLabel("PF");

JLabel lblitax=new JLabel("I-Tax");


final JCheckBox cbpf=new JCheckBox();
final JCheckBox cbitax=new JCheckBox();
JLabel lblsalinhand=new JLabel("Salary in Hand");
final JTextField txtsalinhand=new JTextField(50);
txtsalinhand.setEditable(false);
JButton btncalculate=new JButton("Calculate");
btncalculate.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0)
{
boolean b = false;
try
{
bsal=Double.parseDouble(txtbsal.getText());
}
catch(Exception e)
{
b=true;
}
hra=bsal*0.10;
da=bsal*0.15;

Integers Only.");

if(b==true)
{
JOptionPane.showMessageDialog(null,"Enter
}
else if(cbhra.isSelected() && cbda.isSelected())
{
tsal=bsal+hra+da;
txttsal.setText(String.valueOf(tsal));
}
else if(cbhra.isSelected() && (cbda.isSelected()==false))
{
tsal=bsal+hra;
txttsal.setText(String.valueOf(tsal));
}
else if(cbda.isSelected() && (cbhra.isSelected()==false))
{

}
else
{

tsal=bsal+da;
txttsal.setText(String.valueOf(tsal));

tsal=bsal;
txttsal.setText(String.valueOf(tsal));

pf=tsal*0.12;
itax=tsal*0.02;
if(cbpf.isSelected() && cbitax.isSelected())
{
sihand=tsal-(pf+itax);
txtsalinhand.setText(String.valueOf(sihand));
}
else if(cbpf.isSelected() && (cbitax.isSelected()==false))
{
sihand=tsal-pf;
txtsalinhand.setText(String.valueOf(sihand));
}
else if(cbitax.isSelected() && (cbpf.isSelected()==false))
{
sihand=tsal-itax;
txtsalinhand.setText(String.valueOf(sihand));
}
else
{
sihand=tsal;
txtsalinhand.setText(String.valueOf(sihand));
}

});

this.add(lblname);
lblname.setBounds(25,20,40,20);
this.add(txtname);
txtname.setBounds(110,20,120,20);
this.add(lblbsal);
lblbsal.setBounds(25,50,100,20);
this.add(txtbsal);
txtbsal.setBounds(110,50,120,20);

this.add(lblhra);
lblhra.setBounds(25,75,40,20);
this.add(cbhra);
cbhra.setBounds(65,75,20,20);
this.add(lblda);
lblda.setBounds(25,100,40,20);
this.add(cbda);
cbda.setBounds(65,100,20,20);
this.add(lbltsal);
lbltsal.setBounds(25,125,100,20);
this.add(txttsal);
txttsal.setBounds(110,125,120,20);
this.add(lblpf);
lblpf.setBounds(25,160,40,20);
this.add(cbpf);
cbpf.setBounds(65,160,20,20);
this.add(lblitax);
lblitax.setBounds(25,185,40,20);
this.add(cbitax);
cbitax.setBounds(65,185,20,20);
this.add(lblsalinhand);
lblsalinhand.setBounds(25,215,120,20);
this.add(txtsalinhand);
txtsalinhand.setBounds(110,215,120,20);

this.add(btncalculate);
btncalculate.setBounds(70,250,100,20);

public static boolean isNumb(String str)


{
String s=str;
for (int i = 0; i < s.length(); i++) {

//If we find a non-digit character we return false.


if (!Character.isDigit(s.charAt(i)))
return false;
}
return true;

public static void main(String[] args)


{
LePs2 ab=new LePs2();
ab.setVisible(true);
ab.setSize(260,320);
ab.setResizable(false);
}

O/P:
----------------------------------------------------

----------------------------------------------------

S-ar putea să vă placă și