Creating the Robot s Window


Creating the Robot's Window

So how do you start creating the Robot? This project uses Java Swing to present the window you see in Figure 7.1. The main class, RobotProject (called that so as not to conflict with the Java Robot class), extends the Swing JFrame class and implements the ActionListener class to be able to handle events such as button clicks:

 class RobotProject extends JFrame implements ActionListener {     .     .     . } 

You can find the significant methods of the JFrame class in Table 7.1.

Table 7.1. Significant Methods of the javax.swing.JFrame Class

Method

Does This

protected JRootPane createRootPane()

Called internally in order to create the default rootPane object

protected void frameInit()

Called internally to initialize the JFrame object properly

Container getContentPane()

Returns the contentPane object used in this frame

int getDefaultCloseOperation()

Returns the operation that happens when the user closes this frame

JMenuBar getJMenuBar()

Returns the menu bar used in this frame, if any

JLayeredPane getLayeredPane()

Returns the Swing LayeredPane object used in this frame

JRootPane getRootPane()

Returns the rootPane object used in this frame

static boolean isDefaultLookAndFeelDecorated()

Returns a value of true if new JFrame objects should match the current Swing look and feel

protected boolean isRootPaneCheckingEnabled()

Returns TRue if calls to add and setLayout are handled by the contentPane object

void remove(Component comp)

Removes the component from this container

void setContentPane(Container contentPane)

Sets the contentPane object that will be used

void setDefaultCloseOperation(int operation)

Sets the operation that will occur by default when the user closes this frame

static void setDefaultLookAndFeelDecorated boolean defaultLookAndFeelDecorated)

Specifies whether or not JFrame objectsshould have their borders, buttons, titles, and so on match the current look and feel

void setGlassPane(Component glassPane)

Sets the glassPane component

void setIconImage(Image image)

Sets the image that should be displayed as the icon for this JFrame object

void setJMenuBar(JMenuBar menubar)

Sets the menu bar for this JFrame object

void setLayeredPane(JLayeredPane layeredPane)

Sets the LayeredPane component

void setLayout(LayoutManager manager)

Sets the LayoutManager for the JFrame object

protected void setRootPane(JRootPane root)

Sets the rootPane property

void update(Graphics g)

Calls the paint(g) method


When the Robot first starts, the main method creates a new RobotProject object that calls the RobotProject constructor. That constructor starts by creating a new Swing JButton object with the caption "Go" and making the current object the button's action listener:

 class RobotProject extends JFrame implements ActionListener {     JButton jButton = new JButton("Go");     public static void main(String args[])     {         new RobotProject();     }     RobotProject()     {         jButton.addActionListener(this);         .         .         .      } } 

You can see the significant methods of the JButton class in Table 7.2.

Table 7.2. Significant Methods of the javax.swing.JButton Class

Method

Does This

boolean isDefaultButton()

Returns true if this button is the current default button

void setDefaultCapable (boolean defaultCapable)

Specifies whether this button can be made the default button for its Swing pane

void updateUI()

Resets the UI property to match the current look and feel


How do you add this new button to the Robot's window? In a Swing application, you typically add controls such as buttons to the content pane, which you can get access to using the JFrame class's getContentPane method, which returns a standard Java Container object. To add a control to the content pane, making it visible, you can use the Container class's add method.

Here's how the RobotProject constructor creates the text area of the Swing JTextArea class that will accept typed commands, makes it editable, sets the font used, and adds the text area and the button to the application's content pane:

 class RobotProject extends JFrame implements ActionListener {     JButton jButton = new JButton("Go");     JTextArea jTextArea = new JTextArea("");     public static void main(String args[])     {         new RobotProject();     }     RobotProject()     {         jButton.addActionListener(this);         getContentPane().setLayout(null);         jTextArea.setEditable(true);         jTextArea.setFont(new Font("Times Roman", Font.BOLD, 10));         getContentPane().add(jButton);         getContentPane().add(jTextArea);          .         .         .     } } 

You can see the significant methods of the JTextArea class in Table 7.3.

Table 7.3. Significant Methods of the javax.swing.JTextArea Class

Method

Does This

void append(String str)

Appends the specified text to the end of the text in the text area

int getColumns()

Returns the number of columns currently in the text area control

protected int getColumnWidth()

Returns the column width of the text area control

int getLineCount()

Returns the number of lines contained in the text area control

int getLineEndOffset(int line)

Returns the offset of the end of the specified line

int getLineOfOffset(int offset)

Returns the line of an offset in the text

int getLineStartOffset(int line)

Returns the offset of the start of the specified line

boolean getLineWrap()

Returns whether or not the text area wraps lines

Dimension getPreferredSize()

Returns the preferred size of the text area control

protected int getRowHeight()

Returns the height of a row in the text area control

int getRows()

Returns the number of rows in the text area control

int getTabSize()

Returns the number of characters used to expand tabs

boolean getWrapStyleWord()

Returns the style of wrapping that will be used (if the text area control wraps lines)

