Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,303 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 783 people online right now. Registration is fast and FREE... Join Now!




Java GUI compilation deconstructed

 
Reply to this topicStart new topic

Java GUI compilation deconstructed

bitterlynew2java
4 Dec, 2008 - 09:03 AM
Post #1

New D.I.C Head
*

Joined: 12 Nov, 2008
Posts: 7

I have broken down my GUI app into just the shell to see where I am going wrong. I am following the outline in my book and have used the set-up as written in my book but I can't get the first part of the code to compile. What am I doing wrong? When I try to compile, I get the following error: TSAssn5a.java:11: TSAssn5a is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener
public class TSAssn5a extends JFrame implements ActionListener{
^
CODE
/*
TS NU Grades GUI Interface Assignment 5
*/

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

//#1 Implement event proceesing
public class TSAssn5a extends JFrame implements ActionListener{
  private JTextArea frameTextArea;           //create a text area
  private Container frameContainer;          //for the content pane
  private JScrollPane scrollForTextArea;     //scroll bar
  private BorderLayout frameLayout;          //default
  final int width = 640, height = 480;       //Standard size

//#2 New attributes for data processing
  // and status bar
  private JLabel statusBar;
  //private int addedScore;

  private JMenuBar bar;
  private JMenu fileMenu, editMenu, helpMenu;
  private JMenuItem exitApp, clearText, displayText, addText,
                    aboutThisApp;
  // Constructor
  public TSAssn5a(String title){
     //Call to JFrame constructor
     super(title);
     //Call initialization
     init();
    }

  public void init(){
     //Use the WindowCloser Class
     addWindowListener(new WindowCloser(this));
     //Set up the Window for the operating system
     setLookAndFeel();

     //Get the content pane
     frameContainer = getContentPane();
     //Create an instance ofa JTextArea
     frameTextArea = new JTextArea("",
                                   18, 70);
     frameTextArea.setEditable(true);
     frameTextArea.setLineWrap(true);
     //Set the text area in the scroll pane
     scrollForTextArea = new JScrollPane(frameTextArea);

     //Create an instance of a border layout
     frameLayout = new BorderLayout();
     frameContainer.setLayout(frameLayout);

//#3 Add the status bar (JLabel)
     frameContainer.add(scrollForTextArea, BorderLayout.CENTER);
     //create a label to use as a status bar
     statusBar = new JLabel(" ");
     frameContainer.add(statusBar, BorderLayout.SOUTH);

     setSize(height, width);
     makeMenu();
//#4 Postition the window in the center of the desktop
     positionWindow();
     pack();
     setVisible(true);
  }

//----------------------------setLookAndFeel()----------------------
  //Set the look and feel for the application
  public void setLookAndFeel(){
     try{
        String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
        UIManager.setLookAndFeel(lookAndFeel);
     }
     catch(Exception lFE){
       //process exception
     }
  }

//3.1
//-----------------------setStatus()---------------------------
private void setStatus(String msg){
    statusBar.setText("  " + msg);
}
//----------------------------positionWindow-----------------------------
//#4.1 Position the Frame
private void positionWindow(){
   Dimension sizeOfScreen = Toolkit.getDefaultToolkit().getScreenSize();
   Dimension sizeOfFrame = this.getSize();
   if(sizeOfFrame.height > sizeOfScreen.height){
      sizeOfFrame.height = sizeOfScreen.height;
   }
   if(sizeOfFrame.width > sizeOfScreen.width){
      sizeOfFrame.width = sizeOfScreen.width;
   }
   this.setLocation( (sizeOfScreen.width - sizeOfFrame.width) /2 ,
                (sizeOfScreen.height - sizeOfFrame.height) /2 );
}

//#5----------------------------makeMenu()-----------------------------
  private void makeMenu(){
       //Create a menu bar
     bar = new JMenuBar();

     //create File menu
     fileMenu = new JMenu("File");
     fileMenu.setMnemonic('F');
        //With menu items
        
     //generate events and add handlers
        exitApp = new JMenuItem("Exit");
        exitApp.setMnemonic('X');
        exitApp.addActionListener(this);

        //Add the File items to File menu
        fileMenu.add(exitApp);

     //create Edit menu
     editMenu = new JMenu("Edit");
     editMenu.setMnemonic('E');

       //With menu items
       clearText = new JMenuItem("Clear Text");
       clearText.setMnemonic('R');
       displayText = new JMenuItem("Display Text");
       displayText.setMnemonic('D');
       addText = new JMenuItem("Add Text");
       addText.setMnemonic('A');

       //Add the Edit items to Edit menu
       editMenu.add(clearText);
       clearText.addActionListener(this);
       editMenu.add(displayText);
       displayText.addActionListener(this);
       editMenu.add(addText);
       addText.addActionListener(this);

     //create Help menu
     helpMenu = new JMenu("Help");
     helpMenu.setMnemonic('H');
        //With menu items
        aboutThisApp = new JMenuItem("About...");
        aboutThisApp.setMnemonic('A');
        aboutThisApp.addActionListener(this);
        //Add the Help items to Help menu
        helpMenu.add(aboutThisApp);

    
     //Add File, Edit, and Help to bar
     bar.add(fileMenu);
     bar.add(editMenu);
     bar.add(helpMenu);

     //Add the menu bar to the content pane
     setJMenuBar(bar);
  }//end makeMenu
//#3.1-----------------------main()---------------------------------
  public static void main(String args[]){
     new TSAssn5a("TSNU Grades");
  }//end main
}//end class


User is offlineProfile CardPM
+Quote Post


Martyr2
RE: Java GUI Compilation Deconstructed
4 Dec, 2008 - 09:34 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 6,656



Thanked: 613 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
You say that the class implements ActionListener. This means that the class is going to have a function called "actionPerformed()" as a method which will handle an action the user makes on one of its controls. Since you do not create this actionPerformed method in the class, it is calling you out on it.

If you look at your book again you should see code in there that shows an actionPerformed method. You can read up online about how to create this using the Java tutorial which explains the process.

Java Tutorial - How to write an ActionListener

Pay special attention where it mentions the actionPerformed method. This method should be in your class. This goes with any listener your class implements... they will require certain methods to be defined.

Hope that helps!

"At DIC we be actionListening code ninjas... I hear fighting, lets go!" decap.gif
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 05:59PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month