gpt4 book ai didi

java - 我怎样才能对齐和改进这个乘法表?

转载 作者:行者123 更新时间:2023-12-02 01:53:07 24 4
gpt4 key购买 nike

我被分配创建一个程序,该程序使用文件编写器类写入包含乘法表的文本文件。

当程序执行时,必须显示一个带有表格的 JFrame 窗口。

为此,我编写了三个类:

  • MainTp 运行程序。

  • JFrame,即窗口本身。

  • FileWriteTp 用于写入乘法表。这里我使用了for循环来写表。

一切都运行得很好。除了一件事。当我运行该程序时,我看到以下输出,其中有一些我不喜欢的内容。

输出

enter image description here

如您所见,数字没有很好地对齐,我希望在输出中得到类似的内容:

enter image description here

在这里您可以看到它们非常对齐

经过几个小时的研究,我注意到可以通过使用格式来解决这个问题。问题是我不知道在这种情况下如何使用它。

如果有人能帮助我解决这个问题,我将不胜感激。

此外,我想知道是否可以在这个表格中添加列线、行线和一些颜色,以便它看起来更好以及如何完成。

这是我的源代码

FileWriteTp 类

import java.io.FileWriter;

public class FileWriteTp {

private String pitagora =""; //crea uno "spazio" fra i numeri della tabella

public FileWriteTp() {

try {
FileWriter fw=null;

fw = new FileWriter("tavolapit.txt", false);//con il parametro
//false il file viene aperto in scrittura e comporta la cancellazione
//di un eventuale file preesistente



for(int i=1;i<11; i++) { //creo il primo ciclo per i numeri da 1 a 10


for(int x= 1; x<11; x++) {//creo secondo ciclo per i numeri da 1 a 10
//che vanno a moltiplicare i numeri del primo ciclo

fw.write(i*x +" "); //istruzione che permette l'esecuzione della
//moltiplicazione fra i numeri del primo ciclo(i)e i numeri del
//secondo ciclo(x)
pitagora += i*x + " ";


}
fw.write(System.lineSeparator());//istruzioni che permettono
//l'incolonnamento in verticale e orizzontale

pitagora += "\r\n\n";

}

fw.close(); // chiusura dello stream

}
catch(Exception e) {

e.printStackTrace();

}

}

public String getPitagora() { //metodo che permette il valore di "pitagora"e
//la visualizzazione dell'intera tabella
return String.format(pitagora);
}

}

JFrame 类

import java.awt.*;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

public class Jframe extends JFrame{

public Jframe()
{
super("Tavola Pitagorica"); //assegna il nome alla finestra
setBounds(200, 150, 650, 550);//imposta le misure per la finestra
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
Container c = getContentPane(); //contenitore finestra

FileWriteTp fw = new FileWriteTp(); //oggetto per fileWriteTp


JLabel label1 = new JLabel("Tavola Pitagorica");
label1.setFont(new Font ("Helvetica",Font.BOLD,22));
label1.setHorizontalAlignment(SwingConstants.CENTER);
label1.setBorder(new EmptyBorder (20,0,0,0));
label1.setForeground(Color.BLUE);

JTextArea textArea1 = new JTextArea(fw.getPitagora()); //area di testo
textArea1.setBorder(new EmptyBorder (50,70,0,0));
textArea1.setFont(new Font ("Helvetica",Font.PLAIN,16));

c.add(textArea1, BorderLayout.CENTER); //aggiunge l'area di testo
c.add(label1,BorderLayout.NORTH);
c.setBackground(Color.white);

//all'interno del contenitore impostando il layout al centro

主类

public class MainTp {
public static void main(String[] args) throws Exception {

Jframe finestra = new Jframe();
finestra.setVisible(true);//permette la visibilità della finestra

}

}

非常感谢

最佳答案

您“可能”实现这一目标的几种方法。最简单的解决方案是使用 String.format 和固定宽度字体(如 camickr 建议)

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.util.StringJoiner;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Test {

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

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
setLayout(new BorderLayout());
JTextArea ta = new JTextArea();
ta.setFont(new Font("Monospaced", Font.PLAIN, 12));
ta.setText(buildTable());
ta.setEditable(false);
add(ta);
}

protected String buildTable() {
StringJoiner sj = new StringJoiner(System.lineSeparator());
for (int i = 1; i < 11; i++) {
StringBuilder sbRow = new StringBuilder(128);
for (int x = 1; x < 11; x++) {
int value = i * x;
sbRow.append(String.format("%-8d", value));
}
sj.add(sbRow.toString());
}
return sj.toString();
}

}

}

现在,当然,如果您确实不想使用固定宽度字体,您可以求助于使用基于 html 的表格。

Html based table

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.util.StringJoiner;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;

public class Test {

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

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
setLayout(new BorderLayout());
JTextPane tp = new JTextPane();
tp.setContentType("text/html");
tp.setText(buildTable());
add(tp);
}

protected String buildTable() {
StringJoiner sj = new StringJoiner(System.lineSeparator(), "<html><body><table>", "</table></body></html>");
for (int i = 1; i < 11; i++) {
StringBuilder sbRow = new StringBuilder(128);
sbRow.append("<tr>");
for (int x = 1; x < 11; x++) {
int value = i * x;
sbRow.append("<td>").append(value).append("</td>");
}
sbRow.append("</tr>");
sj.add(sbRow.toString());
}
return sj.toString();
}

}

}

关于java - 我怎样才能对齐和改进这个乘法表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52692440/

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