sapdev logo background
sapdev logo sapdev logo
Comments

AWT Java development




Below you will find the Java code for creating a simple AWT applications.

The Java code to create AWT container
//package awt;
import java.awt.*;

public class Container1 extends Container{

  public static void main(String[] args) {
    Container r_cont = new Frame("Title for Container");
    r_cont.setLayout(new GridLayout(4,1));
    r_cont.setBounds(60,60,400,300);
    r_cont.setVisible(true);
  }
}


The Java code to display a file selection dialog
//package awt;

import java.awt.*;

public class FileLoadDialog {

	public static void main(String[] args) {
	  FileDialog fd = new FileDialog(new Frame(),
                          "Find file path...", FileDialog.LOAD );
	  fd.show();
	  if (fd.getFile() != null)
	    System.out.println(fd.getDirectory() + fd.getFile());
	  else
	    System.out.println("Load cancelled........");
	}
}


The Java code for using buttons
//package awt;
import java.awt.*;
import java.awt.event.*;

// If you are not too bother about this having the close functionality
// replace ZFrameClose with Frame
public class ZButton extends ZFrameClose implements ActionListener{
	public ZButton(){
		super("This is the Frame showing buttons...");
		setLayout(new FlowLayout());
		for (int idx=0;idx<9;idx++){
			Button b = null;
			add(b = new Button("Button"+idx));
			b.addActionListener(this);
			b.setBackground(Color.RED);
			b.setForeground(Color.BLACK);
		}
	}
	public void actionPerformed(ActionEvent event){
		String cmd= event.getActionCommand();
		for (int idx = 0; idx < 99 ; idx++){
			String but = "Button"+idx;
			if (cmd.equals(but))
			   System.out.println("Button "+idx+
			   		        "  pressed.");
		}
	}
	public static void main(String[] args) {
		ZButton aDemo = new ZButton();
		aDemo.setVisible(true);
	}
}


The Java code to demonstrate use of checkbox
//package awt;
import java.awt.*;
import java.awt.event.*;

// If you are not too bother about this having the close functionality
// replace ZFrameClose with Frame
public class ZCheckboxEvent extends ZFrameClose implements ItemListener {
  	public ZCheckboxEvent() {
  	  super("This is the Frame Title for Checkbox....");
	  setLayout(new GridLayout(6, 1));
	  for (int idx = 0; idx < 6; idx++) {
	 	Checkbox cb = null;
		if ((idx % 2) == 0) {
		  add(cb = new Checkbox("Checkbox " + idx, true));
		} else {
		  add(cb = new Checkbox("Checkbox " + idx, false));
		}
		cb.addItemListener(this);
  	  }

	}
	public void itemStateChanged(ItemEvent event) {
  	  Checkbox cb = (Checkbox) event.getItemSelectable();
 	  System.out.println(
		"Label= " + cb.getLabel() + " State= " + cb.getState());
	}
	public static void main(String[] args) {
	  ZCheckboxEvent aDemo = new ZCheckboxEvent();
	  aDemo.setVisible(true);
	}
}

The Java code to demonstrate use of radio button(checkbox group)
//package awt;
import java.awt.*;
//import java.awt.event.*;

// If you are not too bother about this having the close functionality
// replace ZFrameClose with Frame
public class ZCheckboxGroup extends ZFrameClose{
    public ZCheckboxGroup(){
    	super("This is the Frame for CheckboxGroup...");

    	setLayout(new GridLayout(3,2));
    	CheckboxGroup cbg1 = new CheckboxGroup();
		CheckboxGroup cbg2 = new CheckboxGroup();

		add(new Checkbox("north", cbg1, true));
		add(new Checkbox("one", cbg2, false));
		add(new Checkbox("centre", cbg1, false));
		add(new Checkbox("two", cbg2, false));
		add(new Checkbox("south", cbg1, false));
		add(new Checkbox("three", cbg2, true));
    }
	public static void main(String[] args) {
		ZCheckboxGroup aDemo = new ZCheckboxGroup();
		aDemo.setVisible(true);
	}
}


The Java code to demonstrate use of dropdown list(Choice)
//package awt;

import java.awt.*;
import java.awt.event.*;

// If you are not too bother about this having the close functionality
// replace ZFrameClose with Frame
public class ZChoice extends ZFrameClose implements ItemListener {
	public ZChoice() {
		super("Choice frame....");

		Choice choice = new Choice();
		choice.addItemListener(this);

		choice.addItem("Halo");
		choice.addItem("Warcraft");
		choice.addItem("Streetfighter");
		choice.addItem("Quake");
		choice.addItem("Super Mario");
		choice.addItem("Pacman");
		choice.addItem("Space Invaders");

		add(choice);

	}
	public void itemStateChanged(ItemEvent event) {
		Choice choice = (Choice) event.getItemSelectable();
		System.out.println(choice.getSelectedItem());
	}
	public static void main(String[] args) {
		ZChoice aDemo = new ZChoice();
		aDemo.setVisible(true);
	}
}




comments powered by Disqus