亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JLabel-將較長的文本顯示為多行?

JLabel-將較長的文本顯示為多行?

墨色風雨 2019-09-27 15:30:55
所以說我要在一行中顯示很長的一行JLabel。我該怎么做?當前,更長的行出現了:在此處輸入圖片說明我必須調整窗口大小才能查看全文。如何使文本幾乎達到我的寬度時出現換行符JFrame?我不確定這里是否需要任何代碼來回答這個問題,但是仍然:我的鏡框屬性:frame = new JFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(new Dimension(450, 400));frame.setLocation(new Point(400, 300));frame.setLayout(new BorderLayout());我要修改的標簽:question = new JLabel("Question:");question.setFont(new Font("Serif", Font.BOLD, 15));question.setHorizontalAlignment(JLabel.CENTER);編輯:更多詳細信息:我正在從文件中讀取行,然后顯示它們。線的大小不是固定的,所以我不知道放在哪里<br>。編輯2:我最終使用JTextArea。private JTextArea textAreaProperties(JTextArea textArea) {    textArea.setEditable(false);      textArea.setCursor(null);      textArea.setOpaque(false);      textArea.setFocusable(false);    textArea.setLineWrap(true);    textArea.setWrapStyleWord(true);    return textArea;}
查看完整描述

3 回答

?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

另一個例子,表明使用正確的布局管理器,HTML標簽中包裹的文本將自動包裝到可用空間...


在此處輸入圖片說明


public class TestHTMLLabel {


    public static void main(String[] args) {

        new TestHTMLLabel();

    }


    public TestHTMLLabel() {

        EventQueue.invokeLater(new Runnable() {

            @Override

            public void run() {

                try {

                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {

                }


                StringBuilder sb = new StringBuilder(64);

                sb.append("<html>I have something to say, it's beter to burn out then to fade away.").

                                append("  This is a very long String to see if you can wrap with in").

                                append("the available space</html>");


                JLabel label = new JLabel(sb.toString());


                JFrame frame = new JFrame("Test");

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                frame.setLayout(new BorderLayout());

                frame.add(label);

                frame.setSize(100, 100);

                frame.setLocationRelativeTo(null);

                frame.setVisible(true);

            }    

        });

    }        

}


查看完整回答
反對 回復 2019-09-27
?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

使用HTML在標簽內顯示文本。


JLabel fancyLabel = new JLabel("<html>Punch Taskmaster</html>");

(添加了Taskmaster建議的示例)


查看完整回答
反對 回復 2019-09-27
  • 3 回答
  • 0 關注
  • 917 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號