【问题标题】:JTextArea OutputJTextArea 输出
【发布时间】:2018-01-04 23:48:45
【问题描述】:

晚上好,我该怎么做才能让 jTExtArea 的输出像控制台一样? 你可以看到图像是怎样的,我使用 .append 在 jTextArea Image987654321@ 中显示输出

代码:

 public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b1) {

            for (int i = 0; i < a.pattern_cnt; i++) {
                for (int j = 0; j < a.T_element_cnt; j++) {
                 System.out.printf("%.3f\t", a.O[i][j]); //console output

                 t1.append(a.O[i][j]+" ");//jtextarea output
                }

                System.out.println();


            }

【问题讨论】:

    标签: java swing user-interface


    【解决方案1】:

    将字体设置为等宽。

    t1.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
    

    编辑:

    并在主迭代中添加新行并使用字符串格式。

            for (int i = 0; i < a.pattern_cnt; i++) {
                for (int j = 0; j < a.T_element_cnt; j++) {
                 System.out.printf("%.3f\t", a.O[i][j]); //console output
    
                 t1.append(String.format("%.3f\t", a.O[i][j]));//jtextarea output
                }
    
                System.out.println();
                t1.append("\n");
    
            }
    

    【讨论】:

    • 其实不是:(
    • 结果如何?
    • 同理,只是略有不同
    • 并使用String.format("%.3f\t", a.O[i][j]) 调用JTextArea,就像在System.out.printf(...) 中一样。
    • @JohnCoffey 将它放在 JTextArea.append(...) 方法中,而不是当前存在的方法中。毕竟System.out.printf(...) 也只是System.out.print(String.format(...)) 的捷径。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 2015-09-12
    • 2021-09-09
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多