【问题标题】:Java JFrame QuestionJava JFrame 问题
【发布时间】:2010-09-03 03:39:35
【问题描述】:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class tictac2 implements ActionListener{
    static boolean blue = true; //used to keep track of turns. if true, blue's turn, else, red's turn. Blue is x, red is o
    static int bWins = 0, rWins = 0;
    JFrame mainWindow;
    JPanel board;
    JButton[] buttons;
    public tictac2() {
        init();
    }
    private void init() {
        try { //Try to set the L&F to system
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) {
            e.toString();
            System.out.println("Couln't change look and feel. Using default");
        }
        mainWindow = new JFrame("Tic Tac Toe!");
        mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainWindow.setVisible(true);
        mainWindow.setSize(800,600);
        JMenuBar bar = new JMenuBar(); //Menu bar init
        JMenu file = new JMenu("File");
        JMenuItem newGame = new JMenuItem("New Game"), quitItem = new JMenuItem("Quit");
        file.add(newGame);
        file.addSeparator();
        file.add(quitItem);
        bar.add(file);
        mainWindow.getContentPane().add(bar, BorderLayout.NORTH); //Menu bar done
        newGameBoard(); //New Game Board
        JPanel infoPane = new JPanel();
        infoPane.setLayout(new GridLayout(1,3));
        JLabel turn = new JLabel("Blue Player's Turn"), spacer = new JLabel(""), score = new JLabel("Blue: 0, Red: 0");
        infoPane.add(turn);
        infoPane.add(spacer);
        infoPane.add(score);
        mainWindow.getContentPane().add(infoPane,BorderLayout.SOUTH);
        newGame.addActionListener(this); //Add action listeners
        quitItem.addActionListener(this);

    }
    private void newGameBoard() {
        board = new JPanel(); //Game Pane init
        board.setLayout(new GridLayout(3,3));
        buttons = new JButton[9];
        for (int i = 0; i <9; i++){
            buttons[i] = new JButton("");
            buttons[i].setOpaque(true);
            buttons[i].setFont(new Font("sansserif", Font.BOLD, 90));
            board.add(buttons[i]);
            buttons[i].addActionListener(this);
        }
        mainWindow.getContentPane().add(board,BorderLayout.CENTER); //Finish game pane init
    }
    public void actionPerformed(ActionEvent e){
        if (e.getSource() instanceof JButton){
            JButton j = (JButton)e.getSource();
            j.setForeground(tictac2.blue ? Color.BLUE:Color.RED);
            j.setText(tictac2.blue ? "X" : "O");
            j.setEnabled(false);
            tictac2.blue = !tictac2.blue;

        }
        else if (e.getSource() instanceof JMenuItem){
            JMenuItem j = (JMenuItem) e.getSource();
            if (j.getText().equals("Quit")) {
                System.exit(0);
            }
            else if (j.getText().equals("New Game")) {
                board.removeAll();
                mainWindow.remove(board);
                board = null;
                for (int i = 0; i < 9; i++) {
                    buttons[i] = null;
                }
                newGameBoard();
                tictac2.bWins = 0;
                tictac2.rWins = 0;
                tictac2.blue = true;
                mainWindow.repaint(100);
            }
        }
    }
    public static void main(String[] args) {
            new tictac2();
    }
}

我正在开发一个井字游戏。在里面我有2个按钮。每当我打新游戏时,应该运行 newGameBoard 函数并创建一个新棋盘。但是,屏幕只是一片空白!我无法弄清楚,并希望得到帮助。抱歉,如果我没有以正确的格式发布,我是新来的。

非常感谢!

