JFrame never closes

Net

Golden Member
Aug 30, 2003
1,592
3
81
I'm working on a chess game. The problem is I can never close it.

Code:
package gui;

import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;

public class Board {
	
	private JLabel[][] squares;
	private JPanel board;

	public static void main(String[] args) {  // temp for testing
		
		JFrame frame = new JFrame();
		Board board = new Board();
		
	    frame.addWindowListener(new WindowAdapter() {
	        public void windowClosing(WindowEvent e) {System.exit(0);}
	    });

		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JLabel emptyLabel = new JLabel("");
		frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

		frame.getContentPane().add(board.getBoard());
		
		frame.pack();
		frame.setVisible(true);
		
	}
	
	public Board() {
		board = new JPanel(new GridLayout(8,8));
		squares = new JLabel[8][8]; // store a record for easily updating squares in future
		
		//Create squares and place them on board
		for (int row = 0; row < 8; row++) {
			for (int col = 0; col < 8; col++) {
				JLabel newSquare = new JLabel();
				newSquare.setPreferredSize(new Dimension(50,50));
				newSquare.setOpaque(true);
				
				if (row &#37; 2 == 0) {
					if (col % 2 == 0)
						newSquare.setBackground(Color.yellow);
					else
						newSquare.setBackground(Color.green);
				} else {
					if (col % 2 == 0)
						newSquare.setBackground(Color.green);
					else
						newSquare.setBackground(Color.yellow);
				}
				
				board.add(newSquare);
				squares[row][col] = newSquare;  
			}
		}
		
	}
	
	public JPanel getBoard() {
		return board;
	}
	
}
 

Sureshot324

Diamond Member
Feb 4, 2003
3,370
0
71
I'm not really sure, everything seems fine. I assume you put the following lines in to try to make it close, but if not, try commenting them out. By default the JVM will exit when the last window is closed, so there's no need to put a System.exit in the close operation. EXIT_ON_CLOSE is also the default close operation so you don't need to specify it.

frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


Have you tried commenting out the line where you add the board to see what happens?
 

Net

Golden Member
Aug 30, 2003
1,592
3
81
ran it on a windows box and its fine. For some reason the problem persists in linux.
 

Sureshot324

Diamond Member
Feb 4, 2003
3,370
0
71
Is your linux box running Sun Java or the Open source Java? The open source one is buggy and slow, but unfortunately is the default on many distributions such as Ubuntu.