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:
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) {
}
}
Re: Can't Start welcomeScreen.java
When you run a class what the runtime invokes is a method like:
Code :
public static void main(String[] args) {
Quote:
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.
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(); )
Re: Can't Start welcomeScreen.java
Quote:
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.