Top Banner
Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse
23

Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse.

Using Textpad, &Eclipse & a little Java, too

Creating simple Java applications and applets with Textpad or

Eclipse

Page 2: Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse.

About the programs

• Eclipse is open source, Textpad is shareware. Eclipse can be downloaded from http://www.eclipse.org/. Textpad is available from Helios.com.

• Instead, you can use any editor, but save java files as text files with the .java extension.

• Eclipse and Textpad should be available in the campus labs.

Page 3: Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse.

Eclipse

• If you download, be sure to download a java version (not C++) of Eclipse.

• When you start up, select a workspace folder.

• Eclipse does come with its own tutorials and exercises.

Page 4: Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse.

Create a project

• Select: – New– Project– java project– give it a name – finish

• Depending on your eclipse version, it may not look precisely like my screenshots

Page 5: Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse.

New project

Page 6: Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse.

Aside: Java projects

• Java projects are comprised of packages, classes and interfaces.

• When building a project, you can name packages or use the “default package”.

• Packages are like namespaces. They help avoid naming collisions and indicate a required directory structure. The package mystuff.utilities.file_utilities is in the directory proj_name\mystuff\utilities\file_utilities

• Interfaces contain a set of method signatures. Classes implementing an interface must define these methods.

Page 7: Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse.

Eclipse screenshot: a java class in the default package, some minimal content,

selecting Run/run as application

Page 8: Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse.

Application to open a frame (window) on the screen

import javax.swing.*;//JFrame definition is in herepublic class Application {public static void main(String args[]){System.out.println("hello");doStuff();//a method call}public static void doStuff(){//method definitionJFrame myframe=new JFrame();myframe.setBounds(10,10,300,400);myframe.setVisible(true);//a frame with nothing on it}}

Page 9: Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse.

Revised function doStuff which opens a window with various widgets on it

public static void doStuff(){JFrame myframe=new JFrame();myframe.setBounds(10,10,300,400);JPanel mypanel=new JPanel();JTextField a,b;JLabel label=new JLabel("answer will

appear here");JButton button=new JButton("press me");a=new JTextField(20);b=new JTextField(20);mypanel.add(a);mypanel.add(b);mypanel.add(button);mypanel.add(label);myframe.add(mypanel,BorderLayout.CEN

TER);myframe.setVisible(true);}

Page 10: Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse.

Adding functionality

• The “button” has to listen for a click on it and then we should probably get the input values and do something.

• There are many ways to add this functionality to our application. One way, is to let the outer class implement the ActionListener interface, so that it will be responsible for indicating what should happen when a button click occurs. This responsibility could fall to any class which implements the ActionListener interface.

• See next slide for this second possibility.

Page 11: Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse.

A minimal solution

class MyListener implements ActionListener {

@Override

public void actionPerformed(ActionEvent e) {

//put code here later

}

}

Page 12: Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse.

Next…

• Add the ActionListener to the button. Then, when the button is pressed (in actionPerformed method), get input values, do something with them and display the result.

• I’ve moved the declarations “up” to the Applications “global” area. See code in slide notes later.

Page 13: Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse.

Running our application

Page 14: Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse.

Eclipse project…application’s constructor… entire app in slide notes

