我正在尝试使用Button 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
}
答案 0 :(得分:3)
请参阅上面的链接。简而言之,您的视图的最小跨度应该等于首选跨度,然后不使用换行。
要使用基于空间的包裹,您应该正确定义中断权重。
UPDATE http://java-sl.com/tip_letter_wrap_java7.html该链接显示了Java 7的包装不同以及如何修复它。