我在可执行 jar 中正确启动图形时遇到问题。jar 包含所有需要的文件(main.class、main$1.class、HighScore.txt、META-INF 等等),并且程序本身正在正确启动。图形只是不出现。
主要的:
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.io.*;
import java.util.Scanner;
import javax.swing.JFrame;
public class main
{
public static int position = 0;
public static long points = 0;
public static boolean happyInt = true;
public static int highScore;
public static void main(String[] args) throws FileNotFoundException
{
JFrame app = new JFrame();
Canvas window = new Canvas();
window.setVisible(true);
window.setIgnoreRepaint(true);
window.setSize(1100, 145);
app.setResizable(false);
app.setIgnoreRepaint(true);
app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
app.add(window);
app.pack();
app.setVisible(true);
app.setTitle("Shine has made a game!");
Graphics g = window.getGraphics();
g.drawString("Working", 10, 20);
listener(app, window);
@SuppressWarnings("resource")
Scanner input = new Scanner(new File("HighScore.txt"));
while(input.hasNextInt())
{
highScore = input.nextInt();
}
g.drawString("Working", 10, 20);
Graphics graphics = null;
graphics(window);
}
public static void highScore() throws FileNotFoundException
{
@SuppressWarnings("resource")
PrintStream output = new PrintStream(new File("HighScore.txt"));
if(points > highScore)
{
output.println(points);
}
else
{
output.println(highScore);
}
}
public static void graphics(Canvas window)
{
Graphics g = window.getGraphics();
Graphics graphics = null;
while(happyInt)
{
if(position < 11)
{
position++;
}
else
{
position = 1;
}
try
{
// Draw stuff here using Java's Graphics Object!!!
g.setColor(Color.black);
g.fillRect(0, 0, 1100, 145);
g.setColor(Color.magenta);
if(position == 1)
{
g.setColor(Color.white);
}
g.fillRect(0,0,100,100);
g.setColor(Color.blue);
if(position == 2)
{
g.setColor(Color.white);
}
g.fillRect(100,0,100,100);
g.setColor(Color.green);
if(position == 3)
{
g.setColor(Color.white);
}
g.fillRect(200,0,100,100);
g.setColor(Color.yellow);
if(position == 4)
{
g.setColor(Color.white);
}
g.fillRect(300,0,100,100);
g.setColor(Color.orange);
if(position == 5)
{
g.setColor(Color.white);
}
g.fillRect(400,0,100,100);
g.setColor(Color.red);
if(position == 6)
{
g.setColor(Color.white);
}
g.fillRect(500,0,100,100);
g.setColor(Color.orange);
if(position == 7)
{
g.setColor(Color.white);
}
g.fillRect(600,0,100,100);
g.setColor(Color.yellow);
if(position == 8)
{
g.setColor(Color.white);
}
g.fillRect(700,0,100,100);
g.setColor(Color.green);
if(position == 9)
{
g.setColor(Color.white);
}
g.fillRect(800,0,100,100);
g.setColor(Color.blue);
if(position == 10)
{
g.setColor(Color.white);
}
g.fillRect(900,0,100,100);
g.setColor(Color.magenta);
if(position == 11)
{
g.setColor(Color.white);
}
g.fillRect(1000,0,100,100);
g.setColor(Color.white);
g.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 20));
g.drawString("Score: "+points, 5, 120);
g.drawString("High score: "+highScore, 5, 140);
//g.drawString("For debug: "+happyInt, 5, 160);
// Let the OS have a little time...
//Thread.yield();
try
{
Thread.sleep(100-points/25);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
finally
{
if( graphics != null )
{
graphics.dispose();
}
}
}
}
public static void listener(JFrame app, Canvas window)
{
final Graphics g = window.getGraphics();
app.addKeyListener( new KeyAdapter() {
public void keyPressed( KeyEvent e ) {
if( e.getKeyCode() == KeyEvent.VK_SPACE )
{
if(position == 6)
{
points = points + 100;
}
if(position == 5 || position == 7)
{
points = points + 50;
}
if(position > 7 || position < 5)
{
happyInt = false;
g.setColor(Color.black);
g.fillRect(0, 0, 1100, 100);
position = 12;
try
{
highScore();
}
catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
}
}
}
});
}
}
(我知道监听器不是处理这个问题的最佳方式;我会改变它。)我添加了“工作”部分来检查代码是否在停止并且在绘制图形之前它是否在工作,但不是在绘制之后。有谁知道什么会阻止它?