Using Textpad, - PowerPoint PPT Presentation

About This Presentation
Title:

Using Textpad,

Description:

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 ... – PowerPoint PPT presentation

Number of Views:214
Avg rating:3.0/5.0
Slides: 24
Provided by: higg8
Category:
Tags: eclipse | textpad | using

less

Transcript and Presenter's Notes

Title: Using Textpad,


1
Using Textpad, Eclipse a little Java, too
  • Creating simple Java applications and applets
    with Textpad or Eclipse

2
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.

3
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.

4
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

5
New project
6
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_utiliti
    es
  • Interfaces contain a set of method signatures.
    Classes implementing an interface must define
    these methods.

7
Eclipse screenshot a java class in the default
package, some minimal content, selecting Run/run
as application
8
Application to open a frame (window) on the screen
  • import javax.swing.//JFrame definition is in
    here
  • public class Application
  • public static void main(String args)
  • System.out.println("hello")
  • doStuff()//a method call
  • public static void doStuff()//method definition
  • JFrame myframenew JFrame()
  • myframe.setBounds(10,10,300,400)
  • myframe.setVisible(true)//a frame with nothing
    on it

9
Revised function doStuff which opens a window
with various widgets on it
  • public static void doStuff()
  • JFrame myframenew JFrame()
  • myframe.setBounds(10,10,300,400)
  • JPanel mypanelnew JPanel()
  • JTextField a,b
  • JLabel labelnew JLabel("answer will appear
    here")
  • JButton buttonnew JButton("press me")
  • anew JTextField(20)
  • bnew JTextField(20)
  • mypanel.add(a)
  • mypanel.add(b)
  • mypanel.add(button)
  • mypanel.add(label)
  • myframe.add(mypanel,BorderLayout.CENTER)
  • myframe.setVisible(true)

10
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.

11
A minimal solution
  • class MyListener implements ActionListener
  • _at_Override
  • public void actionPerformed(ActionEvent e)
  • //put code here later

12
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.
  • Ive moved the declarations up to the
    Applications global area. See code in slide
    notes later.

13
Running our application
14
Eclipse projectapplications constructor entire
app in slide notes
  • public Application()
  • super()
  • System.out.println("start constructor")//trace
    line could be handled by logging
  • JFrame myframenew JFrame()
  • myframe.setBounds(10,10,300,400)
  • mypanelnew JPanel()
  • MyListener mylistenernew MyListener()
  • labelnew JLabel("answer will appear here")
  • JButton buttonnew JButton("press me")
  • anew JTextField(20)
  • bnew 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

15
Textpadsame 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.

16
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.jarC\jarfiles\j
    ndi.jarC\jarfiles\persistence.jarC\ejbjarfiles
    \j2ee.jarC\myproject\src
  • Assuming classpaths are properly set you compile
    a class named myclass.java with the command
  • C\myproject\srcgtjavac myclass.java
  • Assuming classpaths are properly set you run a
    class named myclass.class with the command
  • C\myproject\srcgtjava myclass

17
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 like
  • http//students.oneonta.edu/LastFM99/subdir/myappl
    et.html

18
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.

19
Simple HTML for an applet
  • lthtmlgt
  • ltapplet code"MyApplet.class" width200
    height300gt
  • lt/appletgt
  • lt/htmlgt

20
A minimal applet with some widgets on it
  • import java.awt.
  • import java.awt.event.
  • import javax.swing.//JFrame in here
  • public class AppletEX extends JApplet
  • public void init()//applets init method browser
    will run this
  • Container cgetContentPane()
  • JTextField a,b
  • JLabel label
  • c.setLayout(new FlowLayout())
  • labelnew JLabel("answer will appear here")
  • JButton buttonnew JButton("press me")
  • anew JTextField(20)
  • bnew JTextField(20)
  • c.add(a)
  • c.add(b)
  • c.add(button)
  • c.add(label)

21
Running an applet locally
  • Blackscreen commands
  • c\Documents and Settings\higgindm\My
    Documentsgtset classpath.
  • c\Documents and Settings\higgindm\My
    Documentsgtappletviewer AppletEX.html
  • AppletEX.HTML file contents
  • lthtmlgt
  • ltapplet code"AppletEX.class" width200
    height300gt
  • lt/appletgt
  • lt/htmlgt

22
A program which can run as either an applet or
application
  • import java.awt.
  • import java.awt.event.
  • import javax.swing.//JFrame in here
  • public class MyApplet extends JApplet
  • public static void main(String args)//applicati
    ons main method.. Os will run this
  • JFrame myframenew JFrame()
  • MyPanel pnew MyPanel()
  • myframe.add(p,BorderLayout.CENTER)
  • myframe.setVisible(true)
  • myframe.setBounds(100,100,500,500)
  • public void init()//applets init method
    browser will run this
  • MyPanel pnew MyPanel()
  • add(p)
  • static class MyPanel extends JPanel //put all
    the functionality in here
  • JTextField a,b

23
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, type
  • C\somepathgt set classpath.
  • C\somepathgt java classname.class
Write a Comment
User Comments (0)
About PowerShow.com