gpt4 book ai didi

Java GUI - JFrame 问题

转载 作者:行者123 更新时间:2023-11-29 05:33:35 26 4
gpt4 key购买 nike

只是想知道为什么最后一个 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);
}

有什么帮助吗?提前致谢

最佳答案

JFrame 的默认布局管理器是 BorderLayoutBorderLayout 只允许单个组件占用它管理的任何可用空间。

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

例如 frame.setLayout(new GridLayout(9, 9)); 可能会有帮助

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

使用 GridLayout 示例更新

GridLayout example

每个 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 提供其首选大小。

enter image description here

这意味着当您调整窗口大小时,字段的大小不会改变,除非没有足够的可用空间来满足首选或最小大小...

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

关于Java GUI - JFrame 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20275110/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com