1

我正在制作一个Talisman Game,到目前为止,我有方法来创建一个Random Characters和一个WIP Main Menu。现在,如果我从主类调用新游戏(创建角色)方法,一切都很好,但如果我从主手册中调用它,则JFrame变得透明并复制它们的背景(比如当窗口卡住时)

新的游戏方法是:

public Game(int playerNum) {

    for (int i = 0; i < chars.length; i++)
    chars[i] = false;

    this.playerNum=playerNum;

    players = new Player[playerNum];
    guiChars = new Gui_Chars[playerNum];

    Card.createCards();
    Game.randomChar();
    for (int j = 0; j < playerNum; j++)
        guiChars[j].update();
}

现在,Game,randomChar() 创建角色:

public static void randomChar() {
    for (int i = 0; i < playerNum; i++) {
        do {
            chooseChar = rnd.nextInt(14);
        } while (chars[chooseChar]);
        switch (chooseChar) {
            case 0: {
                players[i] = new Char_ASSASIN();
                guiChars[i] = new Gui_Chars("Player " + (i + 1) + " - "
                                + "Assasin", players[i], "Images/assassin.jpg");
                System.out.println("Player " + (i + 1) + " is an Assasin");
                chars[0] = true;
                break;
            }
            case 1: {
                players[i] = new Char_DRUID();
                guiChars[i] = new Gui_Chars("Player " + (i + 1) + " - "
                                    + "Druid", players[i], "Images/druid.jpg");
                System.out.println("Player " + (i + 1) + " is a Druid");
                chars[1] = true;
                break;
            }
            case 2: {
                players[i] = new Char_DWARF();
                guiChars[i] = new Gui_Chars("Player " + (i + 1) + " - "
                                   + "Dwarf", players[i], "Images/dwarf.jpg");
                System.out.println("Player " + (i + 1) + " is a Dwarf");
                chars[2] = true;
                break;
            }
            case 3: {
                players[i] = new Char_ELF();
                guiChars[i] = new Gui_Chars(
                         "Player " + (i + 1) + " - " + "Elf", players[i],
                                                           "Images/elf.jpg");
                System.out.println("Player " + (i + 1) + " is an Elf");
                chars[3] = true;
                break;
            }
            case 4: {
                players[i] = new Char_GHOUL();
                guiChars[i] = new Gui_Chars("Player " + (i + 1) + " - "
                                  + "Ghoul", players[i], "Images/ghoul.jpg");
                System.out.println("Player " + (i + 1) + " is a Ghoul");
                chars[4] = true;
                break;
            }
            case 5: {
                players[i] = new Char_MINSTREL();
                guiChars[i] = new Gui_Chars("Player " + (i + 1) + " - "
                            + "Minesrel", players[i], "Images/minstrel.jpg");
                System.out.println("Player " + (i + 1) + " is a Minstrel");
                chars[5] = true;
                break;
            }
            case 6: {
                players[i] = new Char_MONK();
                guiChars[i] = new Gui_Chars("Player " + (i + 1) + " - "
                                    + "Monk", players[i], "Images/monk.jpg");
                System.out.println("Player " + (i + 1) + " is a Monk");
                chars[6] = true;
                break;
            }
            case 7: {
                players[i] = new Char_PRIEST();
                guiChars[i] = new Gui_Chars("Player " + (i + 1) + " - "
                                + "Priest", players[i], "Images/priest.jpg");
                System.out.println("Player " + (i + 1) + " is a Priest");
                chars[7] = true;
                break;
            }
            case 8: {
                players[i] = new Char_PROPHETESS();
                guiChars[i] = new Gui_Chars("Player " + (i + 1) + " - "
                        + "Prophetess", players[i], "Images/prophetess.jpg");
                System.out.println("Player " + (i + 1) + " is a Prophetess");
                chars[8] = true;
                break;
            }
            case 9: {
                players[i] = new Char_SORCERESS();
                guiChars[i] = new Gui_Chars("Player " + (i + 1) + " - "
                          + "Sorceress", players[i], "Images/sorceress.jpg");
                System.out.println("Player " + (i + 1) + " is a Sorceress");
                chars[9] = true;
                break;
            }
            case 10: {
                players[i] = new Char_THIEF();
                guiChars[i] = new Gui_Chars("Player " + (i + 1) + " - "
                                  + "Thief", players[i], "Images/thief.jpg");
                System.out.println("Player " + (i + 1) + " is a Thief");
                chars[10] = true;
                break;
            }
            case 11: {
                players[i] = new Char_TROLL();
                guiChars[i] = new Gui_Chars("Player " + (i + 1) + " - "
                                  + "Troll", players[i], "Images/troll.jpg");
                System.out.println("Player " + (i + 1) + " is a Troll");
                chars[11] = true;
                break;
            }
            case 12: {
                players[i] = new Char_WARRIOR();
                guiChars[i] = new Gui_Chars("Player " + (i + 1) + " - "
                              + "Warrior", players[i], "Images/warrior.jpg");
                System.out.println("Player " + (i + 1) + " is a Warrior");
                chars[12] = true;
                break;
            }
            case 13: {
                players[i] = new Char_WIZARD();
                guiChars[i] = new Gui_Chars("Player " + (i + 1) + " - "
                                + "Wizard", players[i], "Images/wizard.jpg");
                System.out.println("Player " + (i + 1) + " is a Wizard");
                chars[13] = true;
                break;
            }
        }
    }
}

