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?

4

4 回答 4

2

您没有在类中定义主要功能。main 函数是运行文件时将调用的函数。

尝试添加

public static void main(String [] args)
{

}

并在 main 方法中创建并显示 JFrame 的对象。

于 2013-06-21T10:44:13.607 回答
2

右键单击您的项目 -> 属性 -> Java 构建路径 -> 源 -> 添加文件夹

现在选择 src 文件夹并单击 OK

于 2016-05-27T06:27:48.510 回答
1

您应该使用以下签名在您的类(任何一个)中定义一个主要方法:

public static void main(String args[])

该方法是程序的起点。

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
  .....
}
于 2013-06-21T10:44:25.663 回答
1

Java 程序的入口点是 main 方法。您的课程是否包含如下主要方法?

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

如果你没有这个,你的程序将无法运行。

于 2013-06-21T10:45:25.407 回答