1

So for a course project, I'm building a word spelling game. The basic UI should look like this:

But after running the program it primarily takes this form:

Only after I resize or minimize and restore it does it change to the expected form.

Here's the source code:

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

                    }
                });

            }
        }
    }

}

The result is the same regardless of where I call the class from or adding the buttons separately instead of in a loop.

  • please, explain little bit more clearly , i am unable to understand what is actually problem... parden me!! – Vishal Gajera Mar 29 '16 at 08:59
  • 4
    Don't you think it would make _much more_ sense to call `frame.setVisible(true);` _after_ you added every component to the frame? – Tom Mar 29 '16 at 09:01
  • 1
    make the lines `frame.getContentPane().add(panel);` and `frame.setVisible(true);` the last of method/constructor – Sergiy Medvynskyy Mar 29 '16 at 09:01
  • The second photo is what initially appears after I run this program. But it is not the full form. Then if I resize the window, the expected form (image 2) appears. Why doesn't the complete form appear in the first place? – Fatiul Huq Sujoy Mar 29 '16 at 09:03
  • Possible duplicate of [button is not added to the jframe](http://stackoverflow.com/questions/33166664/button-is-not-added-to-the-jframe) – Tom Mar 29 '16 at 09:04
  • Thanks Tom and Sergiy, it's resolved – Fatiul Huq Sujoy Mar 29 '16 at 09:06
  • 1
    1) `frame.getContentPane().setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) `frame.setBounds(230, 70, 900, 451);` should be `pack()` (& directly before `setVisible(true)` .. – Andrew Thompson Mar 29 '16 at 09:22

0 Answers0