Reputation: 909
Just wondering why the last JTextArea isn't being added/showing up. I'm getting this:
http://gyazo.com/19f571409541d3a5d8b53cf816e82b1a
from this code:
public static void initGUI() {
JFrame frame = new JFrame();
frame.setTitle("Sudoku");
frame.setLocation(500, 0);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
//Initiate faces
for( int j = 0; j < 9; j++) {
for( int i = 0; i < 9; i++) {
field[i][j] = new JTextArea();
field[i][j].setFont(font);
field[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
field[i][j].setVisible(true);
field[i][j].setBounds(i*100, j*100, 100, 100);
field[i][j].setText(i+" "+j);
frame.add(field[i][j]);
}
}
frame.setSize(1000,1000);
frame.setVisible(true);
}
Any help? Thanks in advance
Upvotes: 1
Views: 137
Reputation: 347334
The default layout manager for a JFrame
is BorderLayout
. BorderLayout
only allows a single component to occupy any of the available spaces it managers.
Instead, try changing the layout manager to something like GridLayout
Something like frame.setLayout(new GridLayout(9, 9));
for example might help
You should not be using setBounds
as it will have no effect on components under the control of layout managers. Instead, you should use the cols and rows constructor of JTextArea
to provide details about how you want the text area sized to
Update with GridLayout
example
Each JTextArea
will be give equal amount of the available space, so as you resize the window, the fields will also change size...
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestLayout101 {
public static void main(String[] args) {
new TestLayout101();
}
public TestLayout101() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame();
frame.setTitle("Sudoku");
frame.setLocation(500, 0);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(9, 9));
JTextArea[][] field = new JTextArea[9][9];
//Initiate faces
for (int j = 0; j < 9; j++) {
for (int i = 0; i < 9; i++) {
field[i][j] = new JTextArea(1, 3);
field[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
field[i][j].setText(i + " " + j);
frame.add(field[i][j]);
}
}
frame.pack();
frame.setVisible(true);
}
});
}
}
Updated with GridBagLayout
example
This will provide each JTextArea
with it's preferred size, if enough space is available.
This will mean as you resize the window, the fields won't change in size, unless there isn't enough space available to honour the preferred or minimum size...
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestLayout101 {
public static void main(String[] args) {
new TestLayout101();
}
public TestLayout101() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame();
frame.setTitle("Sudoku");
frame.setLocation(500, 0);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
JTextArea[][] field = new JTextArea[9][9];
//Initiate faces
GridBagConstraints gbc = new GridBagConstraints();
for (int j = 0; j < 9; j++) {
gbc.gridy = j;
for (int i = 0; i < 9; i++) {
gbc.gridx = i;
field[i][j] = new JTextArea(1, 3);
field[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
field[i][j].setText(i + " " + j);
frame.add(field[i][j], gbc);
}
}
frame.pack();
frame.setVisible(true);
}
});
}
}
Upvotes: 3
Reputation: 4728
import java.awt.*;
import javax.swing.*;
public class Jessica {
static JTextArea[][] field = new JTextArea [10][10];
public static void main(String[] args) {
initGUI();
}
public static void initGUI() {
JFrame frame = new JFrame();
frame.setTitle("Sudoku");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout(0, 9));
frame.getContentPane().setPreferredSize(new Dimension (500,500));
//Initiate faces
for( int j = 0; j < 9; j++) {
for( int i = 0; i < 9; i++) {
field[i][j] = new JTextArea();
field[i][j].setFont(new Font("ARIAL", Font.BOLD, 25));
field[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
field[i][j].setVisible(true);
field[i][j].setText(i+" "+j);
frame.getContentPane().add(field[i][j]);
}
}
frame.pack();
frame.setVisible(true);
}
}
Use grid layout with number of rows 0, as shown. In grid layout you just specify container pane preferred size, and it splits in equal parts (dont sure if it takes component sizes in account). Put items on content pane, and set its layout manager. http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html, check the example on the page, cause it's not well explained in tutorial.
Upvotes: 0