只是想知道为什么没有添加/显示最后一个JTextArea。我明白了:
http://gyazo.com/19f571409541d3a5d8b53cf816e82b1a
来自此代码:
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);
}
有任何帮助吗? 提前致谢
答案 0 :(得分:3)
JFrame
的默认布局管理器为BorderLayout
。 BorderLayout
只允许单个组件占用它管理的任何可用空间。
相反,请尝试将布局管理器更改为GridLayout
例如frame.setLayout(new GridLayout(9, 9));
之类的内容可能有帮助
您不应该使用setBounds
,因为它对布局管理器控制下的组件没有影响。相反,您应该使用JTextArea
的cols和rows构造函数来提供有关如何将文本区域大小调整为</ p>的详细信息。
使用GridLayout
示例
每个JTextArea
将获得相等数量的可用空间,因此当您调整窗口大小时,字段也会改变大小......
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);
}
});
}
}
已更新为GridBagLayout
示例
如果有足够的空间,这将为每个JTextArea
提供首选大小。
这意味着当你调整窗口大小时,字段的大小不会改变,除非没有足够的空间来支持首选或最小尺寸...
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);
}
});
}
}
答案 1 :(得分:0)
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);
}
}
使用行数为0的网格布局,如图所示。 在网格布局中,您只需指定容器窗格首选大小,并将其拆分为相等的部分(不确定是否需要考虑组件大小)。将项目放在内容窗格上,并设置其布局管理器。 http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html,请检查页面上的示例,因为它在教程中没有得到很好的解释。