【问题标题】:How to LineWrap a text in JTextPane that is layouted with GridBagLayout如何在使用 GridBagLayout 布局的 JTextPane 中换行文本
【发布时间】:2014-05-21 12:55:56
【问题描述】:

我使用带有 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

问题出在哪里?

在此先感谢

【问题讨论】:

  • 您使用的是 JTextPane 还是 JTextArea?
  • 上面的代码中没有 JTextPane .. 我错过了什么吗?
  • 其实和上面的代码一样,只是用JTextPane代替了JTextArea
  • 好的,我已经更改了代码。现在它是用 JTextPane 组件编写的

标签: java swing jtextpane gridbaglayout word-wrap


【解决方案1】:

您的 JTextArea 上缺少 setLineWrap

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 | ");
    }

编辑

在 JTextPane 的情况下,您应该创建一个没有 水平滚动策略并将您的 JtextPane 设置为其视口

【讨论】:

  • 谢谢!以及如何使用 JTextPanes 处理 lineWrapping?
  • 谢谢。现在,我将每个 JTextPane 放入了一个滚动窗格中。如何设置视口?对不起,我有点困惑..
  • 很高兴它对您有所帮助。 .请投票/接受答案.. hv 有趣的编码:)
  • 另一个问题:我的组件始终具有固定大小。我可以手动调整 JScrollPane 或 JTextPane 组件的大小吗??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-29
  • 2013-04-30
相关资源
最近更新 更多