如何在 JLabel 中打印堆栈?

达菲利布劳

我有一个打印堆栈的方法,但是,它通过 System.out.println 打印,我需要将它打印到 JLabel,以便它在我的 GUI 中可见。

我正在使用 Netbeans 的 UI 设计器,因此我有一个 JFrame 文件、一个主类文件和一个堆栈文件,我在其中编写了 showStack 方法。“Pila”是一个扩展 Stack 的类。

这是方法

public void showStack(Pila<Integer> s){  
        if (s.isEmpty())  
            return;  

        Integer x = s.peek();  
        s.pop();  
        showStack(s);  

        System.out.print(x + " ");  
        s.push(x);  
    }  

我想摆脱 System.out.print 并将其替换为我可以用来在 Jlabel 中打印堆栈的东西

P_Luizon

您可以创建一个返回字符串的函数,并将该字符串放在标签的文本上。我认为它可以使用与控制台打印相同的代码,但连接在字符串中而不是打印

String output; // this one is to concatenate your String
public String returnStack(Pila<Integer> s){
    if (s.isEmpty())  {
        output = "";
        return output;
    }

    Integer x = s.peek();  
    s.pop();  
    showStack(s);  

    output+= x + " ";  
    s.push(x);
    return output;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章