Download presentation
Presentation is loading. Please wait.
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.http://www.eclipse.org/ 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_utilities 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 myframe=new 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 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); }
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 { @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. I’ve moved the declarations “up” to the Applications “global” area. See code in slide notes later.
13
Running our application
14
Eclipse project…application’s constructor… entire app in slide notes public Application(){ super(); System.out.println("start constructor");//trace line… could be handled by logging JFrame 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 }
15
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.
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.jar;C:\jarfiles\jndi.jar;C:\jarfiles\persistence.jar;C:\ejbjarfile s\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
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/myap plet.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
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(){//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); }
21
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
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[]){//application’s main method.. Os will run this JFrame 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 this MyPanel p=new MyPanel(); add(p);} static class MyPanel extends JPanel {//put all the functionality in here JTextField 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); }}}
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:\somepath> set classpath=. C:\somepath> java classname.class
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.