Sie sind auf Seite 1von 24

S.

NO
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

PROGRAMS
Writing Of Text On Frame Window Creation Of Single Button Using Inner Class Creation Of 4 Buttons Using Single Listener Object Creation Of 3 Buttons Using Different Listener Objects Drawing Of 2D Shapes Action Object AbstractAction class Window Listener Mouse Listener - Drawing of rectangle based on mouse click event Mouse Motion Listener Key Listener - Notepad simulation Layout Management - Form design Exceptions

1.WRITING OF TEXT ON FRAME WINDOW


PROGRAM:
import javax.swing.*; import java.awt.*;

import java.awt.event.*; public class frametest extends JFrame { public frametest() { setSize(400,200); Mycomp m=new Mycomp(); add(m); } public static void main(String[] args){ frametest frm=new frametest(); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setTitle("SIMPLE FRAME PROGRAME"); frm.setVisible(true); } } class Mycomp extends JComponent{ public void paintComponent(Graphics g) {int a=100,b=100; for(int i=1;i<=10;i++){ g.drawString("2*"+i+"="+(2*i), a,b); b=b+25;} } }

OUTPUT:

2.CREATION OF SINGLE BUTTON USING INNER CLASS


PROGRAM:
import javax.swing.*; import java.awt.*; import java.awt.event.*; class frametest { public static void main(String[] args) { EventQueue.invokeLater(new Runnable(){ public void run() { frame f=new frame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setTitle("SIMPLE FRAME PROGRAME");

} } });

f.setVisible(true);

} class listener implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("Button is Clicked"); JOptionPane.showMessageDialog(null, e.getActionCommand()); } } public class frame extends JFrame { public frame() { JPanel p=new JPanel(); JButton b=new JButton("CLICK"); p.add(b); add(p); setSize(400,200); listener l=new listener(); b.addActionListener(l); } }

OUTPUT:

3.CREATION OF 4 BUTTONS USING SINGLE LISTENER OBJECT


PROGRAM:
import javax.swing.*; import java.awt.*; import java.awt.event.*; class frametest { public static void main(String[] args) { EventQueue.invokeLater(new Runnable(){ public void run() { frame f=new frame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setTitle("SIMPLE FRAME PROGRAME"); f.setVisible(true); } }); }} public class frame extends JFrame { private JPanel p;

private Color old; public frame() { p=new JPanel(); JButton b1=new JButton("RED"); p.add(b1); JButton b2=new JButton("BLUE"); p.add(b2); JButton b3=new JButton("GREEN"); p.add(b3); JButton b4=new JButton("DEFAULT"); p.add(b4); add(p); setSize(400,200); listener l=new listener(); b1.addActionListener(l); b2.addActionListener(l); b3.addActionListener(l); b4.addActionListener(); } class listener implements ActionListener { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, e.getActionCommand()); if(e.getActionCommand().equals("RED")) p.setBackground(Color.red); else if(e.getActionCommand().equals("BLUE")) p.setBackground(Color.blue); else if(e.getActionCommand().equals("GREEN")) p.setBackground(Color.green); else p.setBackground(old); } }}

OUTPUT:

4.CREATION OF 3 BUTTONS USING DIFFERENT LISTENER OBJECTS


