Lab Source 1-17-01
AWT example, no swing
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/**
* Write a description of the applet class Lab_1_17 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Lab_1_17v2 extends Applet
{
Container cPane;
Button b1 = new Button("Blue");
Button b2 = new Button("Red");
//ComboBox jcb1;
TextField tf1 = new TextField("This is a text field");
TextArea ta1 = new TextArea(5, 30);
boolean blueClick = false;
boolean redClick = false;
static Lab_1_17v2 app;
public static void main(String [] args) {
app = new Lab_1_17v2();
app.init();
Frame win = new Frame("My Window");
win.add(app);
win.addWindowListener( new WindowEventHandler() );
win.setSize(300,400);
win.show();
}
/**
* Called by the browser or applet viewer to inform this Applet that it
* has been loaded into the system. It is always called before the first
* time that the start method is called.
*/
public void init()
{
app.setLayout( new FlowLayout() );
app.add(b1);
app.add(b2);
b1.addActionListener( new B1_ButtonListener() );
b2.addActionListener( new B2_ButtonListener() );
//jcb1 = new JComboBox( new MyComboBoxModel() );
//jcb1.setMaximumRowCount(3);
//cPane.add(jcb1);
app.add(tf1);
app.add(ta1);
}
/**
* Called by the browser or applet viewer to inform this Applet that it
* should start its execution. It is called after the init method and
* each time the Applet is revisited in a Web page.
*/
public void start()
{
// provide any code requred to run each time
// web page is visited
}
/**
* This may be the most important method in your applet: Here, the
* drawing of the applet gets done. "paint" gets called everytime the
* applet should be drawn on the screen. So put the code here that
* shows the applet.
*
* @param g the Graphics object for this applet
*/
public void paint(Graphics g)
{
// simple text displayed on applet
/*if (blueClick)
g.setColor(Color.blue);
else if (redClick)
g.setColor(Color.red);
else
g.setColor(Color.yellow);
g.fillRect(0, 0, 200, 100);
g.setColor(Color.black);
g.drawString("Sample Applet", 20, 20);
if (blueClick)
g.setColor(Color.yellow);
else if (redClick)
g.setColor(Color.white);
else
g.setColor(Color.blue);
g.drawString("created by BlueJ", 20, 40);
*/
}
/**
* Called by the browser or applet viewer to inform this Applet that
* it should stop its execution. It is called when the Web page that
* contains this Applet has been replaced by another page, and also
* just before the Applet is to be destroyed. If you do not have any
* resources that you need to release (such as threads that you may
* want to stop) you can remove this method.
*/
public void stop()
{
// provide any code that needs to be run when page
// is replaced by another page or before Applet is destroyed
}
/**
* Called by the browser or applet viewer to inform this Applet that it
* is being reclaimed and that it should destroy any resources that it
* has allocated. The stop method will always be called before destroy.
* If you do not have any resources that you need to release you can
* remove this method.
*/
public void destroy()
{
// provide code to be run when Applet is about to be destroyed.
}
/**
* Returns information about this applet.
* An applet should override this method to return a String containing
* information about the author, version, and copyright of the Applet.
*
* @return a String representation of information about this Applet
*/
public String getAppletInfo()
{
// replace this with your own info
return "Title: BlueJ Applet Demo\n" +
"Author: Bruce Quig\n" +
"A simple applet that draws two strings.";
}
/**
* Returns information about the parameters that are understood by this
* Applet. You should return an array of Strings here to provide details
* about each of the parameters separately.
* Each element of the array should be a set of three Strings containing
* the name, the type, and a description.
*
* @return a String[][] representation of parameter information about
* this Applet
*/
public String[][] getParameterInfo()
{
// provide parameter information about the applet
String paramInfo[][] = {
{"firstParameter", "1-10", "description of first parameter"},
{"secondParameter", "boolean", "description of second parameter"}
};
return paramInfo;
}
//
// NESTED CLASSES BELOW
//
class B1_ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
blueClick = ! blueClick;
ta1.append("Blue");
repaint();
}
}
class B2_ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
redClick = ! redClick;
ta1.append( tf1.getText() );
repaint();
}
}
}
class WindowEventHandler extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
Swing Example with Problems??
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
* Write a description of the applet class Lab_1_17 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Lab_1_17 extends JApplet
{
Container cPane;
JButton b1 = new JButton("Blue");
JButton b2 = new JButton("Red");
JComboBox jcb1;
JTextField tf1 = new JTextField("This is a text field");
JTextArea ta1 = new JTextArea(5, 30);
boolean blueClick = false;
boolean redClick = false;
public static void main(String [] args) {
Lab_1_17 app = new Lab_1_17();
app.init();
JFrame win = new JFrame("My Window");
Container winPane = win.getContentPane();
winPane.add(app);
win.addWindowListener( new WindowEventHandler() );
win.setSize(300,400);
win.show();
}
/**
* Called by the browser or applet viewer to inform this Applet that it
* has been loaded into the system. It is always called before the first
* time that the start method is called.
*/
public void init()
{
cPane = getContentPane();
cPane.setLayout( new FlowLayout() );
cPane.add(b1);
cPane.add(b2);
b1.addActionListener( new B1_ButtonListener() );
b2.addActionListener( new B2_ButtonListener() );
jcb1 = new JComboBox( new MyComboBoxModel() );
jcb1.setMaximumRowCount(3);
cPane.add(jcb1);
cPane.add(tf1);
cPane.add(ta1);
}
/**
* Called by the browser or applet viewer to inform this Applet that it
* should start its execution. It is called after the init method and
* each time the Applet is revisited in a Web page.
*/
public void start()
{
// provide any code requred to run each time
// web page is visited
}
/**
* This may be the most important method in your applet: Here, the
* drawing of the applet gets done. "paint" gets called everytime the
* applet should be drawn on the screen. So put the code here that
* shows the applet.
*
* @param g the Graphics object for this applet
*/
public void paint(Graphics g)
{
// simple text displayed on applet
/*if (blueClick)
g.setColor(Color.blue);
else if (redClick)
g.setColor(Color.red);
else
g.setColor(Color.yellow);
g.fillRect(0, 0, 200, 100);
g.setColor(Color.black);
g.drawString("Sample Applet", 20, 20);
if (blueClick)
g.setColor(Color.yellow);
else if (redClick)
g.setColor(Color.white);
else
g.setColor(Color.blue);
g.drawString("created by BlueJ", 20, 40);
*/
}
/**
* Called by the browser or applet viewer to inform this Applet that
* it should stop its execution. It is called when the Web page that
* contains this Applet has been replaced by another page, and also
* just before the Applet is to be destroyed. If you do not have any
* resources that you need to release (such as threads that you may
* want to stop) you can remove this method.
*/
public void stop()
{
// provide any code that needs to be run when page
// is replaced by another page or before Applet is destroyed
}
/**
* Called by the browser or applet viewer to inform this Applet that it
* is being reclaimed and that it should destroy any resources that it
* has allocated. The stop method will always be called before destroy.
* If you do not have any resources that you need to release you can
* remove this method.
*/
public void destroy()
{
// provide code to be run when Applet is about to be destroyed.
}
/**
* Returns information about this applet.
* An applet should override this method to return a String containing
* information about the author, version, and copyright of the Applet.
*
* @return a String representation of information about this Applet
*/
public String getAppletInfo()
{
// replace this with your own info
return "Title: BlueJ Applet Demo\n" +
"Author: Bruce Quig\n" +
"A simple applet that draws two strings.";
}
/**
* Returns information about the parameters that are understood by this
* Applet. You should return an array of Strings here to provide details
* about each of the parameters separately.
* Each element of the array should be a set of three Strings containing
* the name, the type, and a description.
*
* @return a String[][] representation of parameter information about
* this Applet
*/
public String[][] getParameterInfo()
{
// provide parameter information about the applet
String paramInfo[][] = {
{"firstParameter", "1-10", "description of first parameter"},
{"secondParameter", "boolean", "description of second parameter"}
};
return paramInfo;
}
//
// NESTED CLASSES BELOW
//
class B1_ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
blueClick = ! blueClick;
repaint();
}
}
class B2_ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
redClick = ! redClick;
repaint();
}
}
class MyComboBoxModel extends AbstractListModel
implements ComboBoxModel {
String [] jcbItems = {"Red", "Green", "Blue", "White", "Black"};
String selection = null;
public Object getElementAt(int index) {
return jcbItems[index];
}
public int getSize() {
return jcbItems.length;
}
public void setSelectedItem(Object anItem) {
selection = (String) anItem;
}
public Object getSelectedItem() {
return selection;
}
}
}
class WindowEventHandler extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}