为什么窗户组件没有完全出现?

时间:2016-03-29 08:55:50

标签: java swing user-interface

因此,对于一个课程项目,我正在构建单词拼写游戏。基本UI应如下所示:

但是在运行该程序后,它主要采用以下形式:

只有在我调整大小或最小化并恢复它之后才会更改为预期的形式。

这是源代码:

package game;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GameBlock {

    private static int i, j;
    private static JTextField textField;

    public static void main(String[] args) {
        GameBlock gb = new GameBlock();
    }

    public GameBlock(){
        JFrame frame = new JFrame("Word-A-Lot");
        frame.setBounds(230, 70, 900, 451);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        //frame.setResizable(false);
        frame.setVisible(true);

        JPanel panel = new JPanel();
        panel.setBounds(40, 71, 350, 262);
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0};
        gbl_panel.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        panel.setLayout(gbl_panel);
        frame.getContentPane().add(panel);

        JPanel panel_1 = new JPanel();
        panel_1.setBounds(450, 71, 339, 262);
        panel_1.setLayout(null);
        frame.getContentPane().add(panel_1);

        JLabel lblPlayer = new JLabel("Player 1");
        lblPlayer.setFont(new Font("Tw Cen MT", Font.PLAIN, 14));
        lblPlayer.setBounds(55, 26, 77, 14);
        panel_1.add(lblPlayer);

        JLabel label = new JLabel("00");
        label.setBounds(72, 51, 21, 14);
        panel_1.add(label);

        JLabel label_1 = new JLabel("00");
        label_1.setBounds(247, 51, 21, 14);
        panel_1.add(label_1);

        JButton btnPlay = new JButton("Insert Letter");
        btnPlay.setBounds(119, 165, 106, 23);
        panel_1.add(btnPlay);

        JLabel lblPlayer_1 = new JLabel("Player 2");
        lblPlayer_1.setFont(new Font("Tw Cen MT", Font.PLAIN, 14));
        lblPlayer_1.setBounds(228, 26, 77, 14);
        panel_1.add(lblPlayer_1);

        JLabel lblPlayersTurn = new JLabel("Player 1's turn");
        lblPlayersTurn.setFont(new Font("Tw Cen MT Condensed Extra Bold", Font.PLAIN, 17));
        lblPlayersTurn.setBounds(125, 95, 147, 29);
        panel_1.add(lblPlayersTurn);

        textField = new JTextField();
        textField.setBounds(129, 134, 86, 20);
        textField.setColumns(10);
        panel_1.add(textField);

        JButton btnQuit = new JButton("New Game");
        btnQuit.setBounds(160, 358, 98, 23);
        frame.add(btnQuit);

        JButton[][] buttons = new JButton[8][8];
        ButtonGroup bg = new ButtonGroup();
        GridBagConstraints gbc_button = new GridBagConstraints();

        for(i=0; i<8; i++){
            for(j=0; j<8; j++){
                buttons[i][j] = new JButton(" ");

                gbc_button.insets = new Insets(0, 0, 5, 5);
                gbc_button.gridx = i;
                gbc_button.gridy = j;
                panel.add(buttons[i][j], gbc_button);

                bg.add(buttons[i][j]);
                buttons[i][j].setActionCommand(i + " " + j);
                buttons[i][j].addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent b) {
                        JOptionPane.showMessageDialog(null, b.getActionCommand());

                    }
                });

            }
        }
    }

}

无论我从何处调用类或单独添加按钮而不是循环,结果都是相同的。

0 个答案:

没有答案