Reputation: 513
So here is my basic template for Tic Tac Toe:
package myProjects;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.event.*;
public class SecondTickTacToe extends JFrame{
public JPanel mainPanel;
public static JPanel[][] panel = new JPanel[3][3];
public static void main(String[] args) {
new SecondTickTacToe();
}
public SecondTickTacToe(){
this.setSize(310, 400);
this.setTitle("Tic Tac Toe");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
mainPanel = new JPanel();
for(int row=0; row<3; row++){
for(int column=0; column<3; column++){
int top=0;
int bottom=0;
int left=0;
int right=0;
panel[row][column] = new JPanel();
panel[row][column].addMouseListener(new Mouse());
panel[row][column].setPreferredSize(new Dimension(90, 90));
panel[row][column].setBackground(Color.GREEN);
if(column==0||column==1)
right = 5;
if(column==1||column==2)
left = 5;
if(row==0||row==1)
bottom = 5;
if(row==1||row==2)
top = 5;
panel[row][column].setBorder(BorderFactory.createMatteBorder(top, left, bottom, right, Color.BLACK));
addItem(panel[row][column], row, column);
}
}
this.add(mainPanel);
this.setVisible(true);
}
private void addItem(JComponent c, int x, int y){
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.weightx = 100.0;
gbc.weighty = 100.0;
gbc.fill = GridBagConstraints.NONE;
mainPanel.add(c, gbc);
}
}
class Mouse extends MouseAdapter{
public void mousePressed(MouseEvent e){
if(e.getSource() instanceof JPanel)
((JPanel)e.getSource()).setBackground(Color.BLUE);
}
}
(You'll have to run the program to see what I'm talking about)
So here is my question: Is there a way that I can fill in the space between the borders of the JPanels? I want it to be solid. Making the panels bigger doesn't seem to work, and making the border size bigger doesn't seem to do anything either. Does anyone know how to make this work? (My end goal is to make the borders look like a solid tic tac toe template)
Upvotes: 0
Views: 533
Reputation: 324157
mainPanel = new JPanel();
The default layout for a JPanel
is a FlowLayout
. By default a FlowLayout
leaves a 5 pixel gap between all components. If you don't want this gap then you need to change the layout. Read the FlowLayout
API and you will find the constructor that allows you to specify a 0 gap.
However, using a FlowLayout
may not be the best layout. A GridLayout
is the easier layout to use for row and columns. I suggest you read the section from the Swing tutorial on How to Use Grid Layout for more information and examples. There will be no need for your addItem(...)
method. When you create the GridLayout
with the proper row/columns the components will automatically wrap to a new row so you just add the component to the panel.
mainPanel.add(c, gbc);
By the way, specifying a GridBagConstraint
does nothing unless the panel is actually using a GridBagLayout
, which is not.
Upvotes: 2