void insert(String str, int pos)

Inserts text at the given position

void replaceRange(String str, int start, int end)

Replaces text in the text area control, starting from the start position and ending at the end position, using the given new text

void setColumns(int columns)

Sets the number of columns for this text area control

void setFont(Font f)

Sets the current font used in this text area control

void setLineWrap(boolean wrap)

Sets whether or not the text area control will use line wrapping

void setRows(int rows)

Sets the number of rows in this text area control

void setTabSize(int size)

Sets the number of characters used to expand tabs

void setWrapStyleWord(boolean word)

Sets the style of wrapping lines that will be used (if the text area control is wrapping lines)


The Robot also uses a set of labels to display the static text you see in Figure 7.1, such as the label that holds the title text "The Robot." That label is created with the Swing JLabel class:

 class RobotProject extends JFrame implements ActionListener {     JButton jButton = new JButton("Go");     JTextArea jTextArea = new JTextArea("");     JTextArea helpInfo = new JTextArea("");     JLabel jLabel = new JLabel("The Robot");     public static void main(String args[])     {         new RobotProject();     }     RobotProject()     {         jButton.addActionListener(this);         getContentPane().setLayout(null);         jTextArea.setEditable(true);         jTextArea.setFont(new Font("Times Roman", Font.BOLD, 10));         getContentPane().add(jButton);         getContentPane().add(jTextArea);         getContentPane().add(jLabel);         .         .         .     } } 

You can see the significant methods of the Swing JLabel class in Table 7.4.

Table 7.4. Significant Methods of the javax.swing.JLabel Class

Method

Does This

Icon getDisabledIcon()

Returns the icon that is used by the label when the label is disabled

int getDisplayedMnemonic()

Returns the keycode that specifies a mnemonic key

int getHorizontalAlignment()

Returns the alignment of the label's text horizontally

int getHorizontalTextPosition()

Returns the horizontal location of the label's text

Icon getIcon()

Returns the graphic icon that the label displays, if any

int getIconTextGap()

Returns the amount of space between the text and the icon that this label displays

String getText()

Returns the text that the label will display

LabelUI getUI()

Returns the look-and-feel object that displays this label

int getVerticalAlignment()

Returns the alignment of the label's text vertically

int getVerticalTextPosition()

Returns the vertical position of the label's text

void setDisabledIcon(Icon disabledIcon)

Sets the icon that should be displayed if this JLabel object is disabled

void setDisplayedMnemonic(char aChar)

Sets the displayed mnemonic as a char value

void setDisplayedMnemonic(int key)

Sets a keycode that specifies a mnemonic key

void setHorizontalAlignment(int alignment)

Sets the alignment of the label's text horizontally

void setHorizontalTextPosition(int textPosition)

Sets the horizontal position of the label's text

void setIcon(Icon icon)

Sets the icon this label will display when active

void setIconTextGap(int iconTextGap)

Sets the space between the label's text and icon

void setLabelFor(Component c)

Specifies the component this label labels

void setText(String text)

Specifies the text this label will display

void setUI(LabelUI ui)

Sets the look-and-feel object that will draw this label

void setVerticalAlignment(int alignment)

Sets the alignment of the label's text vertically

void setVerticalTextPosition(int textPosition)

Sets the vertical position of the label's text


Besides labels, the Robot uses a JTextField control to read the name of a command file, if the user enters one. Here's what that looks like in the code:

 class RobotProject extends JFrame implements ActionListener {     JButton jButton = new JButton("Go");     JTextArea jTextArea = new JTextArea("");     JTextArea helpInfo = new JTextArea("");     JTextField jFileName = new JTextField("");     JLabel jLabel = new JLabel("The Robot");     public static void main(String args[])     {         new RobotProject();     }     RobotProject()     {         jButton.addActionListener(this);         getContentPane().setLayout(null);         jTextArea.setEditable(true);         jTextArea.setFont(new Font("Times Roman", Font.BOLD, 10));          getContentPane().add(jButton);         getContentPane().add(jTextArea);         getContentPane().add(jLabel);         getContentPane().add(jFileName);         .         .         .     } } 

You can see the significant methods of the JTextField class in Table 7.5.

Table 7.5. Significant Methods of the javax.swing.JTextField Class

Method

Does This

void addActionListener(ActionListener l)

Adds an action listener that will read action events from this text field control

protected void fireActionPerformed()

Notifies all listeners registered for this event of an event

Action getAction()

Returns the action for this text field control, or null if no action is set

ActionListener[] getActionListeners()

Returns an array of all the ActionListener objects that you added to this text field control

Action[] getActions()

Returns the list of actions for the text field control

int getColumns()

Returns the number of columns in this text field control

protected int getColumnWidth()

Returns the column width used in this text field control

int getHorizontalAlignment()

Returns the horizontal alignment of the text in the text field control

BoundedRangeModel getHorizontalVisibility()

Returns the horizontal visibility of the text field control

Dimension getPreferredSize()

Returns the preferred size for this text field control

