检查状态并取消选中JCheckBox MenuItem ActinListner的状态

时间:2014-06-10 07:37:11

标签: java swing awt jtextarea

我有WordWrap checkBox MenuItem.By默认它是未选中(false)状态。当我运行程序并单击New menuitem并在我点击WordWrap menuItem后写一些文本时,WordWrap状态在前两次没有改变我想第一次改变状态(已检查)。我曾经尝试过,但没有工作。 这是代码:

public class CheckBoxMenuItem extends javax.swing.JFrame {

int i=0;
JTextArea textArea;
JScrollPane scrollpane;
public CheckBoxMenuItem() {
    initComponents();
    WordWrap.setSelected(false);
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    tabbedPane = new javax.swing.JTabbedPane();
    jMenuBar1 = new javax.swing.JMenuBar();
    jMenu1 = new javax.swing.JMenu();
    Create = new javax.swing.JMenuItem();
    WordWrap = 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);

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

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

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

private void CreateActionPerformed(java.awt.event.ActionEvent evt) {                                       
   final JInternalFrame internalFrame = new JInternalFrame("");
    i++;
    internalFrame.setName("Document"+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);
    internalFrame.setSize(internalFrame.getMaximumSize());
    internalFrame.pack();
    internalFrame.setVisible(true);
}                                      

private void WordWrapActionPerformed(java.awt.event.ActionEvent evt) {                                         
    WordWrap.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent ie) {
           AbstractButton button = (AbstractButton) ie.getItem();
           if(button.isSelected()){
              textArea.setLineWrap(true);
              scrollpane.validate(); 
           }
           else
           {
             textArea.setLineWrap(false);
             scrollpane.validate();  
           }
        }
    });
}                                        

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(CheckBoxMenuItem.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(CheckBoxMenuItem.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(CheckBoxMenuItem.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(CheckBoxMenuItem.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 CheckBoxMenuItem().setVisible(true);
        }
    });
}
// Variables declaration - do not modify                     
private javax.swing.JMenuItem Create;
private javax.swing.JCheckBoxMenuItem WordWrap;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JTabbedPane tabbedPane;
// End of variables declaration                   

}

1 个答案:

答案 0 :(得分:1)

WordWrapActionPerformed方法中,您除了添加ItemListener之外什么都不做,这不会做太多......

相反,您应该简单地执行事件的要求,例如......

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();
}

或者更改GUI属性中菜单项上的侦听器以使用ItemListener而不是ActionListener

您可能还希望通过Code Conventions for the Java Programming Language成为农场主,它会使您的代码更容易为其他人阅读;)