Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson 36: The calculator – Java Applets. 1. Creating Your First Applet HelloWorldApp is an example of a Java application, a standalone program. Now you.

Similar presentations


Presentation on theme: "Lesson 36: The calculator – Java Applets. 1. Creating Your First Applet HelloWorldApp is an example of a Java application, a standalone program. Now you."— Presentation transcript:

1 Lesson 36: The calculator – Java Applets

2 1. Creating Your First Applet HelloWorldApp is an example of a Java application, a standalone program. Now you will create a Java applet called HelloWorld, which also displays the greeting "Hello world!". Unlike HelloWorldApp, however, the applet runs in a Java-enabled Web browser such as HotJava, Netscape Navigator, or Microsoft Internet Explorer. import java.applet.*; import java.awt.*; /** * The HelloWorld class implements an applet that * simply displays "Hello World!". */ public class HelloWorld extends Applet { public void paint(Graphics g) { // Display "Hello World!" g.drawString("Hello world!", 50, 25); } Review

3 Compile the applet source File. Compiling the applet java code At the prompt, type the following command and press Return: javac HelloWorld.java The compiler should generate a Java bytecode file, HelloWorld.class. Review

4 Using the applet viewer to see the applet Run the Program. Although you can view your applets using a Web browser, you may find it easier to test your applets using the simple appletviewer application that comes with the JavaTM Platform. To view the HelloWorld applet using appletviewer, enter at the prompt: Review

5 The applet HTML file Review

6 The Java Application calculator

7 // MiniCalc.java - demo gridlayout import java.awt.*; import javax.swing.*; class MiniCalc{ public static void main (String[] args){ JFrame frame = new JFrame("MiniCalc"); Container pane = frame.getContentPane(); //Creating the major components JTextField firstNumber= new JTextField(20); JTextField secondNumber= new JTextField(20); JTextField result= new JTextField(20); JButton addButton = new JButton("Add"); JButton subButton = new JButton("Subtract"); pane.setLayout(new GridLayout(4,2)); pane.add(new JLabel("Enter a number")); pane.add(firstNumber); pane.add(new JLabel("Enter a number")); pane.add(secondNumber); pane.add(new JLabel("Result")); pane.add(result); pane.add(addButton); pane.add(subButton); DoMath listener = new DoMath(firstNumber, secondNumber, result); subButton.addActionListener(listener); addButton.addActionListener(listener); frame.pack(); frame.show(); } Review

8 // DoMath.java import javax.swing.*; import java.awt.event.*; class DoMath implements ActionListener{ DoMath(JTextField first, JTextField second, JTextField result){ inputOne = first; inputTwo = second; output = result; } public void actionPerformed(ActionEvent e){ double first, second; first = Double.parseDouble(inputOne.getText().trim()); second = Double.parseDouble(inputTwo.getText().trim()); if (e.getActionCommand().equals("Add")) output.setText(String.valueOf(first+second)); else output.setText(String.valueOf(first-second)); } private JTextField inputOne, inputTwo, output; } Review

9 Running the application Review

10 The Java Applet calculator

11 In this example we will rewrite the web calculator so that we can place it on the web. The benefit is that this application can run anywhere on any operating system that is Java enabeled.

12 The // MiniCalcApplet.java - /* <applet code=MiniCalcApplet.class" width=200 height =100>*/ import java.awt.*; import javax.swing.*; public class MiniCalcApplet extends JApplet{ public void init (){ Container pane = getContentPane(); JTextField firstNumber= new JTextField(20); JTextField secondNumber= new JTextField(20); JTextField result= new JTextField(20); JButton addButton = new JButton("Add"); JButton subButton = new JButton("Subtract"); // There will be 4 rows of two components each pane.setLayout(new GridLayout(4,2)); pane.add(new JLabel("Enter a number")); pane.add(firstNumber); pane.add(new JLabel("Enter a number")); pane.add(secondNumber); pane.add(new JLabel("Result")); pane.add(result); pane.add(addButton); pane.add(subButton); DoMath listener = new DoMath(firstNumber, secondNumber, result); subButton.addActionListener(listener); addButton.addActionListener(listener); } // MiniCalc.java - demo gridlayout import java.awt.*; import javax.swing.*; class MiniCalc{ public static void main (String[] args){ JFrame frame = new JFrame("MiniCalc"); Container pane = frame.getContentPane(); JTextField firstNumber= new JTextField(20); JTextField secondNumber= new JTextField(20); JTextField result= new JTextField(20); JButton addButton = new JButton("Add"); JButton subButton = new JButton("Subtract"); pane.setLayout(new GridLayout(4,2)); pane.add(new JLabel("Enter a number")); pane.add(firstNumber); pane.add(new JLabel("Enter a number")); pane.add(secondNumber); pane.add(new JLabel("Result")); pane.add(result); pane.add(addButton); pane.add(subButton); DoMath listener = new DoMath(firstNumber, secondNumber, result); subButton.addActionListener(listener); addButton.addActionListener(listener); frame.pack(); frame.show(); } We don’t need frames in an applet

13 // DoMath.java import javax.swing.*; import java.awt.event.*; class DoMath implements ActionListener{ DoMath(JTextField first, JTextField second, JTextField result){ inputOne = first; inputTwo = second; output = result; } public void actionPerformed(ActionEvent e){ double first, second; first = Double.parseDouble(inputOne.getText().trim()); second = Double.parseDouble(inputTwo.getText().trim()); if (e.getActionCommand().equals("Add")) output.setText(String.valueOf(first+second)); else output.setText(String.valueOf(first-second)); } private JTextField inputOne, inputTwo, output; } This Listener class remains unchanged

14 A Calculator using a Java Applet and the DoMath class Here is Berg’s very own web calculator The HMTL page to embedd the JavaApplet

15 The filesCompiling Running in a browser

16 Running in the applet viewer Sometimes it is easier to debug an applet using the applet viewer (we don’t see the HTML web page) The web page

17 Observations: 1.The applet use another host to display information, so we do not need the Jframe component. 2.The container is automatically associated with the viewer since we extended our class and therefore based it on the Japplet. 3.The pack() and the show() methods are not needed. 4.The layout manager still works on the container. 5.The listener classes are still the same and do not need to be recompiled. 6.An applet can be embedded in any webpage and used as applications on the page. The applet will run on any operating system that is java enabeled.


Download ppt "Lesson 36: The calculator – Java Applets. 1. Creating Your First Applet HelloWorldApp is an example of a Java application, a standalone program. Now you."

Similar presentations


Ads by Google