Eclipse:Java,没有找到主要方法 [英] Eclipse: Java, no main method found
问题描述
java.lang.NoSuchMethodError:main
线程中的异常主要的
所以我想我不得不添加一个主要的方法(我从来没有这样做在BlueJ为什么?)所以我这样做只是调用构造方法(在BlueJ我将创建一个新的对象和JFrame将显示)。所以我做到了,同样的错误。尝试不同的东西(例如将构造函数中的代码移动到不同的方法等)。我只是把它放在主要的方法中:
public void main(String [] args)
{
System.out.println(你好,这是主要为什么Java不会找到这个。);
}
之后,我仍然有相同的错误,于是我决定添加它到我所有的类,以确保它不使用另一个类作为主类。还是一样的错误,所以我来找你想知道你们中有没有遇到这个问题。我还搜索Google,所有我发现是私人
类等问题,感觉我所有的类都是 public
(嘿我来自Python :))。我知道这不是问题。帮助请:)
我的运行配置的图片
警告:LONG
import java.awt.event。*;
import java.awt。*;
import javax.swing。*;
public class AppFrame extends JFrame
{
public String status =Status:; // Applet的状态
public int paint_count = 1; // applet已被绘制的次数
public int [] mousePos = {-1,-1}; //存储鼠标的最后点击的X和Y Cordinates
public int [] boardPos = {-1 ,-1}; //存储板的X和Y Cordinates
public int [] selectedSquarePos = {-1,-1};
public int [] boardSquare = {-1,-1}; //存储最后点击的Square
public Color P1_Color = Color.GRAY;
public Color P2_Color = Color.WHITE;
public Color SquareEven = Color.BLACK;
public Color SquareOdd = Color.RED; // piece move on this one
public int boardHeight = 400;
public int boardWidth = 400;
public boolean pieceSelected = false;
public boolean CheckersPiece = false;
public Board CheckersBoard = new Board();
public Image buffer = null;
public Graphics bg = null;
public void main(String [] args)
{
System.out.println(你好,这是主要为什么Java不会找到这个。
}
public AppFrame()
{
super(JCheckers);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(900,900);
setVisible(true);
buffer = createImage(getWidth(),getHeight());
boardHeight = getHeight() - 40; //顶部和底部为20像素边框,蓝色条纹为20像素
boardWidth = getWidth() - 40; //左边的20像素边框
bg = buffer.getGraphics();
addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
handleClick(e);
}
}
);
}
public void handleClick(MouseEvent e)
{
/ *处理鼠标点击跟踪;不要手动显示,它只是更新数据并调用redraw * /
mousePos [0] = e.getX();
mousePos [1] = e.getY();
repaint();
}
public void paint(Graphics g)
{
super.paint(g);
render(g);
}
public void render(Graphics g)
{
bg.clearRect(0,0,getWidth(),getHeight());
//绘制棋盘和棋子
renderChessBoard(bg,20,20);
//显示信息
System.out.println(status);
System.out.println(String.format(Last Mouse Click @(X:%d Y:%d),mousePos [0],mousePos [1]));
System.out.println(Paint#+ paint_count);
System.out.println(String.format(Board Square(x:%s,y:%s)%b,boardSquare [0],boardSquare [1],CheckersPiece));
System.out.println(CheckersBoard.status);
paint_count + = 1;
//将图像绘制到屏幕
g.drawImage(buffer,0,25,null); //所以它不会被蓝色关闭/最小/最大房间隐藏
public boolean isValidSquare(int col,int row)
{
if(col> -1& col< 8){return true;}
返回false;
}
public void renderChessBoard(Graphics g,int x,int y)
{
/ *渲染板和片* /
//感觉行广场是正方形,那么
//板也将是一个正方形;所以我们用任何
//边绘制最小,宽度或高度
boardPos [0] = x;
boardPos [1] = y;
drawBoard(g,x,y,boardWidth,boardHeight);
boardSquare = getBoardSquare(mousePos [0],mousePos [1]);
CheckersPiece = isCheckersPiece(boardSquare [0],boardSquare [1]);
boolean validSquare = isValidSquare(boardSquare [0],boardSquare [1]);
if(validSquare)
{
if(CheckersPiece)
{
selectSquare(g,boardSquare [0],boardSquare [1]);
}
else
{
if(pieceSelected)
{
int selectedCol = selectedSquarePos [0];
int selectedRow = selectedSquarePos [1];
int toCol = boardSquare [0];
int toRow = boardSquare [1];
System.out.println(selectedCol ++ selectedRow ++ toCol ++ toRow);
if(!CheckersBoard.move(selectedSquarePos,boardSquare))//不是有效的移动;
{
pieceSelected = false;
}
}
}
}
parseBoard(CheckersBoard.board,g);
}
public void drawBoard(Graphics g,int Bx,int By,int Bw,int Bh)
{
int numberRowsDrawed = 0;
int rH = Bh / 8;
int rW = Bw; //行宽与板的宽度相同,因为板和行共享相同的边
while(numberRowsDrawed< 8)
{
int rY =(numberRowsDrawed * rH)+ By;
//行X与板X相同,因为板和行共享相同的边
int rX = Bx;
颜色EVEN = SquareEven;
颜色ODD = SquareOdd;
//是是即时颜色现在是奇怪的,反之亦然,因为行只有现在有行计数,所以他们从0开始,不要
//包括
以上的行if ((numberRowsDrawed%2)!= 0){EVEN = SquareOdd; ODD = SquareEven;}
drawRow(g,rX,rY,rW,rH,EVEN,ODD);
numberRowsDrawed + = 1;
}
public void drawRow(Graphics g,int x,int y,int width,int height,Color EVEN,Color ODD )
{
System.out.println(Board Y:+ y);
int squareW = width / 8;
int squareH = height;
int numberSquaresCreated = 0;
while(numberSquaresCreated< 8)
{
//需要一个特殊情况,因为Java的模数使用除法(所以它会给出一个除以0的错误)STUPID JAVA !!!!!!
if(numberSquaresCreated == 0)
{
g.setColor(EVEN);
g.fillRect(x,y,squareW,squareH);
}
else
{
if(numberSquaresCreated%2 == 0){g.setColor(EVEN);}
else {g.setColor(ODD); }
int sX = x +(squareW * numberSquaresCreated);
g.fillRect(sX,y,squareW,squareH);
}
numberSquaresCreated + = 1;
}
}
public void drawMan(Graphics g,int boardRow,int boardCol,Color pieceColor)
{
int x = boardPos [0] ;
int y = boardPos [1];
int pixelPosX = x +((boardWidth / 8)* boardRow);
int pixelPosY = y +((boardHeight / 8)* boardCol);
g.setColor(pieceColor);
g.fillOval(pixelPosX + 13,pixelPosY + 13,(boardWidth / 8) - 26,(boardHeight / 8) - 26);
}
public void drawKing(Graphics g,int boardRow,int boardCol,Color pieceColor,Color crownColor)
{
drawMan(g,boardRow,boardCol,pieceColor );
g.setColor(crownColor);
int x = boardPos [0];
int y = boardPos [1];
double DsizeFactor =((float)boardHeight / 8.0)/ 3.75;
int sizeFactor =(int)DsizeFactor;
int pixelPosX = x +((boardWidth / 8) - sizeFactor)/ 2 +((boardWidth / 8)* boardRow);
int pixelPosY = y +((boardHeight / 8) - sizeFactor)/ 2 +((boardHeight / 8)* boardCol);
int [] xPoints = {pixelPosX,pixelPosX,pixelPosX + sizeFactor,pixelPosX + sizeFactor,pixelPosX +((sizeFactor * 3)/ 4),pixelPosX +(sizeFactor / 2),pixelPosX +(sizeFactor / 4)} ;
int [] yPoints = {pixelPosY,pixelPosY + sizeFactor,pixelPosY + sizeFactor,pixelPosY,pixelPosY +(sizeFactor / 2),pixelPosY,pixelPosY +(sizeFactor / 2)};
g.fillPolygon(xPoints,yPoints,7);
}
public void selectSquare(Graphics g,int bSX,int bSY)
{
g.setColor(Color.YELLOW);
/ * + 10是偏移文本(xy轴是文本的左下角NOT左上角* /
pieceSelected = true;
int squareX = boardPos [0] + (boardWidth / 8)* bSX;
int squareY = 10 + boardPos [1] +(boardHeight / 8)* bSY;
selectedSquarePos [0] = bSX;
selectedSquarePos [1] = bSY;
g.drawString(Selected,squareX,squareY);
}
//数据处理和恢复方法
public void parseBoard (String [] [] Board,Graphics g)
{
int row = 0;
int col = 0;
for(String [] rowOfPieces:Board)
{
for(String piece:rowOfPieces)
{
if(piece!=X)
{
颜色PIECE_COLOR = P1_Color;
if (piece.contains(P2)){PIECE_COLOR = P2_Color;}
if(piece.contains(C))
{
drawMan(g,col,row,PIECE_COLOR);
}
if(piece.contains(K))
{
颜色Crown_Color = P2_Color;
if(PIECE_COLOR!= P1_Color){Crown_Color = P1_Color;}
drawKing(g,col,row,PIECE_COLOR,Crown_Color);
}
}
col + = 1;
}
row + = 1;
col = 0;
}
}
public int [] getBoardSquare(int x,int y)
{
// row or col = boardpos - 偏移/行高或宽度
如果((x (boardPos [0] + boardWidth))|(y& ;(boardPos [1] + boardHeight)))
{
int [] BS = {-1,-1};
返回BS;
}
int col =(x - boardPos [0])/(boardWidth / 8);
int row =(y - boardPos [1])/(boardHeight / 8);
int [] BS = {col,row};
返回BS;
}
public boolean isCheckersPiece(int BoardSquareX,int BoardSquareY)
{
int Px = BoardSquareX;
int Py = BoardSquareY;
if(Px == -1& Py == -1)
{
return false;
}
String Square = CheckersBoard.board [Py] [Px];
return Square!=X;
}
}
你忘了 static
:
public static void main [] args)
但是为了真正启动应用程序,您应该从该方法启动它,不仅仅是拥有它以下是如何启动它:
public static void main(String [] args){
javax.swing。 SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
AppFrame frame = new AppFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
请注意 EXIT_ON_CLOSE
- 当您按下X按钮时,这将使JVM退出。否则您的应用程序将继续运行,您甚至可能不会注意到。
I recently took all my code a manually imported it into an eclipse project from BlueJ, I then got use to the settings up "Run Configurations", finally thinking I was home free. Then I ran the code, and I got this error
java.lang.NoSuchMethodError: main
Exception in thread "main"
so I figured I had to add a main method (I never had to do this in BlueJ, why?). So I did that just called the constructor method (in BlueJ I would just create a new object and the JFrame would show). So I did that, same error. After trying different things (such as moving the code in the constructor to a different method etc.). I just put this in for the main method:
public void main(String[] args)
{
System.out.println("Hello, this is main why won't Java find this.");
}
After that I still got the same error, so I then decided to add it to all my classes to make sure it wasn't using another class as the main class. Still same error, so I come to you wondering if any of you have encountered this problem. Also I did search Google and all I found was problems with private
classes etc and sense all my classes are public
(hey I come from Python :) ). I knew that wasn't the problem. Help Please :)
Picture of my Run Configuration
This is my main method code
WARNING: LONG
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class AppFrame extends JFrame
{
public String status = "Status:";// Status of Applet
public int paint_count = 1;// Number of times applet has been painted
public int[] mousePos = {-1, -1};// Stores Mouse's Last Clicked X and Y Cordinates
public int[] boardPos = {-1, -1};//Stores The Board's X and Y Cordinates
public int[] selectedSquarePos = {-1, -1};
public int[] boardSquare = {-1, -1};//Stores Last Clicked Square
public Color P1_Color = Color.GRAY;
public Color P2_Color = Color.WHITE;
public Color SquareEven = Color.BLACK;
public Color SquareOdd = Color.RED;// pieces move on this one
public int boardHeight = 400;
public int boardWidth = 400;
public boolean pieceSelected = false;
public boolean CheckersPiece = false;
public Board CheckersBoard = new Board();
public Image buffer = null;
public Graphics bg = null;
public void main(String[] args)
{
System.out.println("Hello, this is main why won't Java find this.");
}
public AppFrame()
{
super("JCheckers");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(900,900);
setVisible(true);
buffer = createImage(getWidth(), getHeight());
boardHeight = getHeight() - 40; // 20 pixel border at top and bottom and 20 pixels for blue bar
boardWidth = getWidth() - 40; // 20 pixel border at left and right
bg = buffer.getGraphics();
addMouseListener(new MouseAdapter()
{
public void mouseClicked (MouseEvent e)
{
handleClick(e);
}
}
);
}
public void handleClick(MouseEvent e)
{
/* Handles tracking of mouse clicks; DOES NOT HANDLE DISPLAY, it just updates the data and calls redraw */
mousePos[0] = e.getX();
mousePos[1] = e.getY();
repaint();
}
public void paint(Graphics g)
{
super.paint(g);
render(g);
}
public void render(Graphics g)
{
bg.clearRect(0,0, getWidth(), getHeight());
//Draw Chess Board and Pieces
renderChessBoard(bg, 20, 20);
// Display Info
System.out.println(status);
System.out.println(String.format("Last Mouse Click @ (X:%d Y:%d)", mousePos[0], mousePos[1]) );
System.out.println("Paint #" + paint_count );
System.out.println(String.format("Board Square (x:%s, y:%s) %b", boardSquare[0], boardSquare[1], CheckersPiece) );
System.out.println(CheckersBoard.status );
paint_count += 1;
// Draw Image to Screen
g.drawImage(buffer, 0, 25, null);// so it does not get hidden by the blue close/min/max room
}
public boolean isValidSquare(int col, int row)
{
if (col > -1 & col < 8) {return true;}
return false;
}
public void renderChessBoard(Graphics g, int x, int y)
{
/* Renders board and pieces*/
// sense the row squares are well squares then the
// board will be a square also; so we draw it with whatever
// side is smallest, width or height
boardPos[0] = x;
boardPos[1] = y;
drawBoard(g, x, y, boardWidth, boardHeight);
boardSquare = getBoardSquare(mousePos[0], mousePos[1]);
CheckersPiece = isCheckersPiece(boardSquare[0], boardSquare[1]);
boolean validSquare = isValidSquare(boardSquare[0], boardSquare[1]);
if (validSquare)
{
if (CheckersPiece)
{
selectSquare(g, boardSquare[0], boardSquare[1]);
}
else
{
if (pieceSelected)
{
int selectedCol = selectedSquarePos[0];
int selectedRow = selectedSquarePos[1];
int toCol = boardSquare[0];
int toRow = boardSquare[1];
System.out.println(selectedCol + " " + selectedRow + " " + toCol + " " + toRow);
if (!CheckersBoard.move(selectedSquarePos, boardSquare)) // not a valid move;
{
pieceSelected = false;
}
}
}
}
parseBoard(CheckersBoard.board, g);
}
public void drawBoard(Graphics g, int Bx, int By, int Bw, int Bh)
{
int numberRowsDrawed = 0;
int rH = Bh / 8;
int rW = Bw; // Row width is the same as the Board's width because the board and the row share the same sides
while (numberRowsDrawed < 8)
{
int rY = (numberRowsDrawed * rH) + By;
// Row X is the same as the Board X because the board and the row share the same sides
int rX = Bx;
Color EVEN = SquareEven;
Color ODD = SquareOdd;
// Yes Yes The EVEN Color is now odd and vica versa its because rows only now there row counts and so they start at 0 and don't
// include the rows above
if ((numberRowsDrawed % 2) != 0) {EVEN = SquareOdd; ODD = SquareEven;}
drawRow(g, rX, rY, rW, rH, EVEN, ODD);
numberRowsDrawed +=1;
}
}
public void drawRow(Graphics g, int x, int y, int width, int height, Color EVEN, Color ODD)
{
System.out.println("Board Y: " + y);
int squareW = width / 8;
int squareH = height;
int numberSquaresCreated = 0;
while (numberSquaresCreated < 8)
{
// needs a special case because Java's modulo uses division (so it would give a divide by 0 error) STUPID JAVA!!!!!!
if (numberSquaresCreated == 0)
{
g.setColor(EVEN);
g.fillRect(x, y, squareW, squareH);
}
else
{
if (numberSquaresCreated % 2 == 0){g.setColor(EVEN);}
else {g.setColor(ODD);}
int sX = x + (squareW * numberSquaresCreated);
g.fillRect(sX, y, squareW, squareH);
}
numberSquaresCreated +=1;
}
}
public void drawMan(Graphics g, int boardRow, int boardCol, Color pieceColor)
{
int x = boardPos[0];
int y = boardPos[1];
int pixelPosX = x + ((boardWidth / 8) * boardRow);
int pixelPosY = y + ((boardHeight / 8) * boardCol);
g.setColor(pieceColor);
g.fillOval(pixelPosX + 13, pixelPosY + 13, (boardWidth / 8) - 26, (boardHeight / 8) - 26);
}
public void drawKing(Graphics g, int boardRow, int boardCol, Color pieceColor, Color crownColor)
{
drawMan(g, boardRow, boardCol, pieceColor);
g.setColor(crownColor);
int x = boardPos[0];
int y = boardPos[1];
double DsizeFactor = ( (float) boardHeight / 8.0) / 3.75;
int sizeFactor = (int) DsizeFactor;
int pixelPosX = x + ((boardWidth / 8) - sizeFactor) / 2 + ((boardWidth / 8) * boardRow);
int pixelPosY = y + ((boardHeight / 8) - sizeFactor) / 2 + ((boardHeight / 8) * boardCol);
int[] xPoints = {pixelPosX, pixelPosX, pixelPosX + sizeFactor, pixelPosX + sizeFactor, pixelPosX + ((sizeFactor * 3) / 4), pixelPosX + (sizeFactor / 2), pixelPosX + (sizeFactor / 4) };
int[] yPoints = {pixelPosY, pixelPosY + sizeFactor, pixelPosY + sizeFactor, pixelPosY, pixelPosY + (sizeFactor / 2), pixelPosY, pixelPosY + (sizeFactor / 2)};
g.fillPolygon(xPoints, yPoints, 7);
}
public void selectSquare(Graphics g, int bSX, int bSY)
{
g.setColor(Color.YELLOW);
/*+10 is to offset text (the xy cordinates are the bottom left side of the text NOT top left.*/
pieceSelected = true;
int squareX = boardPos[0] + (boardWidth / 8) * bSX;
int squareY = 10 + boardPos[1] + (boardHeight / 8) * bSY;
selectedSquarePos[0] = bSX;
selectedSquarePos[1] = bSY;
g.drawString("Selected", squareX, squareY);
}
// Data Handling and Retreiving methods
public void parseBoard(String[][] Board, Graphics g)
{
int row = 0;
int col = 0;
for (String[] rowOfPieces : Board)
{
for (String piece : rowOfPieces)
{
if (piece != "X")
{
Color PIECE_COLOR = P1_Color;
if (piece.contains("P2")) {PIECE_COLOR = P2_Color;}
if (piece.contains("C"))
{
drawMan(g, col, row, PIECE_COLOR);
}
if (piece.contains("K"))
{
Color Crown_Color = P2_Color;
if (PIECE_COLOR != P1_Color) {Crown_Color = P1_Color;}
drawKing(g, col, row, PIECE_COLOR, Crown_Color);
}
}
col+=1;
}
row +=1;
col = 0;
}
}
public int[] getBoardSquare(int x, int y)
{
//row or col = boardpos - offset / row height or width
if ((x < boardPos[0]) | (y < boardPos[1]) | (x > (boardPos[0] + boardWidth)) | (y > (boardPos[1] + boardHeight)) )
{
int[] BS = {-1, -1};
return BS;
}
int col = (x - boardPos[0]) / (boardWidth / 8);
int row = (y - boardPos[1]) / (boardHeight / 8);
int[] BS = {col, row};
return BS;
}
public boolean isCheckersPiece(int BoardSquareX, int BoardSquareY)
{
int Px = BoardSquareX;
int Py = BoardSquareY;
if (Px == -1 & Py == -1)
{
return false;
}
String Square = CheckersBoard.board[Py][Px];
return Square != "X";
}
}
You forgot static
:
public static void main(String[] args)
But in order to really start your application, you should launch it from that method, not merely have it. Here is how to start it:
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
AppFrame frame = new AppFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
It's important to note the EXIT_ON_CLOSE
- this will make the JVM exit when you press the X button. Otherwise your application will continue running, and you might even not notice.
这篇关于Eclipse:Java,没有找到主要方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!