java - 如何在使用 GridBagLayout 布局的 JTextPane 中对文本进行换行

标签 java swing jtextpane gridbaglayout word-wrap

我使用带有 GridBagLayout 的 Jpane。在 GridBags 中我放置了一个 JTextPane 组件。 但是当我调整 JFrame 的大小时,我无法在 JTextPane 中对文本进行换行。 这是我的代码...

package problems;


import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.*;

public class ScrollbarTest {

private JFrame frame;
private JPanel panel;
private JScrollPane [] scrollpane = new JScrollPane[4];
private JTextPane [] textPane = new JTextPane[4];
private JScrollPane scrollbar;


ScrollbarTest() {
    //initialize jtextarea
    for(int i=0;i<textPane.length;i++) {

        textPane[i]=new JTextPane();            
        textPane[i].setText("xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx | ");
        scrollpane[i] = new JScrollPane(textPane[i]); 
    }

    panel     = new JPanel();
    frame     = new JFrame();

    createList(panel);
    scrollbar = new JScrollPane(panel);

    frame.add(scrollbar);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setSize(new Dimension(400,400));          
} //constructor

//method to easily add components to GridBags
static void addComponent(JPanel panel, 
                         GridBagLayout gbl, 
                         Component c,
                         int x, int y,
                         int width, int height,
                         double weightx, double weighty) {

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = x; gbc.gridy = y;
    gbc.gridwidth = width; gbc.gridheight = height;
    gbc.weightx = weightx; gbc.weighty = weighty;
    gbl.setConstraints( c, gbc );
    panel.add( c ); 
}//addComponent

public void createList(JPanel panel) {
    //setting layout: "GridBagLayout"
    GridBagLayout gbl = new GridBagLayout();        
    panel.setLayout(gbl);                        

    //put created components to the gridbags of gridbaglayout
    //                                            x  y  w  h  wx wy
    addComponent( panel, gbl, scrollpane[0]     , 0, 1, 1, 1, 1 ,0  );
    addComponent( panel, gbl, scrollpane[1]     , 0, 2, 1, 1, 1 ,0  );
    addComponent( panel, gbl, scrollpane[2]     , 1, 1, 1, 1, 1 ,0  );
    addComponent( panel, gbl, scrollpane[3]     , 1, 2, 1, 1, 1 ,0  );
} //createList

public static void main (String[] args) {
    new ScrollbarTest();        
} //main

} //end of class

问题出在哪里?

提前非常感谢

最佳答案

您缺少setLineWrap在你的 JTextArea 上。

for(int i=0;i<textArea.length;i++) {
        textArea[i]=new JTextArea();
        textArea[i].setLineWrap(true);
        textArea[i].setText("xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx | ");
    }

编辑

in the case of JTextPane you shall create a scrollpane with no horizontal scroll policy and set your JtextPane as its viewPort

关于java - 如何在使用 GridBagLayout 布局的 JTextPane 中对文本进行换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23783797/

相关文章:

Java 在输入时创建字符串

java - 无法将背景图像添加到我的 jpanel,找不到错误,我也不明白错误

java - 如何在不触发 insertUpdate() 的情况下将文本插入文档

java - 动态编辑 JCombobox 中的项目

java - 搜索文本文件并在 JPanel 中显示结果

java - RxJava 调用后从方法返回字符串

java - 格式化用于显示文件系统树的 JTree 上的标签

java - 不同类型的单例模式

java - String.format 不完全工作?