【问题标题】:JTextPane wrap and unwrap long words with out space using JButtonJTextPane 使用 JButton 换行和解开没有空格的长单词
【发布时间】:2014-06-27 07:14:21
【问题描述】:

我正在尝试通过使用 Button to JTextPane 来实现 wordwrap/unwrap。我试过了,但它不能正常工作。这里的问题是:

  • 我输入了带有空格的文本,它正在工作,
  • 没有空格就不行。

这是我的代码:

public class TestVisual extends javax.swing.JFrame {

    private boolean wrapped;
    private JButton toggleButton = null;
    private JTextPane jtp = null;
    private JPanel noWrapPanel = null;
    private JScrollPane scrollPane = null;

    public TestVisual() {
       super();
        init();
    }

    public void init() {
         this.setSize(300, 200);
         this.setLayout(new BorderLayout());
         wrapped = false;
         jtp = new JTextPane();
         noWrapPanel = new JPanel( new BorderLayout() );
         noWrapPanel.add( jtp );
         scrollPane = new JScrollPane( noWrapPanel);     
         toggleButton = new JButton("wrap");
         toggleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                if (wrapped == true){
                    scrollPane.setViewportView(noWrapPanel);
                     noWrapPanel.add(jtp); 
                    wrapped = false;
                }else {   
                    scrollPane.setViewportView(jtp);
                    toggleButton.setText("unWrap");
                    wrapped = true;
                }
            }
        });
              this.add(scrollPane, BorderLayout.CENTER);
              this.add(toggleButton, BorderLayout.NORTH);
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        
     public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TestVisual().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    // End of variables declaration                   
}

【问题讨论】:

    标签: java swing awt word-wrap jtextpane


    【解决方案1】:

    http://java-sl.com/wrap.html

    请参阅上面的链接。简而言之,您的视图的最小跨度应该等于首选跨度,然后不使用换行。

    要使用基于空间的包装,您应该正确定义断裂重量。

    更新 http://java-sl.com/tip_letter_wrap_java7.html 该链接显示了 Java 7 的包装差异以及如何修复它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-29
      • 2010-09-26
      • 1970-01-01
      • 2021-10-31
      • 1970-01-01
      • 2010-12-10
      相关资源
      最近更新 更多