PROGRAM:
import javax.swing.*; import java.awt.*; import java.awt.event.*; class frametest { public static void main(String[] args) { EventQueue.invokeLater(new Runnable(){ public void run() { frame f=new frame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setTitle(SIMPLE FRAME PROGRAME); f.setVisible(true); } });

} public class frame extends Jframe { private Jpanel p; public frame() { p=new Jpanel(); Jbutton b1=new Jbutton(RED); p.add(b1); Jbutton b2=new Jbutton(BLUE); p.add(b2); Jbutton b3=new Jbutton(GREEN); p.add(b3); add(p); setSize(400,200); listener l1=new listener(Color.red); listener l2=new listener(Color.blue); listener l3=new listener(Color.green); b1.addActionListener(l1); b2.addActionListener(l2); b3.addActionListener(l3); } class listener implements ActionListener { private Color bk; public listener(Color c) { bk=c; } public void actionPerformed(ActionEvent e) { p.setBackground(bk); }} }

OUTPUT:

5.DRAWING OF 2D SHAPES
PROGRAM:
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class frame extends JFrame { public frame() { setSize(400,200); Mycomp m=new Mycomp(); add(m); Drawcomp d=new Drawcomp(); add(d); } public static void main(String[] args){ frame frm=new frame(); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setTitle("SIMPLE FRAME PROGRAME"); frm.setVisible(true); } } class Mycomp extends JComponent{ public void paintComponent(Graphics g) { g.drawString("MY FIRST TEXT", 100, 100); } } class Drawcomp extends JPanel{ public void paintComponent(Graphics g) { Graphics2D g2=(Graphics2D ) g; g2.setStroke(new BasicStroke(10)); g2.setColor(new Color(0,128,128));

}}

Rectangle2D.Double r=new Rectangle2D.Double(100,100,40,60); g2.draw(r); Line2D.Double l=new Line2D.Double(200,100,200,300); g2.draw(l); Font font1=new Font("Cosmic Sans MS",Font.BOLD,23); g2.setFont(font1); g2.drawString("hello",400,100); Ellipse2D.Double e=new Ellipse2D.Double(300,100,40,60); g2.draw(e); g2.rotate(10,350,50); g2.draw(r);

OUTPUT:

6.ABSTRACT ACTION
PROGRAM:
import java.awt.*; import java.awt.event.ActionEvent; import javax.swing.*;

public class frame extends JFrame { private JPanel button_panel; public frame() { setTitle("actions"); setSize(300,300); button_panel=new JPanel(); Action yaction1=new coloraction("red",Color.red); Action yaction2=new coloraction("yellow",Color.yellow); Action yaction3=new coloraction("gray",Color.gray); JButton b=new JButton(yaction1); JButton b1=new JButton(yaction2); JButton b2=new JButton(yaction3); button_panel.add(b); button_panel.add(b1); button_panel.add(b2); add(button_panel); InputMap imap=button_panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOC USED_COMPONENT); imap.put(KeyStroke.getKeyStroke("ctrl R"), "red"); ActionMap amap=button_panel.getActionMap(); amap.put("red", yaction1); InputMap imap1=button_panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOC USED_COMPONENT); imap.put(KeyStroke.getKeyStroke("ctrl y"), "yellow"); ActionMap amap1=button_panel.getActionMap(); amap.put("yellow", yaction2); InputMap imap2=button_panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FO CUSED_COMPONENT); imap.put(KeyStroke.getKeyStroke("ctrl g"), "gray"); ActionMap amap2=button_panel.getActionMap(); amap.put("gray", yaction3); } class coloraction extends AbstractAction {

Color bk; public coloraction(String name,Color c) { bk=c; putValue(Action.NAME,name); if(c.equals(Color.red)) { putValue(Action.SMALL_ICON,new ImageIcon("z:\\red.gif")); putValue(Action.SHORT_DESCRIPTION,"set panel color to red"); } else if(c.equals(Color.yellow)) { putValue(Action.SMALL_ICON,new ImageIcon("z:\\yellow.gif")); putValue(Action.SHORT_DESCRIPTION,"set panel color to yellow"); } else { putValue(Action.SMALL_ICON,new ImageIcon("z:\\gray.gif")); putValue(Action.SHORT_DESCRIPTION,"set panel color to yellow"); } } public void actionPerformed(ActionEvent e) { button_panel.setBackground(bk); }}} import javax.swing.JFrame; public class frametest { public static void main(String[] args) { frame f=new frame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }}

Output:

7.WINDOW LISTENER
PROGRAM:
import javax.swing.*; import java.awt.event.*; import java.awt.*; class Frame extends JFrame { public Frame() { setVisible(true); setSize(500,500); setTitle("Window Listener"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Listener l=new Listener(); addWindowListener(l); } class Listener implements WindowListener

public void windowOpened(WindowEvent e) { JOptionPane.showMessageDialog(rootPane,"Window is opened"); } public void windowClosing(WindowEvent e) { JOptionPane.showMessageDialog(rootPane,"Window is to be closed"); } public void windowClosed(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) { } }} public class Test { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { Frame f=new Frame(); } }); }} OUTPUT:

8.MOUSE LISTENER-DRAWING OF RECTANGLE BASED ON MOUSE CLICK


PROGRAM:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.lang.*; public class test { public static void main(String a[]) { frame f=new frame(); f.setVisible(true); f.setSize(400, 400);

f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);

} class frame extends JFrame { public frame() { muse m=new muse(); this.addMouseListener(m); } class muse extends MouseAdapter { public void mousePressed(MouseEvent e) { x1=e.getX(); y1=e.getY(); } public void mouseReleased(MouseEvent e) { x2=e.getX(); y2=e.getY(); l=Math.abs(x2-x1); b=Math.abs(y2-y1); Graphics g=getGraphics(); g.setColor(new Color(100,10,10)); if(x1>x2) x1=x2; if(y1>y2) y1=y2; g.drawRect(x1, y1, l, b); } } private String s; private int x1,x2,y1,y2,l,b; }