Characters 调用 GUI(从Characters类中接收信息,但那肯定不是问题):

public Gui_Chars(String character,Player player,String path) {

    super(character);
    setVisible(true);
    setSize(430, 420);

    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    revalidate();
}

我删除了代码的不重要部分,没有它也会发生问题......最后,这是MainMenu

public class MainMenu extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;
    JLabel num;
    JButton add, sub, start;
    private int i = 2;
    JFrame j ;
    Game game;

    JLayeredPane jp1,jp2,jp3;

    public MainMenu() {

        j= new JFrame("Talisman");
        j.setVisible(true);
        j.setSize(300, 300);
        j.setDefaultCloseOperation(EXIT_ON_CLOSE);

        jp1 = new JLayeredPane();
        jp2 = new JLayeredPane();
        jp3 = new JLayeredPane();   

        JPanel playerNum = new JPanel(new GridBagLayout());
        JPanel startPanel = new JPanel(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(30, 30, 30, 30);

        add = new JButton("+1");
        sub = new JButton("-1");
        start = new JButton("Start");

        start.setPreferredSize(new Dimension(100, 100));

        num = new JLabel("" + i);
        num.setFont(new Font("Serif", Font.BOLD, 40));

        gbc.gridy = 0;
        playerNum.add(add, gbc);
        add.addActionListener(this);
        add.setActionCommand("add");

        gbc.gridy = 1;
        playerNum.add(num, gbc);

        gbc.gridy = 2;
        playerNum.add(sub, gbc);
        sub.addActionListener(this);
        sub.setActionCommand("sub");

        startPanel.add(start, gbc);
        start.addActionListener(this);
        start.setActionCommand("start");

        jp1.add(new ContentPanel());
        jp2.add(playerNum);
        jp3.add(startPanel);

        jp1.setLayer(new ContentPanel(), 1);
        jp2.setLayer(playerNum, 2);
        jp3.setLayer(startPanel, 2);

        j.add(playerNum, BorderLayout.WEST);
        j.add(startPanel, BorderLayout.EAST);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String act = e.getActionCommand();

        if (act.equals("start")) {
            j.setVisible(false);
            j.dispose();
            game = new Game(i);
            game.startGame();

            //JFrame j = new JFrame();
            //j.setVisible(true);
            //j.setSize(300, 300);
        }
    }
}

对于长代码,我感到非常抱歉,但我试图在调试中坐在上面几个小时,却找不到问题。如果有人能帮助我了解正在发生的事情,我将非常感激......再次感谢,并对长代码感到抱歉......

4

1 回答 1

0

通过查看您的代码,它向我表明您不了解 Swings 绘制子系统。

最重要的一点是,永远不要在 Swing 的事件调度事件 (ETD) 中执行任何长的或阻塞的操作。

当您在 Main 中调用它时它起作用的原因实际上是侥幸。Java 不保证哪个线程将执行 Main 方法。你很幸运,这不是 ETD。启动 Swing 应用程序时,您必须始终执行以下操作

EventQueue.invokeLater(new Runnable() {
    public void run() {
        // Your launch code here
    }
}

显然,只在其中包含与 UI 相关的代码。

它现在不起作用的原因是您正在从您的动作侦听器中执行一段阻塞代码,这会阻止 ETD 更新屏幕。

for (int i = 0; i < chars.length; i++)
    chars[i] = false;

.
.
.

public static void randomChar() {
    for (int i = 0; i < playerNum; i++) {
        do {
            chooseChar = rnd.nextInt(14);
        } while (chars[chooseChar]); // This is never true...

哪个被称为

game = new Game(i);

您将希望更改您的游戏模型以允许从除 ETD 之外的线程上下文调用“randomChar”方法。我建议(为方便起见),像 SwingWoker (http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html

从您的代码示例中,我看不到“chars [x]”在哪里设置为真......

事实上,您可能想阅读整个Swing 中的并发教程......

于 2012-07-10T21:20:15.487 回答