Java 如何跨越多行的JLabel Acc?

Java 如何跨越多行的JLabel Acc?,java,jpanel,Java,Jpanel,我通常不会在这里问问题,但我被难住了。我在java应用程序中有一个相当长的JLabel。无论我做什么尝试,Java都不会将此文本包装到下一行。我做过研究,每个人都告诉我在它周围放上HTML标签,但当我尝试时,我的整个GUI停止工作。这是一个学校项目!非常感谢您的帮助 下面是一些代码:(这是一个由main方法表示的类文件) import javax.swing.*; 导入java.awt.event.*//需要按钮侦听器 导入java.awt.*//布局类 公共类gui扩展JFrame实现Swin

我通常不会在这里问问题,但我被难住了。我在java应用程序中有一个相当长的JLabel。无论我做什么尝试,Java都不会将此文本包装到下一行。我做过研究,每个人都告诉我在它周围放上HTML标签,但当我尝试时,我的整个GUI停止工作。这是一个学校项目!非常感谢您的帮助

下面是一些代码:(这是一个由main方法表示的类文件)

import javax.swing.*;
导入java.awt.event.*//需要按钮侦听器
导入java.awt.*//布局类
公共类gui扩展JFrame实现SwingConstants
{
//阵列
公共JPanel[]面板=新JPanel[6];
公共JPanel[]嵌套_面板=新JPanel[6];
public JLabel[]productDescriptions=新JLabel[6];
公共产品[]家庭产品数组=新产品[6];
public JLabel[]productTitles=新JLabel[6];
公共JLabel[]产品价格=新JLabel[6];
公共JButton[]按钮=新JButton[6];
//常数
最终int窗口_宽度=1000;
最终内窗高度=800;
//主窗口
公共JFrame主窗口;
/*    
建造师
*/
公共图形用户界面(){
//主窗口
JFrame主窗口=新JFrame(“服装4销售”);
//集合布局
设置布局(新的网格布局(2,3));
//窗口标题
mainWindow.setTitle(“服装4销售”);
//窗口大小
主窗口。设置大小(窗口宽度、窗口高度);
//关闭按钮
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//初始化窗口
mainWindow.setVisible(true);
//拔头筹
productList product_list=新产品列表();
home_product_array=产品列表。获取产品_array();

对于(inti=0;i有支持多行标签的库,但对于学校项目,您可能确实应该使用HTML

您可以使用这样的方法,它将换行并尊重换行符(原始文本中的换行符):

/*…*/newjlabel(asHtml(somemultilitextstring));
/*...*/
专用字符串asHtml(字符串文本){
返回“+text.replace”(“\n”和“
”)+”; }

编辑:同样,这是一个学校项目。对于真实世界的程序,你会使用一个库,或者至少转义多于换行符(也有一个库).

为什么不使用jtextarea?@yelliver这也可以满足要求,但也需要做一些工作:使其不可编辑、设置透明背景、删除边框……因此根据需要,
jtextarea
可能更合适。不管怎样,你所说的“整个GUI停止工作”到底是什么意思?
import javax.swing.*;
import java.awt.event.*;//needed for button listeners
import java.awt.*;//Layout class

 public class gui extends JFrame implements SwingConstants
{

   //Arrays
   public JPanel[] panels = new JPanel[6];
   public JPanel[] nested_panels = new JPanel[6];
   public JLabel[] productDescriptions = new JLabel[6];
   public product[] home_product_array = new product[6];
   public JLabel[] productTitles = new JLabel[6];
   public JLabel[] productPrices = new JLabel[6];
   public JButton[] buttons = new JButton[6];

  //Constants 
   final int WINDOW_WIDTH = 1000;
   final int WINDOW_HEIGHT = 800;

  //Main Window
   public JFrame mainWindow;

 /*    
        Constructor
 */
 public gui(){
 //main window
 JFrame mainWindow = new JFrame("Clothes 4 Sale");
 //set layout
 mainWindow.setLayout(new GridLayout(2,3));
 //title of window
 mainWindow.setTitle("Clothes 4 Sale");
 //size of window
 mainWindow.setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
 //Close Button
 mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 //inititate window
 mainWindow.setVisible(true);

//pull first title
productList product_list = new productList();
home_product_array = product_list.get_product_array();



for(int i=0;i<6;i++){
//print whole array
System.out.println(home_product_array[i].get_product_title());
System.out.println(home_product_array[i].get_product_description());
System.out.println(home_product_array[i].get_product_price_string());
//

productTitles[i] = new JLabel(home_product_array[i].get_product_title());
productDescriptions[i] = new 
JLabel(home_product_array[i].get_product_description());
productPrices[i] = new 
JLabel(home_product_array[i].get_product_price_string());
productPrices[i].setForeground(Color.GREEN);
panels[i] = new JPanel();
panels[i].setLayout(new BorderLayout(5,10));
panels[i].add(productTitles[i], BorderLayout.NORTH);
panels[i].add(productDescriptions[i], BorderLayout.CENTER);
nested_panels[i] = new JPanel();
nested_panels[i].add(productPrices[i]);
buttons[i] = new JButton("Add To Cart");
nested_panels[i].add(buttons[i]);
panels[i].add(nested_panels[i], BorderLayout.SOUTH);
mainWindow.add(panels[i]);
}//for loop
/*


*/

  }//End of constructor



  }//end of class file
/*...*/ new JLabel(asHtml(someMultilineTextString));
/*...*/
private String asHtml(String text) {
    return "<HTML>" + text.replace("\n", "<br>") + "</HTML>";
}