1

I made a sample code to start one project just refactoring another one.

This the refactored one:

    package com.sh.st; 

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingConstants;



public class Main extends JFrame implements ActionListener {

    /**
     * 
     */
    JMenuBar bar;
    JMenu file, register;
    JMenuItem close, search;
    ImageIcon figure1= new ImageIcon("C:/Users/Victor/Downloads/Untitled.jpg");
    //ImageIcon figure2= new ImageIcon("C:/Victor Rocha/carroicon.jpg");
    JLabel Ibimagem1,Ibimagem2;

    /**
     * 
     */
    public Main()
    {

        bar= new JMenuBar();
        file= new JMenu("file");
        register= new JMenu("register");

        register.setMnemonic(KeyEvent.VK_R);
        file.setMnemonic(KeyEvent.VK_F);

        close= new JMenuItem("Close");
        close.addActionListener(this);

        search= new JMenuItem("Search");
        search.addActionListener(this);



        Ibimagem1= new JLabel(figure1, JLabel.CENTER);
        Ibimagem1.setVerticalTextPosition(SwingConstants.CENTER);

        bar.add(file);
        bar.add(register);
        file.add(close);
        register.add(search);
        //register.add(carro);
        //register.add(cliente);
        //register.add(funcionario);
        getContentPane().add(Ibimagem1);
        setSize(800,600);
        setTitle("SHST");
        setJMenuBar(bar);
        setDefaultCloseOperation(0);
        //setIconImage(figure2.getImage());

            WindowListener J=new WindowAdapter(){
            public void windowClosing(WindowEvent e){
            System.exit(0);
            }
        }; 

        addWindowListener(J);
}

    public void actionPerformed(ActionEvent e){
        if(e.getSource()==close){
            System.exit(0);
        }

        if(e.getSource()==search){
            Search s= new Search();
            s.setVisible(true);
        }

        }
}

This is the original one:

    package com.professordelphi.locadora;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingConstants;

public class Principal extends JFrame implements ActionListener {

    JMenuBar barra;
    JMenu arquivo, cadastro;
    JMenuItem fechar, cliente, funcionario, carro;
    ImageIcon figura1= new ImageIcon("C:/Victor Rocha/carro.jpg");
    ImageIcon figura2= new ImageIcon("C:/Victor Rocha/carroicon.jpg");
    JLabel Ibimagem1,Ibimagem2;

    public Principal()
    {

        barra= new JMenuBar();
        arquivo= new JMenu("Arquivo");
        cadastro= new JMenu("Cadastro");

        cadastro.setMnemonic(KeyEvent.VK_C);
        arquivo.setMnemonic(KeyEvent.VK_A);

        fechar= new JMenuItem("Fechar");
        fechar.addActionListener(this);
        carro= new JMenuItem("Carro");
        carro.addActionListener(this);
        cliente= new JMenuItem("Cliente");
        cliente.addActionListener(this);
        funcionario= new JMenuItem("Funcionario");
        funcionario.addActionListener(this);



        Ibimagem1= new JLabel(figura1, JLabel.CENTER);
        Ibimagem1.setVerticalTextPosition(SwingConstants.CENTER);

        barra.add(arquivo);
        barra.add(cadastro);
        arquivo.add(fechar);
        cadastro.add(carro);
        cadastro.add(cliente);
        cadastro.add(funcionario);
        getContentPane().add(Ibimagem1);
        setSize(800,600);
        setTitle("Sistema de Cadastro");
        setJMenuBar(barra);
        setDefaultCloseOperation(0);
        setIconImage(figura2.getImage());

                WindowListener J=new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        }; 

        addWindowListener(J);
}

    public void actionPerformed(ActionEvent e){
        if(e.getSource()==fechar){
            System.exit(0);
        }
        if(e.getSource()==carro){
            Carro k = new Carro();
            k.setVisible(true);
        }
        if(e.getSource()==cliente){
            Cliente c = new Cliente();
            c.setVisible(true);
        }
        if(e.getSource()==funcionario){
            Funcionario f= new Funcionario();
            f.setVisible(true);
        }

        }
    }

The thing is, the original e building and the refactored is not. The error I receive from the refactored is that "Selection does not contain a main type". I saw a lot of posts regarding this theme but none of them solve my problem. Here is one little list of the things I've tried;

Source: Editor does not contain a main type

  1. clean your workspace and rebuild your Project.
  2. make sure you add your source folder in the project properties -> java build path -> source.
  3. close your project and reopen it.

Trying to run as a Java Application with Eclipse, anyone has suggestions of what should I do?

Community
  • 1
  • 1
Victor Oliveira
  • 3,293
  • 7
  • 47
  • 77
  • possible duplicate of [Error: Selection does not contain a main type](http://stackoverflow.com/questions/16225177/error-selection-does-not-contain-a-main-type) – jww Sep 30 '14 at 05:48

4 Answers4

2

You dont have a main function defined in the class. The main function is the function that will be called when you run the file.

Try adding

public static void main(String [] args)
{

}

and create and show an object of your JFrame in the main method.

Akash
  • 631
  • 1
  • 8
  • 16
2

Right click on your project ->Properties -> Java Build Path -> Source -> Add Folder

Now Select the src folder and click on OK

sapan prajapati
  • 1,514
  • 19
  • 16
1

You should define a main method in your class (either one) with the following signature:

public static void main(String args[])

This method is the start point of program.

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingConstants;
public class Main extends JFrame implements ActionListener {

  public static void main(String args[]){
   // from here the program execution starts
  }
  ....
  your other code
  .....
}
Abubakkar
  • 15,488
  • 8
  • 55
  • 83
1

The entry point for Java programs is the main method. Does your class contain main method like below ?

public static void main(String[] args) {
    //Code
}

If you do not have this, your program will not run.

Rohan
  • 3,068
  • 1
  • 20
  • 26