To create a button just do this:
Button b = new Button("Some Label") add(b);i.e (within a Container)
Panel p = new Panel() p.setLayout(new FlowLayout()); p.add(new Button("one")); p.add(new Button("two")); p.add(new Button("three")); add(p);To handle button events there are two main ways _without_ sub-classing the button:
// The following code handles the button event using the label, // can however compare objects(i.e if(evt.target instanceof Button) // instead of comparing labels as we are doing below public boolean handleEvent(Event evt) { switch(evt.id) { case Event.ACTION_EVENT: { // Note: Java here is converting "Some Label" to // to a String and comparing it to evt.arg which // is the label of the button just pressed. if("Some Label".equals(evt.arg)) { System.out.println("This button has been hit"); // Call a method of yours call myMethod() myMethod(); return true; } } default: return false; } // Or public boolean action(Event evt, Object arg) { if("Some Label".equals(arg)) { System.out.prinln("This button has been hit"); myMethod(); return true; } return false; }
To get a String array of the fonts supported on your current platform:(Submitted by Cliff Berg)String[] fonts= Toolkit.getDefaultToolkit().getFontList();
Note that there appears to be a bug that surfaces if one tries to instantiate too many Font objects.The basic way to "open" a font is:
Font newFont = new Font(fontName, Font.BOLD, fontSize);where fontName is one of the strings returned by getFontList(). To use this font,g.setFont(iconFont);
/*
java applet code written by
M.K. Kwong, MCS, Argonne National Laboratory
kwong@mcs.anl.gov
Date: Jan 1996
Permission to use, copy, modify, and distribute this
software and its documentation for NON-COMMERCIAL or
COMMERCIAL purposes and without fee is hereby granted.
*/
import java.lang.*;
import java.applet.Applet;
import java.awt.*;
public class RedLab extends java.applet.Applet {
public void init() {
setBackground(Color.white);
Panel p = new Panel();
p.setLayout(new FlowLayout());
Label lab = new Label("red",Label.CENTER);
lab.setBackground(Color.red);
p.add(lab);
add(p);
}
}
Note from the editor: This seems to not work on Windows95 at this time (JDK1.0,
Netscape 2.0).
It does, however, work on Solaris.