Java Reference
In-Depth Information
The source fi le
ColorChanger.java
is saved in
c:\myproject\src\com\sybex\events
and the class is compiled using the following command executed from
c:\myproject\src
:
javac -d c:\myproject\build .\com\sybex\events\ColorChanger.java
This command line creates
ColorChanger.class
in the
c:\myproject\build\com\sybex\
events
directory. The following program contains
main
and tests the
ColorChanger
class:
1. package com.sybex.demos;
2.
3. import com.sybex.events.ColorChanger;
4. import java.awt.Button;
5. import java.awt.Color;
6. import java.awt.event.ActionEvent;
7.
8. public class TestColors {
9.
10. public static void main(String [] args) {
11. Button b = new Button(“Testing...”);
12. b.setBackground(Color.GREEN);
13. System.out.println(“Color is “ + b.getBackground());
14.
15. ColorChanger cc = new ColorChanger(b);
16. ActionEvent action = new ActionEvent(b,
17. ActionEvent.ACTION_PERFORMED,
18. “blue”);
19. cc.actionPerformed(action);
20. System.out.println(“Now the color is “
21. + b.getBackground());
22. }
23. }
TestColors.java
is saved in the
c:\myproject\src\com\sybex\demos
directory.
Because
TestColors
is not in the same package as
ColorChanger
, it imports the
ColorChanger
class.
TestColors
is compiled using the following command executed from
the
c:\myproject\src
directory:
javac -d c:\myproject\build .\com\sybex\demos\TestColors.java
This command line creates
TestColors.class
in the directory
c:\myproject\build\com\
sybex\demos
. Figure 1.3 shows the directory structure after compiling the source fi les with
-d
.
Search WWH ::
Custom Search