Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Can't Start welcomeScreen.java

  1. #1
    Junior Member
    Join Date
    May 2012
    Location
    Missouri
    Posts
    12
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Can't Start welcomeScreen.java

    Im using eclipse and I can't start this class, It won't let me. I have to keep the code without a static part, because the code welcomeHelp.addActionListener(this); has to work in a non-static zone. But I can't start the class without it being a non-static zone. Please help.

    The Code:
    package fire_programming.chat_client;
     
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class welcomeScreen implements ActionListener {
     
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
     
    	public void main(String[]args)throws Exception{
     
    		//Creates the JFrame and sets it's properties.
    		JFrame welcomeFrame = new JFrame();
    		welcomeFrame.setSize(500, 500);
    		welcomeFrame.setVisible(true);
    		welcomeFrame.setTitle("Chat Client");
     
    		//Creates the JPanels and sets their properties.
     
    		JPanel welcomePanel1 = new JPanel(new BorderLayout());
    		welcomePanel1.setVisible(true);
     
    		//Creates the JButtons and sets their properties.
    		JButton welcomeHelp = new JButton();
    		welcomeHelp.setVisible(true);
    		welcomeHelp.setText("Help Menu");
    		welcomePanel1.add(welcomeHelp);
    		welcomeHelp.addActionListener(this);
     
     
    	}
     
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
     
     
    	}
     
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Can't Start welcomeScreen.java

    When you run a class what the runtime invokes is a method like:

    public static void main(String[] args) {

    I have to keep the code without a static part, because...
    You can't leave out the "static" for the reason described above.

    Luckily you don't have to. Instead you create an instance of the welcomeScreen class (WelcomeScreen would be a better name) and use that instance instead of "this" as the argument to addActionListener().

    Having said that you should try and keep the main() method as brief as you can. It's really not the place for your program logic which go in constructors (if it involves setting things up so they are properly functional) or methods.

  3. #3
    Junior Member
    Join Date
    May 2012
    Location
    Missouri
    Posts
    12
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't Start welcomeScreen.java

    Fixed it, changed the methods name to the name of the class ( public welcomeScreen(){ ) And used another class to start it using ( new welcomeScreen(); )

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Can't Start welcomeScreen.java

    changed the methods name to the name of the class
    Just to get the terminology straight you changed the static main() method into a class's constructor. (same name as class, doesn't return anything). As noted before a constructor is the right place for things to be set up.

    Notice you don't really need a another class from which to call the constructor. Although it is common.

    And you really should rename the class to conform with Java coding standards: WelcomeScreen.

Similar Threads

  1. java web start still not working
    By gib65 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 15th, 2011, 10:25 AM
  2. Java Web Start app exiting with access denie (java.lang.RuntimePermission
    By sonaljain in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 13th, 2011, 08:43 PM
  3. Regarding java Web Start Technology
    By sujas in forum Java Theory & Questions
    Replies: 1
    Last Post: May 4th, 2011, 07:44 AM
  4. Start With JAVA
    By infoprovider in forum The Cafe
    Replies: 2
    Last Post: July 28th, 2010, 04:34 AM
  5. How should i write and compile java with Ubuntu?
    By Talk Binary in forum Java IDEs
    Replies: 19
    Last Post: May 7th, 2009, 05:29 AM