blustone
blustone

Reputation: 345

Not all buttons showing up in Java?

When I run my program using eclipse only one button shows up (top left corner) but when I use javac in terminal (most of the time) all buttons show up! It's really bugging me. Can anyone help? Thanks!
This is my constructor:

    public TicTacToe(){
    super("Farm Tic-Tac-Toe");
    setSize(450,750);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container cont = getContentPane();
    cont.setLayout(null);
    int newLine = 0;
    int lineCount = 0;
    for(int i = 0; i < buttons.length; i++){
        buttons[ i] = new JButton(blank);
        if(i == 3 || i == 6){
            newLine++;
            lineCount = 0;
        }
        buttons[ i].setBounds(lineCount*150,newLine*150,150,150);
        cont.add(buttons[ i]);
        buttons[ i].addActionListener(this);
        lineCount++;
    }
}

and here's the action listener...

    public void actionPerformed(ActionEvent e){
    for(int i = 0; i < buttons.length; i++){
        if(e.getSource()==buttons[ i]){
            if(turn%2==0){
                buttons[ i].setName("x");
                buttons[ i].setIcon(x);
                buttons[ i].removeActionListener(this);
            }
            else{
                buttons[ i].setName("o");
                buttons[ i].setIcon(o);
            }
            buttons[ i].removeActionListener(this);
        }
    }
    turn++;
    checkWin();
}



Please don't tell me too much about how my code design is bad because I am (not a beginner, but) not too good at Java.

Upvotes: 0

Views: 1194

Answers (3)

MadProgrammer
MadProgrammer

Reputation: 347184

The solution to the problem is really very simple...

The first one is the lack of a layout manager and the other is the order in which you are displaying your UI (as has already being stated)

enter image description here

public class SimpleTicTacToe {

    public static void main(String[] args) {
        new SimpleTicTacToe();
    }

    public SimpleTicTacToe() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new GamePane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class GamePane extends JPanel {

        public GamePane() {
            setLayout(new GridLayout(3, 3));
            for (int index = 0; index < 9; index++) {
                add(new JButton());
            }
        }

    }

}

Take the time to read through Creating a GUI With JFC/Swing to gain a grasp of the basics.

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You're calling setVisible(true) before adding all of your components to the GUI, so it is not showing them all. Don't do this. Instead call setVisible(true) after all components have been added.

Also

  • As has been suggested by many, don't use null layout, but instead learn about and use the layout managers.
  • Yeah, get another book.

Upvotes: 4

durron597
durron597

Reputation: 32323

The Eclipse GUI only renders buttons that are drawn in certain very specific ways. If your code does it differently (for example, with a Loop), Eclipse won't be able to draw it.

Also, use a LayoutManager, don't do things like .setLayout(null)

Upvotes: 1

Related Questions