【问题讨论】:

    标签: java swing jframe


    【解决方案1】:

    mainWindow.setVisible(true) 移动到init() 的末尾。在将所有内容添加到其中之前,您不希望将框架设置为可见。这样,它将验证和布局其子组件。

    另一种解决方案是在init() 的末尾手动调用mainWindow.validate()。如果在使框架可见后将组件添加到框架中,这就是您必须做的事情。

    【讨论】:

    • 我尝试将 mainWindow.setVisible(true) 移到 init() 的末尾,但对新游戏没有帮助。将它移到 newGameBoard() 的末尾仍然有效。
    • 是的,如果设置框架可见之后添加或删除组件,那么您需要调用validate() 以使框架重新布局其组件。
    • 谢谢。现在效果很好!现在做AI。呃。
    【解决方案2】:

    在 mainWindow 上使用 paintAll() 或 validate():

    mainWindow.validate();
    

    而不是:

    mainWindow.repaint(100);
    

    validatepaintAll() 如 API 文档中所述。

    您更改后的代码如下所示:

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class TicTac2 implements ActionListener{
        static boolean blue = true; //used to keep track of turns. if true, blue's turn, else, red's turn. Blue is x, red is o
        static int bWins = 0, rWins = 0;
        JFrame mainWindow;
        JPanel board;
        JButton[] buttons;
        public TicTac2() {
            init();
        }
        private void init() {
            try { //Try to set the L&F to system
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            }
            catch (Exception e) {
                e.toString();
                System.out.println("Couln't change look and feel. Using default");
            }
            mainWindow = new JFrame("Tic Tac Toe!");
            mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mainWindow.setVisible(true);
            mainWindow.setSize(800,600);
            JMenuBar bar = new JMenuBar(); //Menu bar init
            JMenu file = new JMenu("File");
            JMenuItem newGame = new JMenuItem("New Game"), quitItem = new JMenuItem("Quit");
            file.add(newGame);
            file.addSeparator();
            file.add(quitItem);
            bar.add(file);
            mainWindow.getContentPane().add(bar, BorderLayout.NORTH); //Menu bar done
            newGameBoard(); //New Game Board
            JPanel infoPane = new JPanel();
            infoPane.setLayout(new GridLayout(1,3));
            JLabel turn = new JLabel("Blue Player's Turn"), spacer = new JLabel(""), score = new JLabel("Blue: 0, Red: 0");
            infoPane.add(turn);
            infoPane.add(spacer);
            infoPane.add(score);
            mainWindow.getContentPane().add(infoPane,BorderLayout.SOUTH);
            newGame.addActionListener(this); //Add action listeners
            quitItem.addActionListener(this);
    
        }
        private void newGameBoard() {
            board = new JPanel(); //Game Pane init
            board.setLayout(new GridLayout(3,3));
            buttons = new JButton[9];
            for (int i = 0; i <9; i++){
                buttons[i] = new JButton("");
                buttons[i].setOpaque(true);
                buttons[i].setFont(new Font("sansserif", Font.BOLD, 90));
                board.add(buttons[i]);
                buttons[i].addActionListener(this);
            }
            mainWindow.getContentPane().add(board,BorderLayout.CENTER); //Finish game pane init
        }
        public void actionPerformed(ActionEvent e){
            if (e.getSource() instanceof JButton){
                JButton j = (JButton)e.getSource();
                j.setForeground(TicTac2.blue ? Color.BLUE:Color.RED);
                j.setText(TicTac2.blue ? "X" : "O");
                j.setEnabled(false);
                TicTac2.blue = !TicTac2.blue;
    
            }
            else if (e.getSource() instanceof JMenuItem){
                JMenuItem j = (JMenuItem) e.getSource();
                if (j.getText().equals("Quit")) {
                    System.exit(0);
                }
                else if (j.getText().equals("New Game")) {
                    board.removeAll();
                    mainWindow.remove(board);
                    board = null;
                    for (int i = 0; i < 9; i++) {
                        buttons[i] = null;
                    }
                    newGameBoard();
                    TicTac2.bWins = 0;
                    TicTac2.rWins = 0;
                    TicTac2.blue = true;
    
                    mainWindow.validate();
                    //mainWindow.repaint();
                }
            }
        }
        public static void main(String[] args) {
                new TicTac2();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-09-18
      • 1970-01-01
      • 2012-05-20
      • 1970-01-01
      • 2020-12-18
      • 2011-06-29
      • 2016-04-17
      • 1970-01-01
      相关资源
      最近更新 更多