如何使用swing重置检查状态并取消选中Menuitems for Multiple documents的状态

时间:2014-07-30 06:29:44

标签: java swing jcheckbox jmenuitem

我想重置checkboxMenuItem的状态。例如我创建了多个Document然后在最近打开的文档中添加了一些文本并选择了LineNumbers然后我转到previos打开文档那时,linenumbers复选框被重置为原始状态取消选择状态是否我在"选中状态"在另一个Dcoument.please帮助我。谢谢。

这是代码:

import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;

public class ResetItems extends javax.swing.JFrame {

    JTextField status;
    int i=0;
    JTextPane currentTextPane,lnNum;
    JScrollPane scrollPane;

    public ResetItems() {
        initComponents();
        lineNumbers.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();
        create = new javax.swing.JMenuItem();
        lineNumbers = new javax.swing.JCheckBoxMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        file.setText("File");

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

        lineNumbers.setSelected(true);
        lineNumbers.setText("LineNumbers");
        lineNumbers.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lineNumbersActionPerformed(evt);
            }
        });
        file.add(lineNumbers);

        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.Alignment.TRAILING, 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 createActionPerformed(java.awt.event.ActionEvent evt) {                                       
        final JInternalFrame internalFrame = new JInternalFrame("");
        i++;
        internalFrame.setName("Doc "+i);
        internalFrame.setClosable(true);
        internalFrame.setAutoscrolls(true);
        currentTextPane=new JTextPane();
        currentTextPane.setFont(new java.awt.Font("Miriam Fixed", 0, 13));
        tabbedPane.add(internalFrame);
        tabbedPane.add(internalFrame);
        tabbedPane.setSelectedIndex(i-1);
        currentTextPane.setCaretPosition(0);
        scrollPane=new JScrollPane(currentTextPane);
        internalFrame.add(scrollPane);
        internalFrame.setSize(internalFrame.getMaximumSize());
        internalFrame.pack();
        internalFrame.setVisible(true);
    }                                      

    private void lineNumbersActionPerformed(java.awt.event.ActionEvent evt) {                                            

    }                                           

    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(ResetItems.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ResetItems.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ResetItems.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ResetItems.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ResetItems().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JMenuItem create;
    private javax.swing.JMenu file;
    private javax.swing.JCheckBoxMenuItem lineNumbers;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JTabbedPane tabbedPane;
    // End of variables declaration                   
}

0 个答案:

没有答案