Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The default layout manager for a <code>JFrame</code> is <code>BorderLayout</code>. <code>BorderLayout</code> only allows a single component to occupy any of the available spaces it managers.</p> <p>Instead, try changing the layout manager to something like <code>GridLayout</code></p> <p>Something like <code>frame.setLayout(new GridLayout(9, 9));</code> for example might help</p> <p>You should not be using <code>setBounds</code> as it will have no effect on components under the control of layout managers. Instead, you should use the cols and rows constructor of <code>JTextArea</code> to provide details about how you want the text area sized to</p> <p><strong>Update with <code>GridLayout</code> example</strong></p> <p><img src="https://i.stack.imgur.com/4uR9y.png" alt="GridLayout example"></p> <p>Each <code>JTextArea</code> will be give equal amount of the available space, so as you resize the window, the fields will also change size...</p> <pre><code>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 &lt; 9; j++) { for (int i = 0; i &lt; 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); } }); } } </code></pre> <p><strong>Updated with <code>GridBagLayout</code> example</strong></p> <p>This will provide each <code>JTextArea</code> with it's preferred size, if enough space is available.</p> <p><img src="https://i.stack.imgur.com/xqmZO.png" alt="enter image description here"></p> <p>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...</p> <pre><code>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 &lt; 9; j++) { gbc.gridy = j; for (int i = 0; i &lt; 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); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload