I have just exported a Java game I made to a Runnable JAR.
The game has an opening screen (a class named OpeningScreen
extending JPanel). When you press ENTER, it's supposed to go from the opening screen to the game itself (create a new Board
instance and add it to the JPanel.)
It works fine inside Eclipse.
An instance of OpeningScreen
is created in the class with main()
, named Starter
.
When exporting, I set the Starter class as the "Launch configuration", and set "Library handling" to "Extract required libararies into generated JAR".
After exporting, I get a window saying that it exported with compile errors, but doesn't tell me which errors exactly.
When starting the program from the generated JAR, the opening screen does show, but pressing ENTER won't start the game, although it does work inside Eclipse.
Here's the code for OpeningScreen
:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class OpeningScreen extends JPanel implements KeyListener{
private static final long serialVersionUID = 1L;
public OpeningScreen(){
setFocusable(true);
setVisible(true);
addKeyListener(this);
}
public void paint(Graphics g){
super.paint(g);
setBackground(Color.BLACK);
Graphics2D g2d = (Graphics2D) g;
// A lot of drawing Strings.
}
public void startGame(){
JFrame frame = new JFrame("Pong Battle");
frame.setSize(500,500);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Board board = new Board();
frame.add(board);
frame.setVisible(true);
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key==KeyEvent.VK_ENTER)startGame();
}
public void keyReleased(KeyEvent arg0) {
}
public void keyTyped(KeyEvent arg0) {
}
}
EDIT: Starter
class:
import javax.swing.*;
import java.awt.*;
public class Starter extends JFrame {
public Starter(){
setSize(500,500);
setResizable(false);
setTitle("Pong Battle");
setDefaultCloseOperation(EXIT_ON_CLOSE);
OpeningScreen openingS = new OpeningScreen();
add(openingS);
setVisible(true);
}
public static void main(String[]args){
Starter starter = new Starter();
}
}
What could be the problem? Thanks