gpt4 book ai didi

java - JPanel GridLayout(由循环部分填充)添加新值以结束,无论它们添加的顺序如何

转载 作者:行者123 更新时间:2023-11-30 06:12:43 26 4
gpt4 key购买 nike

因此,我正在使用 Swing 和 GUI,设计一个程序,其中我有一个包含 10x10 自定义 JButton 网格的 JPanel。我有一个 1-10 数字的水平索引,以及一个 A-J 字母的垂直索引(两个 JLabel)。

我使用了一个 BorderLayout,其垂直索引指向 WEST,一个 11x10 GridLayout 嵌套在 CENTER(为什么?因为这是我能想到的最简单的方法来完善水平索引号在其相应列上方的位置按钮)。

我在 110 个空格的网格中的前 10 个空格填充了数字来索引列 - 这工作正常。

然后,我使用 11x1 网格将垂直索引上的字母等距地间隔到西边,以说明网格顶部水平索引的额外插槽(见下图)。

我的问题是:每当我去填充垂直网格顶部的空白空间时,它都会将新的 JLabel 放在底部,无论它是放在填充循环之前还是之后。

结果如下:

enter image description here

这里是我用来填充循环的代码,在填充循环之前和之后添加了一个 JLabel,以显示它是如何放置在底部的:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test {

private static void createAndDisplayGUI() {

// This panel contains the grid layout for the buttons
JPanel center = new JPanel();
center.setLayout(new BorderLayout());

JPanel gridIndexLeft = new JPanel();
gridIndexLeft.setLayout(new GridLayout(12,1));
gridIndexLeft.add(new JLabel(" " + 0)); // Added a 0 here before fill loop to show where space should be
for (int i=0; i<10; i++) {
gridIndexLeft.add(new JLabel(" " + Character.toString((char)('J' - i))), SwingConstants.CENTER);
}
gridIndexLeft.add(new JLabel(" " + 0)); // And again here with same result

JPanel buttons = new JPanel();
buttons.setLayout(new GridLayout(11,10));
for (int i=0; i<10; i++) {
buttons.add(new JLabel(" " + (10-i)), SwingConstants.CENTER);
}
for (int i=0; i<100; i++) {
buttons.add(new BattleshipButton());
}

center.add(gridIndexLeft, BorderLayout.WEST, SwingConstants.CENTER);
center.add(buttons, BorderLayout.CENTER, SwingConstants.CENTER);

JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(center, BorderLayout.CENTER);

JFrame display = new JFrame();
display.setContentPane(content);
display.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
display.setBackground(Color.GRAY);
display.setResizable(false);
display.setSize(300,325);
display.setLocation(500,190);
display.setVisible(true);
}

public static void main(String[] args) {

try {
UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
} catch (Exception e) {
e.printStackTrace();
}

SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndDisplayGUI();
}
});
}
}

我知道间距应为 11x1 以匹配,但我将其设为 12x1 以显示在循环前后添加新的 JLabel 时会发生什么。

任何输入将不胜感激!

最佳答案

你的问题出现在这里...

gridIndexLeft.add(new JLabel(" " + Character.toString((char) ('J' - i))), SwingConstants.CENTER);

SwingConstants.CENTER == 0,它被中断为“在位置 0 处添加此组件”,这意味着您添加的任何内容在它真正被按下之前,为什么你在使用 GridLayout 时尝试使用它,我不确定(也许你想改变标签的水平/垂直位置?)

所以,当我将您的代码更新为...

JPanel gridIndexLeft = new JPanel();
gridIndexLeft.setLayout(new GridLayout(12, 1));
gridIndexLeft.add(new JLabel("-" + 0)); // Added a 0 here before fill loop to show where space should be
for (int i = 0; i < 10; i++) {
gridIndexLeft.add(new JLabel(" " + Character.toString((char) ('J' - i))));
}
gridIndexLeft.add(new JLabel("+" + 0)); // And again here with same result

它产生了...

GridLayout

可以看到-0在前,+0在后

现在,如果您真的有兴趣,可以使用一个容器和一个 GridLayout

GridLayout

import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class GridTest {

public static void main(String[] args) {
new GridTest();
}

public GridTest() {
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("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
setLayout(new GridLayout(11, 11));

for (int row = 0; row < 11; row++) {
if (row > 0) {
add(new JLabel(Character.toString((char) (('A' + row) - 1))));
} else {
add(new JLabel(" "));
}
for (int col = 0; col < 10; col++) {
if (row == 0) {
add(new JLabel(Integer.toString(col + 1)));
} else {
add(new JButton(" "));
}
}
}
}

}

}

是的,您可以更改 JLabel

的水平和垂直属性

关于java - JPanel GridLayout(由循环部分填充)添加新值以结束,无论它们添加的顺序如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32598647/

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