Wednesday, November 5, 2014

Java frame pro



Disable Button

import java.applet.Applet;
import java.awt.Button;
public class DisableButtonExample extends Applet
{
public void init()
{
Button Button1 = new Button("Ok");
Button Button2 = new Button("Cancel");           
add(Button1);
add(Button2);
Button2.setEnabled(false);
  }
}

Handle Action Events for AWT Button

import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class HandleActionEventExample extends Applet implements ActionListener
{
String actionMessage="";   
public void init()
{
Button Button1 = new Button("Ok");
Button Button2 = new Button("Cancel");       
add(Button1);
add(Button2);
Button1.addActionListener(this);
Button2.addActionListener(this);
}
     
public void paint(Graphics g)
{
g.drawString(actionMessage,10,50);
}
public void actionPerformed(ActionEvent ae)
{
String action = ae.getActionCommand();       
if(action.equals("Ok"))
actionMessage = "Ok Button Pressed";
else if(action.equals("Cancel"))
actionMessage = "Cancel Button Pressed";             
repaint();
    }
}

Hide Button

import java.applet.Applet;
import java.awt.Button;
public class HideButtonExample extends Applet
public void init()
{
Button Button1 = new Button("Ok");
Button Button2 = new Button("Cancel");
add(Button1);
add(Button2);
Button2.setVisible(false);
   }
}

Remove AWT Button From Applet or Window Frame

import java.applet.Applet;
import java.awt.Button;
public class RemoveButtonExample extends Applet
{
public void init()
{
Button Button1 = new Button("Ok");
Button Button2 = new Button("Cancel");
add(Button1);
add(Button2);
remove(Button1);
  }
}

Set Action Command For AWT Button

import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ButtonSetActionCommandExample extends Applet implements ActionListener
{  
String actionMessage="";         
public void init()
{
Button Button1 = new Button("I agree with the terms and conditions"); add(Button1);    Button1.addActionListener(this);
Button1.setActionCommand("Agree");
}
public void paint(Graphics g)
{
g.drawString(actionMessage,10,50);
}         
public void actionPerformed(ActionEvent ae)
{
String action = ae.getActionCommand();
actionMessage = action + " button pressed!";
repaint();
    }
}

Change Checkbox Background Color

import java.applet.Applet;
import java.awt.Checkbox;
import java.awt.Color;
public class ChangeCheckboxBackgroundExample extends Applet
{
public void init()
{
Checkbox checkbox1 = new Checkbox("Checkbox 1");
Checkbox checkbox2 = new Checkbox("Checkbox 2");             
checkbox1.setBackground(Color.red);
checkbox2.setBackground(Color.green);
add(checkbox1);
add(checkbox2);
   }
}

No comments:

Post a Comment