Aim: Create an application to demonstrate the use of following GUI components & event handling.
1)Menu 2) Menuitem 3) Frame 4) FileDialog 5) FileChooser 6) TextField 7) Button 8) RadioButton 9) TextArea 10) Graphics 11) Look & Feel 12) Layout Manager 13) JTable 14) JScrollPanel
Code:-
File:- guiComponent.java
package GuiComponent;
import com.sun.java.swing.plaf.motif.MotifLookAndFeel;
import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class guiComponent extends JFrame implements ActionListener, ItemListener
{
MenuBar mb;
Menu menuFile, menuDraw;
Menu jmLooknFeel;
MenuItem miExit, miOpen, miOvel, miCircle, miSqare, i5;
JLabel lbName, lbAddress, lbSub, lbGender;
JTextField tfName;
JTextArea taAddress, ta2;
JRadioButton rbMale, rbFemale;
JCheckBox cbDotNet, cbAJT, cbCD, act, cbMC, cbSeminar;
JButton btnSubmit, btnClear;
ButtonGroup bgGender;
JPanel p1, p2, p3, p4, p5, p6;
String gen;
String Subjects;
String fname;
String fpath;
MyCanvac m;
DefaultTableModel dtm;
JTable tb;
Vector v;
MenuItem jmiLookWindow;
MenuItem jmiLookMatif;
MenuItem jmiLookNimbus;
WindowsLookAndFeel windowsLF;
MotifLookAndFeel motifLF;
NimbusLookAndFeel nimbusLF;
public guiComponent()
{
this.setTitle("GUI Component");
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(7, 1, 5, 5));
//menu item and menus...
menuFile = new Menu("File");
menuDraw = new Menu("Draw");
jmLooknFeel = new Menu("Look And Feel");
mb = new MenuBar();
miExit = new MenuItem("Exit");
miOpen = new MenuItem("Open");
miOvel = new MenuItem("Ovel");
miCircle = new MenuItem("Circle");
miSqare = new MenuItem("Squar");
windowsLF = new WindowsLookAndFeel();
motifLF = new MotifLookAndFeel();
nimbusLF = new NimbusLookAndFeel();
//Student info
lbName = new JLabel("Student Name:");
lbAddress = new JLabel("Address");
lbSub = new JLabel("Subject Offered");
tfName = new JTextField(20);
taAddress = new JTextArea("Address", 5, 30);
taAddress.setBorder(javax.swing.BorderFactory.createTitledBorder("address"));
lbGender = new JLabel("Gender");
bgGender = new ButtonGroup();
rbMale = new JRadioButton("Male");
rbFemale = new JRadioButton("Female");
cbDotNet = new JCheckBox("Dot Net");
cbAJT = new JCheckBox("Advance Java Technology");
cbCD = new JCheckBox("Compiler Design");
cbMC = new JCheckBox("Mobile Computing");
cbSeminar = new JCheckBox("Seminar");
btnSubmit = new JButton("SUBMIT");
btnClear = new JButton("CLEAR");
jmiLookWindow = new MenuItem("Look 1 - Window");
jmiLookMatif = new MenuItem("Look 2 - Matif");
jmiLookNimbus = new MenuItem("Look 3 - Nimbus");
// Panelwise Adding of Component
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();
p5 = new JPanel();
p6 = new JPanel();
this.setMenuBar(mb);
mb.add(menuFile);
mb.add(menuDraw);
mb.add(jmLooknFeel);
menuFile.add(miOpen);
menuFile.add(miExit);
menuDraw.add(miOvel);
menuDraw.add(miCircle);
menuDraw.add(miSqare);
jmLooknFeel.add(jmiLookWindow);
jmLooknFeel.add(jmiLookMatif);
jmLooknFeel.add(jmiLookNimbus);
//panel 1
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p1.add(lbName);
p1.add(tfName);
// panel 2
p2.setLayout(new FlowLayout(FlowLayout.LEFT));
p2.add(lbAddress);
p2.add(taAddress);
//panel 3
p3.setLayout(new FlowLayout(FlowLayout.LEFT));
p3.add(lbGender);
bgGender.add(rbMale);
bgGender.add(rbFemale);
p3.add(rbMale);
p3.add(rbFemale);
//Panel 4
p4.setLayout(new FlowLayout(FlowLayout.LEFT));
p4.add(lbSub);
p4.add(cbSeminar);
p4.add(cbMC);
p4.add(cbCD);
p4.add(cbDotNet);
p4.add(cbAJT);
// Panel 5
p5.setLayout(new FlowLayout(FlowLayout.LEFT));
p5.add(btnSubmit);
p5.add(btnClear);
//Panel 6 with Out Put
dtm = new DefaultTableModel(null, new Object[]{"StudentName", "RollNo", "Gender"});
tb = new JTable(dtm);
add(new JScrollPane(tb));
v = new Vector();
this.add(p1);
this.add(p2);
this.add(p3);
this.add(p4);
this.add(p5);
this.add(p6);
//Register Listener
btnSubmit.addActionListener(this);
btnClear.addActionListener(this);
rbMale.addItemListener(this);
rbFemale.addItemListener(this);
cbAJT.addItemListener(this);
cbDotNet.addItemListener(this);
cbMC.addItemListener(this);
cbSeminar.addItemListener(this);
cbCD.addItemListener(this);
miOpen.addActionListener(this);
miExit.addActionListener(this);
miOvel.addActionListener(this);
miSqare.addActionListener(this);
miCircle.addActionListener(this);
jmiLookMatif.addActionListener(this);
jmiLookNimbus.addActionListener(this);
jmiLookWindow.addActionListener(this);
m = new MyCanvac(1);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(miSqare)) {
System.out.print("Square is selected");
m.setV(1);
m.drawShap();
} else if (e.getSource().equals(miOvel)) {
System.out.print("Ovel is selected");
m.setV(2);
m.drawShap();
} else if (e.getSource().equals(miCircle)) {
System.out.print("Circlel is selected");
m.setV(3);
m.drawShap();
} else if (e.getSource().equals(miExit)) {
System.exit(0);
} else if (e.getSource().equals(miOpen)) {
fileMenuMenuSelected();
} else if (e.getSource().equals(btnSubmit)) {
dtm.addRow(new String[]{tfName.getText(), taAddress.getText(), gen});
} else if (e.getSource().equals(btnClear)) {
taAddress.setText("");
tfName.setText("");
rbMale.setSelected(false);
rbFemale.setSelected(false);
cbAJT.setSelected(false);
cbDotNet.setSelected(false);
cbCD.setSelected(false);
cbMC.setSelected(false);
cbSeminar.setSelected(false);
} else if (e.getSource().equals(jmiLookWindow)) {
try {
System.out.print("windows");
UIManager.setLookAndFeel(windowsLF);
SwingUtilities.updateComponentTreeUI(guiComponent.this);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(guiComponent.class.getName()).log(Level.SEVERE, null, ex);
}
} else if (e.getSource()==jmiLookMatif) {
try {
System.out.print("Matif");
UIManager.setLookAndFeel(motifLF);
SwingUtilities.updateComponentTreeUI(guiComponent.this);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(guiComponent.class.getName()).log(Level.SEVERE, null, ex);
}
} else if (e.getSource()==jmiLookNimbus) {
try {
System.out.print("Nibus");
UIManager.setLookAndFeel(nimbusLF);
SwingUtilities.updateComponentTreeUI(this);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(guiComponent.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource().equals(rbMale)) {
if (rbMale.isSelected()) {
gen = "Gender:Male";
}
} else if (e.getSource().equals(rbFemale)) {
if (rbFemale.isSelected()) {
gen = "Gender:Female";
}
}
if (e.getItem().equals(cbDotNet)) {
if (cbDotNet.isSelected()) {
Subjects = "Dot Net";}
}
if (e.getItem().equals(cbSeminar)) {
if (cbSeminar.isSelected()) {
Subjects = Subjects + "\tSeminar";
}
}
if (e.getItem().equals(cbAJT)) {
if (cbAJT.isSelected()) {
Subjects = Subjects + "\tAdvance Java";
}
}
if (e.getItem().equals(cbCD)) {
if (cbCD.isSelected()) {
Subjects = Subjects + "\tCompiler Design";
}
}
if (e.getItem().equals(cbMC)) {
if (cbMC.isSelected()) {
Subjects = Subjects + "\tMobile Computing";
}
}
}
private void fileMenuMenuSelected() {
JFileChooser js = new JFileChooser();
int ret = js.showOpenDialog(this);
fname = js.getSelectedFile().getName();
fpath = js.getSelectedFile().getPath();
System.out.println(fname);
System.out.println(fpath);
File f = new File(fpath);
try {
FileInputStream fo = new FileInputStream(f);
byte b[] = new byte[(int) f.length()];
fo.read(b, 0, (int) f.length());
String s = new String(b);
JFrame f1 = new JFrame("File " + fname);
JTextArea ta = new JTextArea(s);
f1.add(ta);
f1.setVisible(true);
f1.setSize(500, 500);
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String args[]) {
guiComponent g = new guiComponent();
}
}
MyCanvac.java
package GuiComponent;
import java.awt.Canvas;
import java.awt.Graphics;
import javax.swing.JFrame;
public class MyCanvac extends Canvas{//extends Canvas{
int v;
public int getV() {
return v;
}
public void setV(int v) {
this.v = v;
}
public MyCanvac(int v) {
this.v = v;
System.out.print("MyCanvac : v "+ v );
}
@Override
public void paint(Graphics g) {
if (v ==1) {
g.drawRect(5, 5, 500, 500);
}else if(v==2)
{ g.drawOval(5, 5, 250, 100);}
else if(v==3){
g.drawOval(5, 5, 100, 100);
}
}
void drawShap()
{ JFrame f = new JFrame();
f.add(this);
f.setVisible(true);
repaint();
}
}
OUTPUT:
1.File Open
2.Draw->Circle
3. Submit
4. Clear
5.Look And Feel->Nimbus
1)Menu 2) Menuitem 3) Frame 4) FileDialog 5) FileChooser 6) TextField 7) Button 8) RadioButton 9) TextArea 10) Graphics 11) Look & Feel 12) Layout Manager 13) JTable 14) JScrollPanel
Code:-
File:- guiComponent.java
package GuiComponent;
import com.sun.java.swing.plaf.motif.MotifLookAndFeel;
import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class guiComponent extends JFrame implements ActionListener, ItemListener
{
MenuBar mb;
Menu menuFile, menuDraw;
Menu jmLooknFeel;
MenuItem miExit, miOpen, miOvel, miCircle, miSqare, i5;
JLabel lbName, lbAddress, lbSub, lbGender;
JTextField tfName;
JTextArea taAddress, ta2;
JRadioButton rbMale, rbFemale;
JCheckBox cbDotNet, cbAJT, cbCD, act, cbMC, cbSeminar;
JButton btnSubmit, btnClear;
ButtonGroup bgGender;
JPanel p1, p2, p3, p4, p5, p6;
String gen;
String Subjects;
String fname;
String fpath;
MyCanvac m;
DefaultTableModel dtm;
JTable tb;
Vector v;
MenuItem jmiLookWindow;
MenuItem jmiLookMatif;
MenuItem jmiLookNimbus;
WindowsLookAndFeel windowsLF;
MotifLookAndFeel motifLF;
NimbusLookAndFeel nimbusLF;
public guiComponent()
{
this.setTitle("GUI Component");
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(7, 1, 5, 5));
//menu item and menus...
menuFile = new Menu("File");
menuDraw = new Menu("Draw");
jmLooknFeel = new Menu("Look And Feel");
mb = new MenuBar();
miExit = new MenuItem("Exit");
miOpen = new MenuItem("Open");
miOvel = new MenuItem("Ovel");
miCircle = new MenuItem("Circle");
miSqare = new MenuItem("Squar");
windowsLF = new WindowsLookAndFeel();
motifLF = new MotifLookAndFeel();
nimbusLF = new NimbusLookAndFeel();
//Student info
lbName = new JLabel("Student Name:");
lbAddress = new JLabel("Address");
lbSub = new JLabel("Subject Offered");
tfName = new JTextField(20);
taAddress = new JTextArea("Address", 5, 30);
taAddress.setBorder(javax.swing.BorderFactory.createTitledBorder("address"));
lbGender = new JLabel("Gender");
bgGender = new ButtonGroup();
rbMale = new JRadioButton("Male");
rbFemale = new JRadioButton("Female");
cbDotNet = new JCheckBox("Dot Net");
cbAJT = new JCheckBox("Advance Java Technology");
cbCD = new JCheckBox("Compiler Design");
cbMC = new JCheckBox("Mobile Computing");
cbSeminar = new JCheckBox("Seminar");
btnSubmit = new JButton("SUBMIT");
btnClear = new JButton("CLEAR");
jmiLookWindow = new MenuItem("Look 1 - Window");
jmiLookMatif = new MenuItem("Look 2 - Matif");
jmiLookNimbus = new MenuItem("Look 3 - Nimbus");
// Panelwise Adding of Component
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();
p5 = new JPanel();
p6 = new JPanel();
this.setMenuBar(mb);
mb.add(menuFile);
mb.add(menuDraw);
mb.add(jmLooknFeel);
menuFile.add(miOpen);
menuFile.add(miExit);
menuDraw.add(miOvel);
menuDraw.add(miCircle);
menuDraw.add(miSqare);
jmLooknFeel.add(jmiLookWindow);
jmLooknFeel.add(jmiLookMatif);
jmLooknFeel.add(jmiLookNimbus);
//panel 1
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p1.add(lbName);
p1.add(tfName);
// panel 2
p2.setLayout(new FlowLayout(FlowLayout.LEFT));
p2.add(lbAddress);
p2.add(taAddress);
//panel 3
p3.setLayout(new FlowLayout(FlowLayout.LEFT));
p3.add(lbGender);
bgGender.add(rbMale);
bgGender.add(rbFemale);
p3.add(rbMale);
p3.add(rbFemale);
//Panel 4
p4.setLayout(new FlowLayout(FlowLayout.LEFT));
p4.add(lbSub);
p4.add(cbSeminar);
p4.add(cbMC);
p4.add(cbCD);
p4.add(cbDotNet);
p4.add(cbAJT);
// Panel 5
p5.setLayout(new FlowLayout(FlowLayout.LEFT));
p5.add(btnSubmit);
p5.add(btnClear);
//Panel 6 with Out Put
dtm = new DefaultTableModel(null, new Object[]{"StudentName", "RollNo", "Gender"});
tb = new JTable(dtm);
add(new JScrollPane(tb));
v = new Vector();
this.add(p1);
this.add(p2);
this.add(p3);
this.add(p4);
this.add(p5);
this.add(p6);
//Register Listener
btnSubmit.addActionListener(this);
btnClear.addActionListener(this);
rbMale.addItemListener(this);
rbFemale.addItemListener(this);
cbAJT.addItemListener(this);
cbDotNet.addItemListener(this);
cbMC.addItemListener(this);
cbSeminar.addItemListener(this);
cbCD.addItemListener(this);
miOpen.addActionListener(this);
miExit.addActionListener(this);
miOvel.addActionListener(this);
miSqare.addActionListener(this);
miCircle.addActionListener(this);
jmiLookMatif.addActionListener(this);
jmiLookNimbus.addActionListener(this);
jmiLookWindow.addActionListener(this);
m = new MyCanvac(1);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(miSqare)) {
System.out.print("Square is selected");
m.setV(1);
m.drawShap();
} else if (e.getSource().equals(miOvel)) {
System.out.print("Ovel is selected");
m.setV(2);
m.drawShap();
} else if (e.getSource().equals(miCircle)) {
System.out.print("Circlel is selected");
m.setV(3);
m.drawShap();
} else if (e.getSource().equals(miExit)) {
System.exit(0);
} else if (e.getSource().equals(miOpen)) {
fileMenuMenuSelected();
} else if (e.getSource().equals(btnSubmit)) {
dtm.addRow(new String[]{tfName.getText(), taAddress.getText(), gen});
} else if (e.getSource().equals(btnClear)) {
taAddress.setText("");
tfName.setText("");
rbMale.setSelected(false);
rbFemale.setSelected(false);
cbAJT.setSelected(false);
cbDotNet.setSelected(false);
cbCD.setSelected(false);
cbMC.setSelected(false);
cbSeminar.setSelected(false);
} else if (e.getSource().equals(jmiLookWindow)) {
try {
System.out.print("windows");
UIManager.setLookAndFeel(windowsLF);
SwingUtilities.updateComponentTreeUI(guiComponent.this);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(guiComponent.class.getName()).log(Level.SEVERE, null, ex);
}
} else if (e.getSource()==jmiLookMatif) {
try {
System.out.print("Matif");
UIManager.setLookAndFeel(motifLF);
SwingUtilities.updateComponentTreeUI(guiComponent.this);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(guiComponent.class.getName()).log(Level.SEVERE, null, ex);
}
} else if (e.getSource()==jmiLookNimbus) {
try {
System.out.print("Nibus");
UIManager.setLookAndFeel(nimbusLF);
SwingUtilities.updateComponentTreeUI(this);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(guiComponent.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource().equals(rbMale)) {
if (rbMale.isSelected()) {
gen = "Gender:Male";
}
} else if (e.getSource().equals(rbFemale)) {
if (rbFemale.isSelected()) {
gen = "Gender:Female";
}
}
if (e.getItem().equals(cbDotNet)) {
if (cbDotNet.isSelected()) {
Subjects = "Dot Net";}
}
if (e.getItem().equals(cbSeminar)) {
if (cbSeminar.isSelected()) {
Subjects = Subjects + "\tSeminar";
}
}
if (e.getItem().equals(cbAJT)) {
if (cbAJT.isSelected()) {
Subjects = Subjects + "\tAdvance Java";
}
}
if (e.getItem().equals(cbCD)) {
if (cbCD.isSelected()) {
Subjects = Subjects + "\tCompiler Design";
}
}
if (e.getItem().equals(cbMC)) {
if (cbMC.isSelected()) {
Subjects = Subjects + "\tMobile Computing";
}
}
}
private void fileMenuMenuSelected() {
JFileChooser js = new JFileChooser();
int ret = js.showOpenDialog(this);
fname = js.getSelectedFile().getName();
fpath = js.getSelectedFile().getPath();
System.out.println(fname);
System.out.println(fpath);
File f = new File(fpath);
try {
FileInputStream fo = new FileInputStream(f);
byte b[] = new byte[(int) f.length()];
fo.read(b, 0, (int) f.length());
String s = new String(b);
JFrame f1 = new JFrame("File " + fname);
JTextArea ta = new JTextArea(s);
f1.add(ta);
f1.setVisible(true);
f1.setSize(500, 500);
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String args[]) {
guiComponent g = new guiComponent();
}
}
MyCanvac.java
package GuiComponent;
import java.awt.Canvas;
import java.awt.Graphics;
import javax.swing.JFrame;
public class MyCanvac extends Canvas{//extends Canvas{
int v;
public int getV() {
return v;
}
public void setV(int v) {
this.v = v;
}
public MyCanvac(int v) {
this.v = v;
System.out.print("MyCanvac : v "+ v );
}
@Override
public void paint(Graphics g) {
if (v ==1) {
g.drawRect(5, 5, 500, 500);
}else if(v==2)
{ g.drawOval(5, 5, 250, 100);}
else if(v==3){
g.drawOval(5, 5, 100, 100);
}
}
void drawShap()
{ JFrame f = new JFrame();
f.add(this);
f.setVisible(true);
repaint();
}
}
OUTPUT:
1.File Open
2.Draw->Circle
3. Submit
4. Clear
5.Look And Feel->Nimbus
No comments:
Post a Comment