import java.io.*;
import javax.swing.*;
// CIS161 Lab 1/29/01
import java.awt.*;
import java.awt.event.*;
/**
* Applet with a main() that allows
* it to be an application also.
*
*/
public class Sample1 extends JApplet{
public void init() {
JButton b = new JButton("Click Me");
final JTextField t = new JTextField("Hello");
Container c = getContentPane();
c.setLayout( new FlowLayout() );
c.add(b);
c.add(t);
b.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
doButtonAction(t);
}
});
}
private void doButtonAction(JTextField txt) {
txt.setText("hello world");
}
/**
*
* Adding the correct code allows an Applet
* to be used as an application. It must have
* a main method.
*/
public static void main(String[] args) {
// Create a window for the applet from a JFrame
final JFrame window = new JFrame("Applet Any One?");
window.setSize(300,200);
// This is the way to exit, it's a window listener
window.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
window.dispose();
System.exit(0);
}
});
// Get the content pane of our JFrame
Container c = window.getContentPane();
// Create an instance of the JApplet
JApplet app = new Sample1();
// Call the init method of our applet
app.init();
// Add the applet to our JFrame
c.add(app);
// Finally show the JFrame
window.show();
}
/*
public static void main(String[] args) {
System.out.println("Hello world");
int value = getInt("Enter a number: ");
System.out.println("Number is: " + value);
try {
int ch = Integer.parseInt("123");
}
catch(NumberFormatException e) {
System.out.println("bad entry for number");
}
JOptionPane.showMessageDialog(null, "Press OK to continue");
System.exit(0);
}
*/
public static int getInt(String prompt) {
int number = 0;
boolean badInput = true;
do {
badInput = true;
try {
number = Integer.parseInt(
JOptionPane.showInputDialog(null, prompt));
badInput = false;
}
catch(NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Bad input dope");
}
} while (badInput);
return number;
}
}