1

I have a JAVA card game, which show four cards at each round.

Currently, the stop between each round is waiting for a \n input in console. But I'd like to change it to waiting for a keyboard "enter" on the GUI.

Following is my current code. Please let me know how should I change it?

Millions of thanks!!!

import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;



public class Game {

    public static void main(String args[]) throws IOException {

        Deck deck = new Deck();
        deck.shuffle();
        int aGame = 4;

        List<Card> cards = new ArrayList<Card> ();

        for(int i = 0; i < 52; i++) {
            cards.add(deck.deck.get(i));
            if(aGame == 1) {
                System.out.println(deck.deck.get(i).toString());
                System.out.println("Start!!!");

                JFrame f = new JFrame("Calculating 24!");
                GridLayout grid = new GridLayout(2, 2);
                f.setLayout(grid);

                f.addWindowListener(new WindowAdapter(){
                        public void windowClosing(WindowEvent e) {
                            System.exit(0);
                        }
                    });

                for(Card c: cards){
                    f.add(new LoadImageApp(c));
                }
                f.pack();
                f.setVisible(true);
                cards.clear();

                while (true) {
                    char c = (char) System.in.read();
                    if (c == '\n') {

                        f.setVisible(false);
                        break;
                    }
                }

                aGame = 4;
                if(i == 51) {
                    deck.shuffle();
                    i = -1;
                }
            }
            else if(aGame == 4) {
                System.out.println("Calculate based on the following four cards!");
                System.out.println(deck.deck.get(i).toString());
                aGame --;
            }
            else {
                System.out.println(deck.deck.get(i).toString());
                aGame --;
            }
        }
    }



}
4

2 回答 2

3

For Swing based applications key events should be handled using Key Bindings. However it may not be apparent to the end user that ENTER is required. A more UI centric approach is to use a dialog for this purpose

JOptionPane.showMessageDialog(frame, "Press ENTER to continue");

Using console based read methods such as InputStream#read is another source of confusion for users. Use a JTextComponent such as a JTextField to read user input in Swing applications.

于 2013-07-08T12:35:16.413 回答
3

如果接受值的 GUI 元素是 a JTextField,则可以添加一个ActionListener通常会在用户点击时响应的元素Enter

其他提示

1)

                f.addWindowListener(new WindowAdapter(){
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                });

将其更改为:

f.setDefaultCloseOperaiton(JFrame.EXIT_ON_CLOSE);

或更好..

f.setDefaultCloseOperaiton(JFrame.DISPOSE_ON_CLOSE);

第一个将具有与之前相同的效果,但第二个还将检查在退出之前没有运行非守护线程。

2)

不要尝试将 GUI 和命令行混合在一起。为两者编写应用程序的方式大不相同。

于 2013-07-08T12:46:28.590 回答