用空格替换点并使用java swing反转

时间:2014-06-03 06:38:03

标签: java swing awt

我想在点击View-spaces复选框MenuItem时显示点而不是空格。我的问题是当我点击复选框时它用dot替换空格但是,当我向textarea添加一些额外的文本并点击时Viewspace JCheckBox menuitem,它只采用了以前的文本并替换了。我试过这个,请运行我的代码你可以很容易地理解这个问题。请给出一些建议.....谢谢。 这是我的代码:

public class VisibleSpaces extends javax.swing.JFrame {
int i=0;
JTextArea tx,lnNum;
JScrollPane scrollpane;
public VisibleSpaces() {
    initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    tp = new javax.swing.JTabbedPane();
    jMenuBar1 = new javax.swing.JMenuBar();
    jMenu1 = new javax.swing.JMenu();
    Create = new javax.swing.JMenuItem();
    ViewSpace = new javax.swing.JCheckBoxMenuItem();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jMenu1.setText("File");

    Create.setText("Create");
    Create.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            CreateActionPerformed(evt);
        }
    });
    jMenu1.add(Create);

    ViewSpace.setSelected(true);
    ViewSpace.setText("ViewSpaces");
    ViewSpace.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            ViewSpaceActionPerformed(evt);
        }
    });
    jMenu1.add(ViewSpace);

    jMenuBar1.add(jMenu1);

    setJMenuBar(jMenuBar1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(tp, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(tp, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
    );

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

private void ViewSpaceActionPerformed(java.awt.event.ActionEvent evt) {                                          
   ViewSpace.addItemListener(new ItemListener() {
           String str=tx.getText();
           String previous=str;

        @Override
        public void itemStateChanged(ItemEvent ie) {
            if(ViewSpace.getState()){
                tx.setText(previous.replace(" ","."));
        }
            else
                tx.setText(str);
        }
    });
}                                         

private void CreateActionPerformed(java.awt.event.ActionEvent evt) {                                       
    final JInternalFrame internalFrame = new JInternalFrame("");
    i++;
    internalFrame.setName("Document"+i);
    internalFrame.setClosable(true);
    internalFrame.setAutoscrolls(true);
    tx=new JTextArea();
    tx.setFont(new java.awt.Font("Miriam Fixed", 0, 13));
    scrollpane=new JScrollPane(tx);
    internalFrame.add(scrollpane);
    tp.add(internalFrame);
    internalFrame.setSize(internalFrame.getMaximumSize());
    internalFrame.pack();
    internalFrame.setVisible(true);
}                                      

public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    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(VisibleSpaces.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(VisibleSpaces.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(VisibleSpaces.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(VisibleSpaces.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new VisibleSpaces().setVisible(true);
        }
    });
}
// Variables declaration - do not modify                     
private javax.swing.JMenuItem Create;
private javax.swing.JCheckBoxMenuItem ViewSpace;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JTabbedPane tp;
// End of variables declaration                   

}

1 个答案:

答案 0 :(得分:0)

你的代码不正确,充满了糟糕的做法。经过大量的努力,我理解了你的问题。我只是给出了具体问题的解决方案,但是试着按照你的问题给出的评论,以便更好地理解Java swing组件的用法。

解决方案:

更改代码的这一部分

public void itemStateChanged(ItemEvent ie) {
            if(ViewSpace.getState()){
                tx.setText(previous.replace(" ","."));
        }
            else
                tx.setText(str);
        }

这一个

public void itemStateChanged(ItemEvent ie) {
                if(ViewSpace.getState()){
                    tx.setText(tx.getText().replace(" ","."));

            }
                else
                {
                    String last = tx.getText().substring(tx.getText().length()-1, tx.getText().length());
                    String rest = tx.getText().substring(0, tx.getText().length()-1);
                    if(!last.equals("."))
                    tx.setText(tx.getText().replace("."," "));
                    else
                        {
                        rest=rest.replace("."," ");
                        tx.setText(rest+".");

                        }
                }