【问题标题】:How to format a multiplication table that is created with JOptionPane with HTML?如何格式化使用 JOptionPane 和 HTML 创建的乘法表?
【发布时间】:2018-09-26 18:56:14
【问题描述】:

我正在尝试将 HTML 集成到 JOptionPane 中,但无法格式化表格。

代码是:

import javax.swing.JOptionPane;

    public class tabell1 {
        public static void main(String[] arg) {
        int p;
        String t="";
        String l="\n";
        for (int r=1; r <=10; r++) {
        for (int k=1; k  <=10; k++) {
            p=r*k;
            System.out.print(p+"");
            t=t+p+"";
        }
        t=t+l;
        System.out.println("");
        }
    JOptionPane.showMessageDialog(null, t);
        }
    }

谁能帮我用HTML格式化一个用JOptionPane创建的乘法表?

【问题讨论】:

  • 将数据的生成与演示分开。不用担心 Swing;先搞清楚乘法表的逻辑。
  • 这与 html 或 joptionpane 有什么关系?
  • 我尝试让表格的框架看起来更好。
  • setTitle("Multiplikationstabell"); JLabel l = new JLabel( ""+ ""+ ""+ ""+ ""+ ""+ "
    2 3 4
    2 3 4
    ", JLabel.CENTER); setDefaultCloseOperation(EXIT_ON_CLOSE);

标签: java swing joptionpane


【解决方案1】:

要在 Swing 组件中正确显示为 HTML,数据必须格式化为 HTML!

import javax.swing.JOptionPane;

public class tabell1 {

    public static void main(String[] arg) {
        int p;
        String t = "<html><body><table>";
        for (int r = 1; r <= 10; r++) {
            t = t + "<tr>";
            for (int k = 1; k <= 10; k++) {
                t = t + "<td>";
                p = r * k;
                System.out.print(p + " ");
                t = t + p + "";
                t = t + "</td>";
            }
            t = t + "</tr>";
            //t = t + l;
            System.out.println("");
        }
        JOptionPane.showMessageDialog(null, t);
    }
}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
  • 2013-04-24
  • 2022-01-21
  • 2022-12-17
  • 2014-12-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多