1

一時文字列に書き込む簡単な方法を使用し、後でブレークラインを作成しないため、JLabel に入れます。そして、何も表示しないボタンを表示する際に問題が発生しました。

public String printBestillingsordre(){
        JButton button = new JButton("Varemodtaget");

        for(HentbestillingsordreRegistrer hentboregistrer: hbor){
        if(status == "Leveret"){    

        temp += "Ordre Status: " + hentboregistrer.BestillingsStatus+" \n " +
        "LeverandoerID: "+ hentboregistrer.LeverandoerID+" \n ";


        }else{
            temp += hentboregistrer.BestillingsStatus+ button + "\n" + 
                    "LeverandoerID: "+ hentboregistrer.LeverandoerID+"\n";
        }
        System.out.println(temp);

    }
        return temp;
}

私がそれを呼ぶところ、それはこのように見えます

hboh.Hentbestillingsordre();
        hboh.printBestillingsordre();
        tilbagetilhovedmenu = new JButton("HovedMenu");
        add(tilbagetilhovedmenu, BorderLayout.NORTH);
        JPanel test = hboh.HentOrdreGUI();
        add(test, BorderLayout.CENTER);
        Handler handler = new Handler();
        tilbagetilhovedmenu.addActionListener(handler);
4

1 に答える 1

1

まず第一に、あなたがあなたの一時的な文字列をどこで宣言するのかわかりません、そして

私があなたのコードで変更する2つのことがあります:

1)文字列ビルダーを使用します2)equalsIgnoreCaseを使用します3)buttonあまり意味がないため、文字列連結内でそれをどのように処理しているかを確認します

    public String printBestillingsordre(){

        StringBuilder temp = new StringBuilder();
        JButton button = new JButton("Varemodtaget");

        for(HentbestillingsordreRegistrer hentboregistrer: hbor){
        if(status.equalsIgnoreCase("Leveret"){    
            temp.append("<html>");
            temp.append("Ordre Status: ");
            temp.append(hentboregistrer.BestillingsStatus);
            temp.append("<br>");
            temp.append("LeverandoerID: ");
            temp.append(hentboregistrer.LeverandoerID);
            temp.append("<br>");
            temp.append("</html>");

        }else{
            temp.append("<html>");
            temp.append(hentboregistrer.BestillingsStatus);
            temp.append(button);     // whatever you're trying to append from button
            temp.append("<br>");
            temp.append("LeverandoerID: ");
            temp.append(hentboregistrer.LeverandoerID);
            temp.append("<br>");
            temp.append("</html>");
        }
        System.out.println(temp.toSTring());

    }
        return temp.toSTring();
}

編集:

回避策1(私の例で使用):<html></html>タグを追加する(開始と終了に追加する)ことを検討でき、改行が必要になるたびに<br>タグを使用します

回避策2::を使用SwingX Labelする

XLabel label = new JXLabel();
label.setLineWrap(true);

マルチスレッドアプリを開発している場合は、トレッドと同期されているStringBufferの使用を検討してください。詳細については、この質問を確認してください。

于 2012-12-10T10:58:16.587 に答える