【问题标题】:How to Make Multiline (or New Line) JLabel in JForm如何在 JFrame 中制作多行(或换行)JLabel
【发布时间】:2018-12-24 07:15:30
【问题描述】:

我正在制作一个运输标签程序,其中输入的地址在 JLabel 上预览。 问题是 JLabel 不支持多行文本。 如何使其成为多行?

我尝试过 HTML 方法,但没有成功。

无论如何,我正在使用 Netbeans 8.0.2

private void buttonexitActionPerformed(java.awt.event.ActionEvent evt) {                                           
    int result = JOptionPane.showConfirmDialog(null, "Exit now?", "You're about to exit", JOptionPane.YES_NO_OPTION);
    if(result == JOptionPane.YES_OPTION){
        System.exit(0);
    }
    else if(result == JOptionPane.NO_OPTION) {
        this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE);
    }
}                                                                                    

private void buttonpreviewActionPerformed(java.awt.event.ActionEvent evt) {                                              
    pname.setFont(new java.awt.Font("Times New Roman", 0, 12));
    pname.setForeground(Color.BLACK);
    pname.setText(String.valueOf(inputname.getText()));

    pnumber.setFont(new java.awt.Font("Times New Roman", 0, 12));
    pnumber.setForeground(Color.BLACK);
    pnumber.setText(String.valueOf(inputnumber.getText()));

    paddress.setFont(new java.awt.Font("Times New Roman", 0, 12));
    paddress.setForeground(Color.BLACK);
    padress.setText(String.valueOf(inputaddress.getText()));



    pcourier.setFont(new java.awt.Font("Times New Roman", 1, 12));
    pcourier.setForeground(Color.BLACK);
  pcourier.setText(String.valueOf(cbcourier.getModel().getSelectedItem()));
}                                             

private void buttonclearActionPerformed(java.awt.event.ActionEvent evt) {                                            
    inputname.setText("");
    inputnumber.setText("");
    inputaddress.setText("");
    cbcourier.setSelectedIndex(0);
}                                           

private void buttonprintActionPerformed(java.awt.event.ActionEvent evt) {                                            
    Toolkit tk = panelpreview.getToolkit();
    PrintJob pj = tk.getPrintJob(this, null, null);
    Graphics g = pj.getGraphics();
    panelpreview.print(g);
    g.dispose();
    pj.end();

我希望“paddress”(JLabel)可以显示与我在“inputadress”(JTextField)中输入的文本相同的文本。有什么建议吗?

【问题讨论】:

    标签: java swing newline jlabel multiline


    【解决方案1】:

    我使用 HTML 找到了答案,将 /n 替换为

     padress.setText("<html>" + String.valueOf(inputaddress.getText().replaceAll("<","&lt;").replaceAll(">", "&gt;").replaceAll("\n", "<br/>")) + "</html>");
    

    来源:source

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-30
      • 1970-01-01
      • 2014-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多