Java: получение другого результата в работающей банке по сравнению с затмением - PullRequest
0 голосов
/ 04 апреля 2020

Итак, я делал игру на чей-то день рождения, я наконец-то закончил, но у меня проблемы с экспортом в файл jar. Игра похожа на «Кто хочет стать миллионером», поэтому она открывает окно, спрашивая участника, как его зовут, а затем задает вопросы. В конце концов, он открывает письмо, которое я им написал.

Теперь о фактической проблеме. Я запускаю проект в Eclipse, и он работает как шарм. Однако, когда я запускаю его как банку, я получаю приглашение с именем, затем все закрывается. Я не получаю сообщений об ошибках, которые вижу. Я попытался запустить его через командную строку, чтобы увидеть, если что-нибудь случится, но не игра в кости. Любая помощь будет принята с благодарностью за это!

РЕДАКТИРОВАТЬ: Ниже мой стартовый класс и мой класс JGame:

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);
}
}

РЕДАКТИРОВАТЬ 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
...