import java.awt.*;
import java.io.*;
public class EchoClass
{
public static void main(String[] args)
{
for (int i=0; i<args.length; i++)
System.out.println(args[i] + " ");
}
}
java EchoClass first second third
EchoClass.main({"first", "second", "third"});
import java.awt.*;
import java.io.*;
// Nice to have just the initialization in the main class
// This allows you to reuse YourMainWindow more easily in
// other situations; that is, YourMainWindow does not have to be
// the main program to be reused.
public class BasicApplication {
public static void main(String[] args)
{
YourMainWindow w = new YourMainWindow();
w.show();
}
}
class YourMainWindow
extends Frame
{
public YourMainWindow()
{
super("YourMainWindow Title");
// NEED to specify a layout manager!!
setLayout(new FlowLayout());
resize(200,200);
// add what you want to this Frame
add(new Label("An Application Template..."));
}
}
import java.awt.*;
import java.io.*;
public class BasicEventApplication
{
public static void main(String[] args)
{
YourMainWindow w = new YourMainWindow();
w.show();
}
}
class YourMainWindow
extends Frame
{
TextField a,b;
Button but;
public YourMainWindow()
{
super("YourMainWindow Title");
setLayout(new FlowLayout());
resize(400,200);
// for example, we add a few text fields and a button
add(new Label("Click the mouse..."));
a = new TextField("0", 4);
b = new TextField("0", 4);
but = new Button("Reset");
but.addMouseListener(new MyMouseListener());
add(a);
add(b);
add(but);
}
class MyMouseListener
extends MouseListener
{
mb = new MenuBar(); // Step 1
m = new Menu("File"); // Step 2
m.add(new MenuItem("Open")); // Step 3
m.add(new MenuItem("-")); // add a separator
m.add(new CheckboxMenuItem("Allow writing"));
sub = Menu("Options..."); // Create a submenu
sub.add(new MenuItem("Option 1"));
m.add(sub); // add submenu to File menu
mb.add(m); // add File menu to menu bar
setMenuBar(mb); // set menu bar of your Frame subclass
// override postEvent. e.arg is the selected menu item (a String)
public boolean postEvent(Event e)
{
String item = (String) e.arg;
if ( item.equals("selection 1") ) { ... return true; }
else if ( item.equals("selection 2") ) { ... return true; }
else return super.postEvent(e); // if not handled, use normal behavior
}
import java.awt.*;
import java.io.*;
public class MenuTest
{
public static void main(String[] args)
{
MainWindow w = new MainWindow();
w.show();
}
}
// Make a main window with two top-level menus: File and Help.
// Help has a submenu and demonstrates a few interesting menu items.
class MainWindow
extends Frame
{
public MainWindow()
{
super("MenuTest Window");
setLayout(new FlowLayout());
resize(200,200);
// make a top level File menu
FileMenu fileMenu = new FileMenu(this);
// make a top level Help menu
HelpMenu helpMenu = new HelpMenu(this);
// make a menu bar for this frame and add top level
// menus File and Menu
MenuBar mb = new MenuBar();
mb.add(fileMenu);
mb.add(helpMenu);
setMenuBar(mb);
}
public void exit()
{
hide(); // hide the Frame
dispose(); // tell windowing system to free resources
System.exit(0); // exit
}
}
// Encapsulate the look and behavior of the File menu
class FileMenu
extends Menu
{
MainWindow mw; // who owns us?
public FileMenu(MainWindow m)
{
super("File");
mw = m;
add(new MenuItem("Open"));
add(new MenuItem("Close"));
add(new MenuItem("Exit"));
}
// respond to the Exit menu choice
// This method does not pass unhandled events to the parent
public boolean postEvent(Event e)
{
String item = (String) e.arg;
if ( item.equals("Exit") ) mw.exit();
else System.out.println("Selected FileMenu " + item);
return true;
}
}
// Encapsulate the look and behavior of the Help menu
class HelpMenu
extends Menu
{
MainWindow mw;
public HelpMenu(MainWindow m)
{
super("Help");
mw = m;
add(new MenuItem("Fundamentals"));
add(new MenuItem("Advanced"));
add(new MenuItem("-"));
add(new CheckboxMenuItem("Have Read"));
add(new CheckboxMenuItem("Have Not Read"));
// make a Misc sub menu of Help menu
Menu subMenu = new Menu("Misc");
subMenu.add(new MenuItem("Help!!!"));
subMenu.add(new MenuItem("Why did that happen?"));
add(subMenu);
}
// respond to a few menu items
// This method does not pass unhandled events to the parent
public boolean postEvent(Event e)
{
String item = (String) e.arg;
if ( item.equals("Fundamentals") )
System.out.println("Fundamentals");
else if ( item.equals("Help!!!") )
System.out.println("Help!!!");
// etc...
return true;
}
}
import java.awt.*;
import java.io.*;
public class DialogTest {
public static void main(String[] args)
{
AMainWindow mw = new AMainWindow();
mw.show();
}
}
class AMainWindow
extends Frame
{
Dialog d;
public AMainWindow()
{
super("YourMainWindow Title");
setLayout(new FlowLayout());
resize(200,200);
add(new Label("Dialog test...hit a key"));
// make a dialog box with a label inside and a button
d = new Dialog(this, "Error Dialog", true);
d.setLayout(new GridLayout(2,1));
d.resize(200,200);
d.add(new Label("something bad happened!!!!!"));
d.add(new Button("Ok"));
addKeyListener(new MyKeyHandler());
}
class MyKeyHandler
extends KeyAdaptor
{
public void keyPressed(KeyEvent e)
{
d.show();
}
}
}