Reputation: 430
I'm trying to create a Sudoku app in Eclipse using WindowBuilder. I decided to use JLabels as cells. I have a class Cell that extends JLabel and adds 3 fields - value, x and y. The Cells are places in 9 JPanels with GridLayout.
The problem is that when I add Cells into the JPanels, they look like they are stacked on top of each other, as if the GridLayout is not working. When I do the same with JLabels instead, they look fine.
With Cells:
With JLabels:
This is the Cell class:
import javax.swing.JLabel;
public class Cell extends JLabel {
private static final long serialVersionUID = 1L;
private int value;
private int x;
private int y;
public Cell(int value, int x, int y) {
super(Integer.toString(value));
this.value = value;
this.x = x;
this.y = y;
}
public Cell(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
Below is the MainFrame class. I'm changing two lines in the initialize() method. It works fine with
JLabel cell = new JLabel(Integer.toString(j));
but not with
JLabel cell = new Cell(j, j, i);
The full MainFrame class:
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
public class MainFrame {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame window = new MainFrame();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainFrame() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setResizable(false);
frame.setBounds(100, 100, 458, 532);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout(1, 0, 0, 0));
JPanel mainPanel = new JPanel();
mainPanel.setForeground(Color.WHITE);
mainPanel.setBorder(new LineBorder(new Color(0, 0, 0), 2));
mainPanel.setBounds(10, 61, 422, 422);
frame.getContentPane().add(mainPanel);
mainPanel.setLayout(new GridLayout(3, 0, 0, 0));
JPanel[] bigSquares = new JPanel[9];
for (int i=0; i < 9; i++) {
bigSquares[i] = new JPanel();
mainPanel.add(bigSquares[i]);
bigSquares[i].setBorder(new LineBorder(new Color(0, 0, 0), 2));
bigSquares[i].setLayout(new GridLayout(3, 0, 0, 0));
for (int j=0; j < 9; j++) {
JLabel cell = new Cell(j, j, i);
//JLabel cell = new JLabel();
cell.setBorder(new LineBorder(new Color(0, 0, 0), 1));
cell.setVerticalAlignment(SwingConstants.CENTER);
cell.setHorizontalAlignment(SwingConstants.CENTER);
cell.setFont(new Font("Tahoma", Font.PLAIN, 14));
bigSquares[i].add(cell);
}
}
}
}
Upvotes: 1
Views: 112
Reputation: 21435
The problem is that JLabel
already has getX()
and getY()
methods (actually, the methods are from JComponent
. See the JavaDoc for getX().
The orignal methods
Returns the current x/y coordinate of the component's origin
By overwriting getX
and getY
you are actually defining components coordinates relative to it's parent container.
One solution would be to rename your methods to getCellX
, getCellY
.
Another solution would be to work with a separate class Cell
that has a value, coordinates x, y and a JLabel for displaying the value.
Upvotes: 1