gpt4 book ai didi

java - 在 JOptionPane 中打印二维数组

转载 作者:行者123 更新时间:2023-12-02 11:55:04 27 4
gpt4 key购买 nike

我尝试在 JOptionPane 中打印随机一维和二维数组。但结果是这样的:

enter image description here

有没有办法将其格式化为这样排列:

enter image description here

行和列也是随机的。

另一件事,有没有办法只使用一个函数来打印一维和二维数组?如果不是,2d 函数可以使用第一个函数吗?这是学校作业。我们不可以乱搞 JOptionPane。它应该保持这样: JOptionPane.showMessageDialog(null, String);

 public static String printMatrix(int[] m) {
String result = "";
for (int i = 0; i < m.length; i++) {
result += m[i] + " ";
}
return result;
}


public static String printMatrix2D(int[][] m) {
String result = "";
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[i].length; j++) {
result += m[i][j] + " ";
}
result += "\n";
}
return result;
}

最佳答案

如果您希望将数字放置在表格中,则需要使用 GridLayout 创建一个 JPanel,其中包含一些 hgapvgap 。这适用于 4 位和 5 位数字或任意数量的数字。

例如:

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JOptionPanePrintingInColumns {

private JPanel pane;
private String[] oneDArray = new String[] {"1000", "10000", "99999", "23000", "100000"};
private String[][] twoDArray = new String[][] {
{"1000", "10000", "99999", "23000", "100000"},
{"1000", "10000", "99999", "23000", "100000"},
{"1000", "10000", "99999", "23000", "100000"},
{"1000", "10000", "99999", "23000", "100000"},
{"1000", "10000", "99999", "23000", "100000"}
};

public static void main(String[] args) {
SwingUtilities.invokeLater(new JOptionPanePrintingInColumns()::createAndShowGui);
}

private void createAndShowGui() {
//For the 1D Array
pane = new JPanel();
pane.setLayout(new GridLayout(0, 5, 50, 10));
for (int i = 0; i < oneDArray.length; i++) {
JLabel label = new JLabel(oneDArray[i]);
label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
pane.add(label);
}
JOptionPane.showMessageDialog(new JFrame(), pane);

//For the 2D array
pane = new JPanel();
pane.setLayout(new GridLayout(0, 5, 50, 10));
for (int i = 0; i < twoDArray.length; i++) {
for (int j = 0; j < twoDArray[i].length; j++) {
JLabel label = new JLabel(twoDArray[i][j]);
label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
pane.add(label);
}
}
JOptionPane.showMessageDialog(new JFrame(), pane);
}
}

这会产生这样的结果:

enter image description here enter image description here

因为我们可以看到边框,所以我们可以注意到每个标签的大小与最大的标签(即每行的最后一个)的大小相同,这就是它适用于更大或更短数字的原因。

您可能已经注意到我使用了这一行:

pane.setLayout(new GridLayout(0, 5, 50, 10));

但是0是什么意思呢?这意味着“根据需要创建尽可能多的行,每行 5 列”,因此这适用于 1D 或 2D 数组,只需更改从每个数组获取数据的方式即可。

<小时/>

编辑:

but i forgot to mention this is for school homework. we are not allowed to mess with JOptionPane. it's supposed to stay like this: JOptionPane.showMessageDialog(null, String);

您可以在字符串内使用 HTML 标签将其转换为表格,然后将每个数字放入其单元格内,使用一些 cellspacing 来提供更多或更少的内容空间(玩它):

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JOptionPanePrintingInColumns {

private JPanel pane;
private String[] oneDArray = new String[] {"1000", "10000", "99999", "23000", "100000"};
private String[][] twoDArray = new String[][] {
{"1000", "10000", "99999", "23000", "100000"},
{"100", "180000", "9999", "3000", "1090000"},
{"111000", "1220000", "9955999", "230100", "1000200"},
{"10010", "1005400", "9999954", "9", "123"},
{"100430", "1000054", "999", "23123000", "123456789"}
};

public static void main(String[] args) {
SwingUtilities.invokeLater(new JOptionPanePrintingInColumns()::createAndShowGui);
}

private void createAndShowGui() {
String output;
StringBuilder sb = new StringBuilder();
sb.append("<html><table><tr>");
for (int i = 0; i < oneDArray.length; i++) {
sb.append("<td>").append(oneDArray[i]).append("</td>");
}
sb.append("</tr></table></html>");
output = sb.toString();
JOptionPane.showMessageDialog(new JFrame(), output);

sb = new StringBuilder();
sb.append("<html><table cellspacing=10>");
for (int i = 0; i < twoDArray.length; i++) {
sb.append("<tr>");
for (int j = 0; j < twoDArray.length; j++) {
sb.append("<td>").append(twoDArray[i][j]).append("</td>");
}
}
sb.append("</tr></table></html>");
output = sb.toString();
JOptionPane.showMessageDialog(new JFrame(), output);
}
}

enter image description here

关于java - 在 JOptionPane 中打印二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47658405/

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