Posts

Showing posts from September, 2014

Frame with Menus , Dialog , Panels arranged in BorderLayout

import java.awt.*; import java.awt.event.*; /*class WindowDemo extends Window { WindowDemo(Frame myf) { super(myf); setSize(200,100); setVisible(true); setBackground(Color.red); } }*/ public class TestFrameWindow extends Frame implements ActionListener { private Button btclose,btok; private Panel pes, pws,pnr, psu,pce; private Font myf; private List mylist ; private TextArea ta; private Dialog dlg; private MenuBar mb; private Menu filem, editm, helpm, workspace; private MenuItem exitmi, openmi, openallmi; TestFrameWindow() { mb = new MenuBar(); setMenuBar(mb); filem = new Menu("File"); editm = new Menu("Edit"); helpm = new Menu("Help"); workspace = new Menu("WorkSpace.."); mb.add(filem); mb.add(editm); mb.add(helpm); exitmi = new MenuItem("Exit"); MenuShortcut shrtct = new MenuShortcut(KeyEvent.VK_X,true); exitmi.setShortcut(shrtct); exitmi.addActionL...

Various MouseEvent example in an Java Applet

import java.applet.Applet; import java.awt.Graphics; import java.awt.Color; import java.awt.event.*; public class TestApplet extends Applet implements MouseListener { int px, py; public void init() { setBackground(Color.red); this.addMouseListener(this); } public void paint(Graphics g) { g.setColor(Color.white); g.drawString("Welcome Applet", px,py); } public void mouseExited(MouseEvent me)  { showStatus("Mouse Exited"); } public void mouseEntered(MouseEvent me)  { showStatus("Mouse Entered"); } public void mousePressed(MouseEvent me)  { showStatus("Mouse Pressed"); } public void mouseReleased(MouseEvent me)  { showStatus("Mouse Released"); } public void mouseClicked(MouseEvent me)  { px = me.getX(); py = me.getY();   repaint();   showStatus("Mouse Clicked"); } }

WindowEvent example

import java.awt.*; import java.awt.event.*; public class TestWindowEvent extends Frame implements WindowListener { public TestWindowEvent() { setSize(400,500); setVisible(true); this.addWindowListener(this); } public void windowDeactivated(WindowEvent we) { System.out.println("Deactivated" ); } public void windowActivated(WindowEvent we) { System.out.println("Activated" ); } public void windowDeiconified(WindowEvent we) { System.out.println("Deiconifid" ); } public void windowIconified(WindowEvent we) { System.out.println("Iconified" ); } public void windowClosed(WindowEvent we) { System.out.println("Closed" ); } public void windowOpened(WindowEvent we) { System.out.println("Opened" ); } public void windowClosing(WindowEvent we) { System.exit(0); } public static void main(String args[]) { new TestWindowEvent(); ...

Frame with ActionEvent

import java.awt.*; import java.awt.event.*; public class TestEventFrame extends Frame implements ActionListener { Button bt, clearbt,addbt; TextField tf1, tf2, tf3; Label lno1, lno2; public TestEventFrame() { //creating components bt = new Button("Close"); clearbt = new Button("Clear"); addbt = new Button("Add"); tf1 = new TextField(3); tf2 = new TextField(3); tf3 = new TextField(3); tf3.setEditable(false); lno1 = new Label("No1 :"); lno2 = new Label("No2 :"); //Adding Components add(lno1); add(tf1); add(lno2); add(tf2); add(tf3); add(bt); add(addbt); add(clearbt); //registering listener bt.addActionListener(this); addbt.addActionListener(this); clearbt.addActionListener(this); //setting properties of Frame setSize(400,500); setVisible(true); setTitle("First Frame"); setLayout(new FlowLayout()); } public void actionPerforme...

Simple Swing Frame with closing functionality

import javax.swing.*; public class SwingWindowEvent extends JFrame { public SwingWindowEvent() { setSize(400,500); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String args[]) { new SwingWindowEvent(); } }

Simple GUI AWT Frame

import java.awt.*; class MyFrame extends Frame { private Button myb; MyFrame() { myb = new Button("OK"); setLayout(new FlowLayout()); add(myb); setSize(200,300); setTitle("First Frame"); setVisible(true); } } public class MyFirstFrame  { public static void main(String args[]) { // MyFrame f = new MyFrame(); new MyFrame(); } }

Multiple Exception Example

import java.util.Scanner; public class MultipleTestException { /* exception example  */  public static void main(String args[])  {   int no1=0, no2=0 ;  try  {  no1 = Integer.parseInt(args[0]);  no2 = Integer.parseInt(args[1]);  System.out.println("After 2nd argument" );  }  catch (ArrayIndexOutOfBoundsException ae)  {   System.out.println(ae);  }  catch (NumberFormatException ne) {   System.out.println(ne);  }  finally  {     System.out.println("Resources are released");  }   System.out.println("sum  : " + (no1 + no2) );   try  {   System.out.println("Division  : " + (no1 / no2) );   }   catch (ArithmeticException ae)   {    System.out.println(ae.getMessage());    //System.out.println("Divide by Zero");   }  System.out.println("Difference  : " + (no1 - no2) ); ...

Exception Example

import java.util.Scanner; public class TestException { /* exception example  */  public static void main(String args[])  {  Scanner sin = new Scanner(System.in);  System.out.println("Enter no1 :");   int no1 = sin.nextInt();   System.out.println("Enter no2 :");   int no2 = sin.nextInt();   System.out.println("sum  : " + (no1 + no2) );   try  {   System.out.println("Division  : " + (no1 / no2) );   }   catch (ArithmeticException ae)   {    System.out.println("Divide by Zero");   }  System.out.println("Difference  : " + (no1 - no2) );  System.out.println("Multiplication  : " + (no1 * no2) );  } }