Java:在可运行的JAR文件和Eclipse中获得不同的结果

huangapple 未分类评论42阅读模式
英文:

Java: Getting a different result in runnable jar compared to eclipse

问题

以下是您提供的内容的翻译:

public class Start {

    public static void main(String[] args) throws ParseException {
        // TODO Auto-generated method stub
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        SimpleDateFormat sdformat = new SimpleDateFormat("yyyy/MM/dd");
        Date bday = sdformat.parse("2020/02/01");
        LocalDate now = LocalDate.now();
        String toda = dtf.format(now);
        Date today = sdformat.parse(toda);
        if (today.after(bday)) {
            new JWelcome();
        } else {
            new JWrongDate();
        }
    }
}

public class JGame extends JWin {
    private static final long serialVersionUID = 1L;
    private JLabel jlabel;
    private JWelcome jwelcome;
    private JGWindowListener jgwl;
    private JGMBar jgmb;
    private JPanel northpanel;
    private JPanel centerpanel;
    private JPanel eastpanel;
    private JPanel jsouthpanel;

    private JButton jbutton_A;
    private JButton jbutton_B;
    private JButton jbutton_C;
    private JButton jbutton_D;
    private JButton jbfifty_fifty;
    private JTArea jtarea;
    private JGameListener jgml;
    private JFile jfile;
    private JFile jfile_A;
    private JFile jfile_B;
    private JFile jfile_C;
    private JFile jfile_D;
    private JFile jsolution;
    private String answer_A;
    private String answer_B;
    private String answer_C;
    private String answer_D;
    private String solution;

    private int index;

    public JGame(JWelcome jwelcome) throws ParseException {
        super();
        // ...(省略其他内容)
    }

    public JButton getJButton_A() {
        return jbutton_A;
    }

    public JButton getJButton_B() {
        return jbutton_B;
    }

    // ...(省略其他内容)

    public void close() {
        dispose();
        setVisible(false);
        System.exit(0);
    }
}

编辑 2:
您用于运行游戏的命令:

C:\Program Files\Java\jdk-13.0.2\bin\javaw.exe -Dfile.encoding=Cp1252 -classpath "C:\Users\neilp\eclipse-workspace\Brigit Birthday Quiz\bin" start
英文:

So I've been making a game for someone's birthday, I finally finished, but I'm having issues exporting it into a runnable jar file. What the game is is similar to "Who wants to be a millionaire", so it opens a window asking the contestant their name, then it goes on to ask the questions. At the end, it opens a letter that I wrote to them.

Now for the actual issue. I run the project in Eclipse and it works like a charm. However when I run it as a jar, I get the name prompt, then it all closes down. I don't get any error messages that I can see. I've tried running it through command prompt to see if anything would happen but no dice. Any help would be greatly appreciated with this!

EDIT: Below is my Start class and my JGame class:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class start {

public static void main(String[] args) throws ParseException {
	// TODO Auto-generated method stub
	DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
	SimpleDateFormat sdformat = new SimpleDateFormat("yyyy/MM/dd");
	Date bday = sdformat.parse("2020/02/01");
	LocalDate now = LocalDate.now();
	String toda = dtf.format(now);
	Date today = sdformat.parse(toda);
	if (today.after(bday)) {
		new JWelcome();
	} else {
		new JWrongDate();
	}
}

}