public Application(){super();System.out.println("start constructor");//trace line… could be handled by loggingJFrame myframe=new JFrame();myframe.setBounds(10,10,300,400);mypanel=new JPanel();MyListener mylistener=new MyListener();label=new JLabel("answer will appear here");JButton button=new JButton("press me");a=new JTextField(20);b=new JTextField(20);mypanel.add(a);mypanel.add(b);mypanel.add(button);mypanel.add(label);button.addActionListener(mylistener);myframe.add(mypanel,BorderLayout.CENTER);myframe.setVisible(true);System.out.println("end constructor"); //trace line… could be handled by logging}

Page 15: Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse.

Textpad…same code

• You will need to slightly configure your own version of Textpad, adding javac command to tools if you wish to compile from within Textpad

• Run the .class file from the command line.

• Looks the same.

Page 16: Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse.

Aside: Command line on blackscreen DOS

• You can run projects from within Eclipse. If you add tools and set preferences in Textpad, you can run projects in Textpad, too.

• To run in DOS:• Path settings must contain paths to executables. Classpaths must

contain paths to classes to be executed. These can be set – depending on your O.S. - in control panel/environment variables.

• To set a classpath you might type something like– set CLASSPATH= .;C:\jarfiles\ejb.jar;C:\jarfiles\jndi.jar;C:\jarfiles\

persistence.jar;C:\ejbjarfiles\j2ee.jar;C:\myproject\src;• Assuming classpaths are properly set you compile a class named

myclass.java with the command:– C:\myproject\src>javac myclass.java

• Assuming classpaths are properly set you run a class named myclass.class with the command: – C:\myproject\src>java myclass

Page 17: Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse.

About applets

• An applet is an internet program. You would “access” it, by entering a url to an html file that “points” to the class.

• In java, an applet is defined as a special sort of a panel.

• To run an applet, you must post both the class file (result of javac command) and the special html file, on your w drive.

• To access the applet, you enter something likehttp://students.oneonta.edu/LastFM99/subdir/myapplet.html

Page 18: Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse.

More about applications and applets

• Applications usually need a window, a JFrame in swing terminology. An applet uses the browser window, so it does not require a special window to be opened.

• One way to create code which can run either way, is to put all the functionality into a JPanel. In the init method, add an instance of this JPanel to the applet. Supply a main method too, in case it is run as an application. Here, construct a JFrame and add the panel to the JFrame.

Page 19: Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse.

Simple HTML for an applet

<html>

<applet code="MyApplet.class" width=200 height=300>

</applet>

</html>

Page 20: Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse.

A minimal applet with some widgets on it

import java.awt.*;import java.awt.event.*;import javax.swing.*;//JFrame in herepublic class AppletEX extends JApplet{public void init(){//applet’s init method browser will run this

Container c=getContentPane();JTextField a,b;JLabel label;c.setLayout(new FlowLayout());label=new JLabel("answer will appear here");JButton button=new JButton("press me");a=new JTextField(20);b=new JTextField(20);c.add(a);c.add(b);c.add(button);c.add(label);}}

Page 21: Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse.

Running an applet locally

• Blackscreen commands

c:\Documents and Settings\higgindm\My Documents>set classpath=.

c:\Documents and Settings\higgindm\My Documents>appletviewer AppletEX.html

• AppletEX.HTML file contents

<html>

<applet code="AppletEX.class" width=200 height=300>

</applet>

</html>

Page 22: Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse.

A program which can run as either an applet or application

import java.awt.*;import java.awt.event.*;import javax.swing.*;//JFrame in herepublic class MyApplet extends JApplet{

public static void main(String args[]){//application’s main method.. Os will run thisJFrame myframe=new JFrame();MyPanel p=new MyPanel();myframe.add(p,BorderLayout.CENTER);myframe.setVisible(true);myframe.setBounds(100,100,500,500);}

public void init(){//applet’s init method… browser will run thisMyPanel p=new MyPanel();add(p); }

static class MyPanel extends JPanel {//put all the functionality in hereJTextField a,b;

JLabel label;public MyPanel(){

label=new JLabel("answer will appear here");JButton button=new JButton("press me");a=new JTextField(20);b=new JTextField(20);add(a);add(b);add(button);add(label);}}}

Page 23: Using Textpad, &Eclipse & a little Java, too Creating simple Java applications and applets with Textpad or Eclipse.

Running it

• You need to first compile this and be able to access the .class file generated. I used textpad for this example.

• To view the applet not on the network, but on your desktop, java comes with a program named appletviewer.exe. You need to write the html file to access the applet, even if you are using appletviewer, (unless you are using a lab machine, in which case you can execute the applet locally from textpad).

• In comand window, set classpath (example below) then type – appletviewer whatever.html

• To run application, typeC:\somepath> set classpath=. C:\somepath> java classname.class