int getScrollOffset()

Returns the scroll offset of the text field control (in pixels)

void removeActionListener(ActionListener l)

Removes a given action listener so that it will not receive action events from this text field control

void scrollRectToVisible(Rectangle r)

Scrolls the text field control

void setAction(Action a)

Sets the action for the text field control

void setActionCommand(String command)

Specifies the command string used for action events in this text field control

void setColumns(int columns)

Specifies the number of columns in this text field control

void setFont(Font f)

Sets the current font used in this text field control

void setHorizontalAlignment(int alignment)

Sets the horizontal alignment of the text in the text field control

void setScrollOffset(int scrollOffset)

Sets the scroll offset in this text field control (in pixels)


The constructor also adds the other controls you see in Figure 7.1 and then sizes all the controls appropriately, using their setBounds methods, and adds text that should be displayed, such as the help text:

 class RobotProject extends JFrame implements ActionListener {     JButton jButton = new JButton("Go");     JTextArea jTextArea = new JTextArea("");     JTextArea helpInfo = new JTextArea("");     JTextField jFileName = new JTextField("");     JLabel jLabel = new JLabel("The Robot");     JLabel prompt = new JLabel("Commands:");     JLabel usage = new JLabel("Usage:");     JLabel jFileNameLabel = new JLabel("Command file:");     public static void main(String args[])     {         new RobotProject();     }     RobotProject()     {         jButton.addActionListener(this);         getContentPane().setLayout(null);         jTextArea.setEditable(true);         jTextArea.setFont(new Font("Times Roman", Font.BOLD, 10));         getContentPane().add(jButton);         getContentPane().add(jTextArea);         getContentPane().add(jLabel);         getContentPane().add(jFileName);         getContentPane().add(prompt);         getContentPane().add(helpInfo);         getContentPane().add(usage);         getContentPane().add(jFileNameLabel);         jLabel.setBounds(30, 0, 120, 60);         jButton.setBounds(100, 450, 80, 40);         jLabel.setFont(new Font("Times Roman", Font.BOLD, 24));         prompt.setBounds(10, 50, 80, 20);         usage.setBounds(100, 50, 80, 20);         jTextArea.setBounds(10, 70, 80, 420);         jFileName.setBounds(100, 425, 80, 20);         jFileNameLabel.setBounds(95, 405, 90, 20);         helpInfo.setBounds(100, 70, 80, 335);         helpInfo.setEditable(false);         helpInfo.setText("Type text:\n    t:abc\n    t:ALTDN"             +  "\n    t:ALTUP"             +  "\n    t:CTRLDN"             +  "\n    t:CTRLUP"             +  "\n    t:TAB"             + "\n    t:ENTER"             + "\n    t:ESCAPE"             + "\nMove mouse:\n    m:x,y\n"             + "Left Click:\n    c:\n"             + "Right Click:\n    r:\n"             + "Wait n sec's:\n    w:n\n"             + "Cap screen:\n    s:\n"             + "Beep:\n    b:");         .         .         .     } } 

All that's left in the constructor is to set the title for the main window, make that window visible, and handle window-closing events. The code handles that this way:

 class RobotProject extends JFrame implements ActionListener {     JButton jButton = new JButton("Go");     JTextArea jTextArea = new JTextArea("");     JTextArea helpInfo = new JTextArea("");     JTextField jFileName = new JTextField("");     JLabel jLabel = new JLabel("The Robot");     JLabel prompt = new JLabel("Commands:");     JLabel usage = new JLabel("Usage:");     JLabel jFileNameLabel = new JLabel("Command file:");     String commands[] = new String[1024];     int numberCommands;     public static void main(String args[])     {         new RobotProject();     }     RobotProject()     {         .         .         .         helpInfo.setBounds(100, 70, 80, 335);         helpInfo.setEditable(false);         helpInfo.setText("Type text:\n    t:abc\n    t:ALTDN"             +  "\n    t:ALTUP"             +  "\n    t:CTRLDN"             +  "\n    t:CTRLUP"             +  "\n    t:TAB"             + "\n    t:ENTER"             + "\n    t:ESCAPE"             + "\nMove mouse:\n    m:x,y\n"             + "Left Click:\n    c:\n"             + "Right Click:\n    r:\n"             + "Wait n sec's:\n    w:n\n"             + "Cap screen:\n    s:\n"             + "Beep:\n    b:");         setTitle("Robot");         setSize(200,520);         setVisible(true);         this.addWindowListener(             new WindowAdapter(){             public void windowClosing(WindowEvent e)                 {                     System.exit(0);                 }             }         );     } } 

At this point, then, the Robot's window has been set up, as shown in Figure 7.1. Now it's time to get the Robot working. Now what about actually executing commands when the user clicks the Go button?



    Java After Hours(c) 10 Projects You'll Never Do at Work
    Java After Hours: 10 Projects Youll Never Do at Work
    ISBN: 0672327473
    EAN: 2147483647
    Year: 2006
    Pages: 128

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net