javagui-JFrame的问题

javagui-JFrame的问题,java,swing,user-interface,jtextarea,Java,Swing,User Interface,Jtextarea,只是想知道为什么最后一个JTextArea没有被添加/显示。我明白了: 根据该代码: public static void initGUI() { JFrame frame = new JFrame(); frame.setTitle("Sudoku"); frame.setLocation(500, 0); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); //Initiate faces for

只是想知道为什么最后一个JTextArea没有被添加/显示。我明白了:

根据该代码:

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);
}
publicstaticvoidinitgui(){
JFrame=新JFrame();
frame.setTitle(“数独”);
帧设置位置(500,0);
frame.setDefaultCloseOperation(关闭时退出);
//初始化面孔
对于(int j=0;j<9;j++){
对于(int i=0;i<9;i++){
字段[i][j]=新的JTextArea();
字段[i][j].setFont(font);
字段[i][j].setboorder(BorderFactory.createLineBorder(Color.BLACK));
字段[i][j].setVisible(true);
字段[i][j]。立根(i*100,j*100100100);
字段[i][j].setText(i+“”+j);
框架.添加(字段[i][j]);
}
}
框架设置尺寸(10001000);
frame.setVisible(true);
}
有什么帮助吗?
提前感谢
JFrame
的默认布局管理器是
BorderLayout
<代码>边框布局仅允许单个组件占用其管理的任何可用空间

相反,尝试将布局管理器更改为类似于
GridLayout

类似于
frame.setLayout(新的GridLayout(9,9))可能会有所帮助

您不应该使用
setBounds
,因为它不会对布局管理器控制下的组件产生任何影响。相反,您应该使用
JTextArea
的cols和rows构造函数来提供有关如何调整文本区域大小的详细信息

使用
网格布局更新
示例

每个
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);
            }
        });
    }

}
import java.awt.*;
导入javax.swing.*;
公共课杰西卡{
静态JTextArea[][]字段=新的JTextArea[10][10];
公共静态void main(字符串[]args){
initGUI();
}
公共静态void initGUI(){
JFrame=新JFrame();
frame.setTitle(“数独”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(新的GridLayout(0,9));
frame.getContentPane().setPreferredSize(新维度(500500));
//初始化面孔
对于(int j=0;j<9;j++){
对于(int i=0;i<9;i++){
字段[i][j]=新的JTextArea();
字段[i][j].setFont(新字体(“ARIAL”,Font.BOLD,25));
字段[i][j].setboorder(BorderFactory.createLineBorder(Color.BLACK));
字段[i][j].setVisible(true);
字段[i][j].setText(i+“”+j);
frame.getContentPane().add(字段[i][j]);
}
}
frame.pack();
frame.setVisible(true);
}
}
使用行数为0的栅格布局,如图所示。
在网格布局中,您只需指定容器窗格的首选大小,并将其分成相等的部分(不确定是否考虑组件大小)。将项目放在内容窗格上,并设置其布局管理器,检查页面上的示例,因为它在教程中没有很好的解释。

您想要
j在右下角应该有一个最后的JTextArea,上面写着“8”。不知道为什么它不在那里我使用GridLayout,但不喜欢不能自由添加其他组件。原来块8出现在块0的下面。有什么原因吗?试着改用GridBagLayout之类的东西then@FraserPrice我使用
GridLayout
尝试了您的测试代码,似乎对我来说效果不错。尽量避免使用
setBounds
你应该Hmmmmm,我已经阅读了链接,没有看到为什么不使用此方法的解释。您认为我应该在组件中使用getPreferredSize,还是根本不使用它。如何让控制面板占据我想要的空间?首选过程是覆盖
getPreferred
(和/或根据需要覆盖最小/最大值)。使用
setPreferredSize
只会把事情弄得一团糟。我的前任用文本字段做了这件事,当我的老板坚持要我们改变字体时,文本字段在我脸上爆炸了。啊哈,啊哈,把setXXX改成getXXX。我同意。
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);
            }
        });
    }
}
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);
}
}