【发布时间】: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