Set Selected Radio Button Example
- /*
- Set Selected Radio Button Example
- This java example shows how to set a radio button selected using
- Java AWT CheckboxGroup class.
- */
- import java.applet.Applet;
- import java.awt.Checkbox;
- import java.awt.CheckboxGroup;
- /*
- <applet code="SetSelectedRadioButtonExample" width=200 height=200>
- </applet>
- */
- public class SetSelectedRadioButtonExample extends Applet{
- CheckboxGroup lngGrp = null;
- public void init(){
- //create group
- lngGrp = new CheckboxGroup();
- //create checkboxes and add to group
- Checkbox java = new Checkbox("Java", lngGrp, false);
- Checkbox cpp = new Checkbox("C++", lngGrp, false);
- Checkbox vb = new Checkbox("VB", lngGrp, false);
- //add radio buttons
- add(java);
- add(cpp);
- add(vb);
- /*
- * To set a radio button selected by default, use
- * void setSelectedCheckbox(Checkbox chk)
- * method of Java AWT CheckboxGroup class.
- */
- lngGrp.setSelectedCheckbox(cpp);
- }
- }
Example Output
2.
Get Selected Item Of AWT Choice Or Combobox Example
- /*
- Get Selected Item Of AWT Choice Or Combobox Example
- This java example shows how to get selected item of a choice or combobox
- using Java AWT Choice class.
- */
- import java.applet.Applet;
- import java.awt.Choice;
- import java.awt.Graphics;
- import java.awt.event.ItemEvent;
- import java.awt.event.ItemListener;
- /*
- <applet code="GetSelectedItemExample" width=200 height=200>
- </applet>
- */
- public class GetSelectedItemExample extends Applet implements ItemListener{
- Choice language = null;
- public void init(){
- //create choice or combobox
- language = new Choice();
- //add items to the choice
- language.add("Java");
- language.add("C++");
- language.add("VB");
- language.add("Perl");
- //add choice or combobox
- add(language);
- //add item listener
- language.addItemListener(this);
- }
- public void paint(Graphics g){
- /*
- * To get selected item, use
- * String getSelectedItem()
- * method of AWT Choice class.
- */
- g.drawString(language.getSelectedItem(),10, 70);
- }
- public void itemStateChanged(ItemEvent arg0) {
- repaint();
- }
- }
Example Output
3.
Create Custom Color Using RGB Example
- /*
- Create Custom Color Using RGB Example
- This java example shows how to create a custom color using red, green
- and blue (RGB) components in an Applet window using Java AWT Color class.
- */
- import java.applet.Applet;
- import java.awt.Color;
- import java.awt.Graphics;
- /*
- <applet code="CreateCustomColor" width=200 height=100>
- </applet>
- */
- public class CreateCustomColor extends Applet{
- public void paint(Graphics g) {
- /*
- * To create a custom color using RGB, use
- * Color(int red,int green, int blue)
- * constructor of Color class.
- */
- //this will create light blue color
- Color customColor = new Color(10,10,255);
- //set foreground color of an applet
- this.setForeground(customColor);
- g.drawString("This is a custom RGB color", 10, 50);
- }
- }
Example Output
4.
Change AWT Button Font Example
- /*
- Change Button Font Example
- This java example shows how to change button's font using
- AWT Button class.
- */
- import java.applet.Applet;
- import java.awt.Button;
- import java.awt.Font;
- /*
- <applet code="ChangeButtonFontExample" width=200 height=200>
- </applet>
- */
- public class ChangeButtonFontExample extends Applet{
- public void init(){
- //create buttons
- Button button1 = new Button("Button 1");
- Button button2 = new Button("Button 2");
- /*
- * To change font of a button use
- * setFont(Font f) method.
- */
- Font myFont = new Font("Courier", Font.ITALIC,12);
- button1.setFont(myFont);
- //add buttons
- add(button1);
- add(button2);
- }
- }
Example Output
5.
Create Checked Checkbox Example
- /*
- Create Checked Checkbox Example
- This java example shows how to create a checked Checkbox using AWT
- Checkbox class.
- */
- import java.applet.Applet;
- import java.awt.Checkbox;
- /*
- <applet code="CreateCheckBox" width=200 height=200>
- </applet>
- */
- public class CreateCheckedCheckBox extends Applet{
- public void init(){
- //crete checkboxes
- Checkbox checkBox1 = new Checkbox("My Checkbox 1");
- /*
- * To create a checked checkbox, use
- * Checkbox(String label, boolean on)
- * Constructor.
- */
- Checkbox checkbox2 = new Checkbox("My Checkbox 2", true);
- //add checkboxes using add method
- add(checkBox1);
- add(checkbox2);
- }
- }
Example Output
6.
Hide Checkbox Example
- /*
- Hide Checkbox Example
- This java example shows how to hide a Checkbox using setVisible method.
- */
- import java.applet.Applet;
- import java.awt.Checkbox;
- /*
- <applet code="HideCheckboxExample" width=100 height=200>
- </applet>
- */
- public class HideCheckboxExample extends Applet{
- public void init(){
- //create Checkboxes
- Checkbox Checkbox1 = new Checkbox("Checkbox 1");
- Checkbox Checkbox2 = new Checkbox("Checkbox 2");
- //add Checkboxes
- add(Checkbox1);
- add(Checkbox2);
- /*
- * To hide a Checkbox, use
- * void setVisible(Boolean visible)
- * method.
- */
- Checkbox2.setVisible(false);
- }
- }
Example Output
7.
Get Available Font Family Names Example
- /*
- Get Available Font Family Names Example
- This java example shows how to get available font family names using
- getAvailableFontFamilyNames method of GraphicsEnvironment class.
- */
- import java.applet.Applet;
- import java.awt.Graphics;
- import java.awt.GraphicsEnvironment;
- /*
- <applet code="GetAvailableFonts" width=200 height=200>
- </applet>
- */
- public class GetAvailableFonts extends Applet{
- public void paint(Graphics g){
- /*
- * To get an object of GraphicsEnvironment, use
- * static GraphicsEnvironment getLocalGraphicsEnvironment()
- * method of GraphicsEnvironment class.
- *
- * This is a static method.
- */
- GraphicsEnvironment graphicsEnvironment =
- GraphicsEnvironment.getLocalGraphicsEnvironment();
- /*
- * To get available font family names use,
- * String[] getAvailableFontFamilyNames()
- * method of GraphicsEnvironment class.
- *
- * This method returns an array of strings
- * containing names of available font families.
- */
- String fontNames[] =graphicsEnvironment.getAvailableFontFamilyNames();
- int y = 20;
- for(int i=0; i < fontNames.length; i++){
- //print font names
- g.drawString(fontNames[i], 10, y);
- y += 20;
- }
- }
- }
Example Output
8.
Create Frame Window With Window Close Event Example
- /*
- Create Frame Window With Window Close Event Example
- This java example shows how to create frame window and handle windowClosing
- event using WindowAdapter class.
- */
- import java.awt.Frame;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- /*
- * To create a stand alone window, class should be extended from
- * Frame and not from Applet class.
- */
- public class CreateWindowWithEventsExample extends Frame{
- CreateWindowWithEventsExample(String title){
- //call the superclass constructor with the specified title
- super(title);
- //add window event adapter
- addWindowListener(new MyWindowAdapter(this));
- //set window size using setSize method
- this.setSize(300,300);
- //show window using setVisible method
- this.setVisible(true);
- }
- //extend WindowAdapter
- class MyWindowAdapter extends WindowAdapter{
- CreateWindowWithEventsExample myWindow = null;
- MyWindowAdapter(CreateWindowWithEventsExample myWindow){
- this.myWindow = myWindow;
- }
- //implement windowClosing method
- public void windowClosing(WindowEvent e) {
- //hide the window when window's close button is clicked
- myWindow.setVisible(false);
- }
- }
- public static void main(String[] args) {
- CreateWindowWithEventsExample myWindow =
- new CreateWindowWithEventsExample("Window with event Example");
- }
- }
Example Output
No comments:
Post a Comment