JGame:

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import java.util.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.text.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class JGame extends JWin {
private static final long serialVersionUID = 1L;
private JLabel jlabel;
private JWelcome jwelcome;
private JGWindowListener jgwl;
private JGMBar jgmb;
private JPanel northpanel;
private JPanel centerpanel;
private JPanel eastpanel;
private JPanel jsouthpanel;

private JButton jbutton_A;
private JButton jbutton_B;
private JButton jbutton_C;
private JButton jbutton_D;
private JButton jbfifty_fifty;
private JTArea jtarea;
private JGameListener jgml;
private JFile jfile;
private JFile jfile_A;
private JFile jfile_B;
private JFile jfile_C;
private JFile jfile_D;
private JFile jsolution;
private String answer_A;
private String answer_B;
private String answer_C;
private String answer_D;
private String solution;
// private JLabel jwon;

private int index;

public JGame(JWelcome jwelcome) throws ParseException {
	super();
	this.jwelcome = jwelcome;
	jgwl = new JGWindowListener(this);
	jgml = new JGameListener(this);
	jgmb = new JGMBar(this);
	setTitle("Brigit's Birthday Game");
	setSize(800, 300);
	
	jlabel = new JLabel("Hello " + jwelcome.getJTArea().getText() + "! Welcome to your birthday game!");

	jtarea = new JTArea("Question");

	jfile = new JFile();
	jfile_A = new JFile();
	jfile_B = new JFile();
	jfile_C = new JFile();
	jfile_D = new JFile();
	jsolution = new JFile();

	index = 0;
	jtarea.setText(jfile.loadQuestions("Questions.txt", index));

	Rectangle r = new Rectangle();
	jtarea.scrollRectToVisible(r);
	getContentPane().setLayout(new BorderLayout());

	// jwon = new JLabel("Select the correct answer");
	northpanel = new JPanel();
	northpanel.setLayout(new FlowLayout());
	northpanel.add(jlabel);
	northpanel.setBackground(Color.magenta);
	getContentPane().add((BorderLayout.NORTH), northpanel);
	addWindowListener(jgwl);
	setJMenuBar(jgmb);

	centerpanel = new JPanel();
	centerpanel.setLayout(new FlowLayout());
	centerpanel.add(jtarea);
	centerpanel.setBackground(Color.magenta);

	getContentPane().add(BorderLayout.CENTER, centerpanel);

	jbfifty_fifty = new JButton("50:50");
	jbfifty_fifty.addActionListener(jgml);

	eastpanel = new JPanel();
	eastpanel.setLayout(new FlowLayout());
	eastpanel.add(jbfifty_fifty);
	eastpanel.setBackground(Color.magenta);

	getContentPane().add(BorderLayout.EAST, eastpanel);

	// jfile_A.loadQuestions("Answers_A.txt", 0
	answer_A = (String) jfile_A.loadQuestions("Answers_A.txt", index);
	answer_B = (String) jfile_B.loadQuestions("Answers_B.txt", index);
	answer_C = (String) jfile_C.loadQuestions("Answers_C.txt", index);
	answer_D = (String) jfile_D.loadQuestions("Answers_D.txt", index);
	solution = (String) jsolution.loadQuestions("Solutions.txt", index);

	jbutton_A = new JButton("A: ");
	jbutton_B = new JButton("B: ");
	jbutton_C = new JButton("C: ");
	jbutton_D = new JButton("D: ");

	jbutton_A.setText("A: " + answer_A);
	jbutton_B.setText("B: " + answer_B);
	jbutton_C.setText("C: " + answer_C);
	jbutton_D.setText("D: " + answer_D);

	jsouthpanel = new JPanel();
	jsouthpanel.setLayout(new FlowLayout());

	jbutton_A.addActionListener(jgml);
	jbutton_B.addActionListener(jgml);
	jbutton_C.addActionListener(jgml);
	jbutton_D.addActionListener(jgml);

	jsouthpanel.add(jbutton_A);
	jsouthpanel.add(jbutton_B);
	jsouthpanel.add(jbutton_C);
	jsouthpanel.add(jbutton_D);

	jsouthpanel.setBackground(Color.BLACK);
	jsouthpanel.setForeground(Color.BLACK);

	getContentPane().add(BorderLayout.SOUTH, jsouthpanel);
	setVisible(true);
}

public JButton getJButton_A() {
	return jbutton_A;
}

public JButton getJButton_B() {
	return jbutton_B;
}

public JButton getJButton_C() {
	return jbutton_C;
}

public JButton getJButton_D() {
	return jbutton_D;
}

public JFile getFile_A() {
	return jfile_A;
}

public JFile getFile_B() {
	return jfile_B;
}

public JFile getFile_C() {
	return jfile_C;
}

public JFile getFile_D() {
	return jfile_D;
}

public String getAnswer_A() {
	return answer_A;
}

public String getAnswer_B() {
	return answer_B;
}

public String getAnswer_C() {
	return answer_C;
}

public String getAnswer_D() {
	return answer_D;
}

public String getSolution() {
	return solution;
}

public JPanel getSouthPanel() {
	return jsouthpanel;
}

public JFile getJFile() {
	return jfile;
}

public JTArea getJTArea() {
	return jtarea;
}

public int getIndex() {
	return index;
}

public void setIndex(int index) {
	this.index = index;
}

public JLabel getJLabel() {
	return jlabel;
}

public void setJLabel(JLabel jl) {
	jlabel = jl;
}

public JButton getFifty() {
	return jbfifty_fifty;
}

int nextQuestion() throws IOException {
	try {
		index = index + 1;
		if (index >= 20) {
			File file = new File("letter.docx");

			Desktop desktop = Desktop.getDesktop();
			if (file.exists())
				desktop.open(file);
			
			setIndex(0);
		}
	} catch (ArrayIndexOutOfBoundsException e) {
		System.err.println("Error: " + e);
	}
	jtarea.setText(jfile.loadQuestions("Questions.txt", index));
	answer_A = (String) jfile_A.loadQuestions("Answers_A.txt", index);
	answer_B = (String) jfile_B.loadQuestions("Answers_B.txt", index);
	answer_C = (String) jfile_C.loadQuestions("Answers_C.txt", index);
	answer_D = (String) jfile_D.loadQuestions("Answers_D.txt", index);
	solution = (String) jsolution.loadQuestions("Solutions.txt", index);
	jbutton_A.setText("A: " + answer_A);
	jbutton_B.setText("B: " + answer_B);
	jbutton_C.setText("C: " + answer_C);
	jbutton_D.setText("D: " + answer_D);

	return index;
}

void play() {
	jlabel.setText("Select one of the four answers");
	jbutton_A.addActionListener(jgml);
	jbutton_B.addActionListener(jgml);
	jbutton_C.addActionListener(jgml);
	jbutton_D.addActionListener(jgml);
}

void pause() {
	jlabel.setText("pause");
	jbutton_A.removeActionListener(jgml);
	jbutton_B.removeActionListener(jgml);
	jbutton_C.removeActionListener(jgml);
	jbutton_D.removeActionListener(jgml);
	jsouthpanel.setBackground(Color.BLACK);
}

public void paintComponent(Graphics g) {
	Image img = Toolkit.getDefaultToolkit().getImage(JGame.class.getResource("baloons.jpeg"));
	g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}

void close() {
	dispose();
	setVisible(false);
	System.exit(0);
}
}

EDIT 2:
Here is the command used to run my game:

C:\Program Files\Java\jdk-13.0.2\bin\javaw.exe -Dfile.encoding=Cp1252 -classpath "C:\Users\neilp\eclipse-workspace\Brigit Birthday Quiz\bin" start

huangapple
  • 本文由 发表于 2020年4月4日 19:51:12
  • 转载请务必保留本文链接:https://java.coder-hub.com/61027636.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定