OUTPUT:

9.MOUSEMOTION LISTENER
PROGRAM:
import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class mouse { public static void main(String[] args) { frame f=new frame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setTitle("MOUSE"); f.setVisible(true); } } class frame extends JFrame{ public frame() {

setSize(400,500); mouselisten m=new mouselisten(); add(m); addMouseMotionListener(m); } } class mouselisten extends JPanel implements MouseMotionListener { int x,y; public void paintComponent(Graphics g) { g.drawLine(x,y,x+50,y+50); } public void mouseDragged(MouseEvent e){ } public void mouseMoved(MouseEvent e){ x=e.getX(); y=e.getY(); this.repaint(); } }

OUTPUT:

10. KEYLISTENER-NOTEPAD SIMULATION

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class framecheck { public static void main(String[] args){ frame frm=new frame(); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setVisible(true); } } class frame extends JFrame{ public frame(){ setSize(500,500); listener key=new listener(); add(key);

}} class listener extends JPanel implements KeyListener{ String text=""; int code; public void paintComponent(Graphics g){ super.paintComponent(g); g.drawString(text,10,20); } public void keyReleased(KeyEvent k){} public void keyTyped(KeyEvent k){} public void keyPressed(KeyEvent k){ code=k.getKeyCode(); if(code==20){ text=text+k.getKeyText(code); } text=text+(k.getKeyChar()); this.repaint(); } } OUTPUT:

addKeyListener(key);

11.LAYOUT MANAGEMENT - FORM DESIGN

frame.java import java.awt.event.*; import javax.swing.*; import javax.swing.ButtonGroup.*; import java.awt.*; public class frame extends JFrame { JPanel p; JTextField t1,t2,t3; JTextArea t4; ButtonGroup b3; JRadioButton f,m; public frame() { setSize(400,500); JButton b1=new JButton("Submit"); JButton b2=new JButton("Reset"); JLabel l1=new JLabel("User name "); JLabel l2=new JLabel("Sex"); JLabel l3=new JLabel("Qualification"); JLabel l4=new JLabel("Address"); t1=new JTextField(); //t2=new JTextField(); f=new JRadioButton("Female"); m=new JRadioButton("Male"); b3=new ButtonGroup(); b3.add(f); b3.add(m); JPanel pp=new JPanel(); pp.add(f); pp.add(m); t3=new JTextField(); t4=new JTextArea(); p=new JPanel(new GridLayout(6,2)); p.add(l1); p.add(t1); p.add(l2); p.add(pp);

p.add(l3); p.add(t3); p.add(l4); p.add(t4); p.add(b1); p.add(b2); add(p); Action n=new Action(); b1.addActionListener(n); b2.addActionListener(n); } class Action implements ActionListener { public void actionPerformed(ActionEvent e) { String ss; if(e.getActionCommand().equals("Submit")) { if (f.isSelected()) { ss="female"; } else ss="male"; String s="The user "+t1.getText()+" Gender: "+ss+" ," +t3.getText() +" from "+t4.getText()+" has submitted the form"; JOptionPane.showMessageDialog(null, s) ; } else if(e.getActionCommand().equals("Reset")) { t1.setText(" "); f.setSelected(false); m.setSelected(false); t3.setText(" "); t4.setText(" "); } } } }

Test.java
import java.awt.event.*; import javax.swing.*; import javax.swing.ButtonGroup.*; import java.awt.*; public class Test { public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { public void run() { frame f=new frame(); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }); } } Output:

12.EXCEPTIONS

import java.util.*; import java.lang.Throwable.*; public class userexcep { public static void main(String [ ] args) { String s="INDIA"; System.out.println("enter the string:"); Scanner read=new Scanner(System.in); String s1=read.next(); if(s1.equals(s)) System.out.println("dd"); else throw new NoMatchException("hello"); } } class NoMatchException extends RuntimeException { public NoMatchException(String m) { System.out.println(m); } } OUTPUT: enter the string: JAVA hello Exception in thread "main" NoMatchException at userexcep.main(userexcep.java:22) Java Result: 1

Das könnte Ihnen auch gefallen