如何在包装模式下为给定文本提供LineNumbers

时间:2014-08-08 07:17:19

标签: java swing awt document jtextarea

我在JTextArea中为文本编写了LineNumbers逻辑。每当输入一些文本并启用Viewlinenumbers时,它工作正常。但是,我用多行输入一些长行文本,然后我首先启用wordwrap操作,然后启用ViewLineNumbers,它无法正常工作。 请检查一次。

这是代码:

public class ViewLineNumbers extends javax.swing.JFrame {
  int i=0;
  JTextArea textArea,lnNum;
  JScrollPane scrollPane;
  public ViewLineNumbers() {
    initComponents();
    viewLineNumbers.setSelected(false);
    wordWrap.setSelected(false);
  }
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">
  private void initComponents() {

    tabbedPane = new javax.swing.JTabbedPane();
    menuBar = new javax.swing.JMenuBar();
    file = new javax.swing.JMenu();
    newAction = new javax.swing.JMenuItem();
    viewLineNumbers = new javax.swing.JCheckBoxMenuItem();
    wordWrap = new javax.swing.JCheckBoxMenuItem();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    file.setText("File");

    newAction.setText("New");
    newAction.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            newActionActionPerformed(evt);
        }
    });
    file.add(newAction);

    viewLineNumbers.setSelected(true);
    viewLineNumbers.setText("ViewLineNumbers");
    viewLineNumbers.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            viewLineNumbersActionPerformed(evt);
        }
    });
    file.add(viewLineNumbers);

    wordWrap.setSelected(true);
    wordWrap.setText("WordWrap");
    wordWrap.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            wordWrapActionPerformed(evt);
        }
    });
    file.add(wordWrap);

    menuBar.add(file);

    setJMenuBar(menuBar);

    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)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 279, Short.MAX_VALUE)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE))
    );

    pack();
  }// </editor-fold>

  private void newActionActionPerformed(java.awt.event.ActionEvent evt) {
    final JInternalFrame internalFrame = new JInternalFrame("");
    i++;
    internalFrame.setName("Doc "+i);
    internalFrame.setClosable(true);
    internalFrame.setAutoscrolls(true);
    textArea=new JTextArea();
    textArea.setFont(new java.awt.Font("Miriam Fixed", 0, 13));
    scrollPane=new JScrollPane(textArea);
    internalFrame.add(scrollPane);
    tabbedPane.add(internalFrame);
  }

  private void wordWrapActionPerformed(java.awt.event.ActionEvent evt) {
    AbstractButton button = (AbstractButton) evt.getSource();
    if (button.isSelected()) {
         textArea.setLineWrap(true);
    }
    else {
          textArea.setLineWrap(false);
    }
    textArea.revalidate();
  }

  private void viewLineNumbersActionPerformed(java.awt.event.ActionEvent evt) {
    AbstractButton button = (AbstractButton) evt.getSource();
                  if(button.isSelected()){
                    lnNum = new JTextArea("");
                    lnNum.setEditable(false);
                    lnNum.setSize(50,50);
                    lnNum.setFont(new java.awt.Font("Miriam Fixed", 0, 13));
                    lnNum.setText(getText());
                    textArea.getDocument().addDocumentListener(new DocumentListener(){
                     @Override
                     public void changedUpdate(DocumentEvent de) {
                     lnNum.setText(getText());
                  }
                @Override
                 public void insertUpdate(DocumentEvent de) {
                 lnNum.setText(getText());
                }
                @Override
                public void removeUpdate(DocumentEvent de) {
                lnNum.setText(getText());
                }
             });
                   scrollPane.getViewport().add(textArea);
                   scrollPane.setRowHeaderView(lnNum);
                   }
                   else{
                       scrollPane.setRowHeaderView(null);
                   }
  }
  public String getText(){
    int caretPosition = textArea.getDocument().getLength();
    javax.swing.text.Element root = textArea.getDocument().getDefaultRootElement();
    String text = "1" + System.getProperty("line.separator");
    for(int i = 2; i <=  root.getElementIndex( caretPosition ) + 1; i++){
        text += i + System.getProperty("line.separator");
    }
    return text;
  }
  public static void main(String args[]) {
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(ViewLineNumbers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(ViewLineNumbers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(ViewLineNumbers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(ViewLineNumbers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new ViewLineNumbers().setVisible(true);
        }
    });
  }
  private javax.swing.JMenu file;
  private javax.swing.JMenuBar menuBar;
  private javax.swing.JMenuItem newAction;
  private javax.swing.JTabbedPane tabbedPane;
  private javax.swing.JCheckBoxMenuItem viewLineNumbers;
  private javax.swing.JCheckBoxMenuItem wordWrap;
}

0 个答案:

没有答案