如何使用Swing组件为每个Document设置复选框MenuItems的状态

时间:2014-08-06 05:50:27

标签: java swing checkbox awt menuitem

我想更改特定文档的checkBox menuItems(已启用或已禁用)的状态,而不是所有Documents.For示例在我的应用程序中,我创建了多个文档,我想启用视图空间复选框menuItem for doc1,它已启用所有创建的文档仅适用于Doc1。问题是Checkbox仅为Doc1启用,并非为所有人提供。请给我一些建议或者如果可能的话请提供一个示例。

我的代码:

public class ResetState extends javax.swing.JFrame {
int i=0;
JTextPane textPane;
public ResetState() {
    initComponents();
    viewSpaces.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();
    viewSpaces = 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);

    viewSpaces.setSelected(true);
    viewSpaces.setText("ViewSpaces");
    viewSpaces.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            viewSpacesActionPerformed(evt);
        }
    });
    file.add(viewSpaces);

    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 createActionPerformed(java.awt.event.ActionEvent evt) {                                       
    final JInternalFrame internalFrame = new JInternalFrame("");
    i++;
    internalFrame.setName("Doc "+i);
    internalFrame.setClosable(true);
    internalFrame.setAutoscrolls(true);
    textPane=new JTextPane();
    internalFrame.add(textPane);
    tabbedPane.add(internalFrame);
    tabbedPane.setSelectedIndex(i-1);
    internalFrame.addInternalFrameListener(new InternalFrameListener() {
        @Override
        public void internalFrameOpened(InternalFrameEvent ife) {
        }
        @Override
        public void internalFrameClosing(InternalFrameEvent ife) {
        }
        @Override
        public void internalFrameClosed(InternalFrameEvent ife) {
        }
        @Override
        public void internalFrameIconified(InternalFrameEvent ife) {
        }
        @Override
        public void internalFrameDeiconified(InternalFrameEvent ife) {
        }
        @Override
        public void internalFrameActivated(InternalFrameEvent ife) {
          viewSpaces.setSelected(false);
        }
        @Override
        public void internalFrameDeactivated(InternalFrameEvent ife) {
        }
    });
    internalFrame.setSize(internalFrame.getMaximumSize());
    internalFrame.pack();
    internalFrame.setVisible(true);       
}                                      

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


}                                          

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(ResetState.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(ResetState.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(ResetState.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(ResetState.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new ResetState().setVisible(true);
        }
    });
}                    
private javax.swing.JMenuItem create;
private javax.swing.JMenu file;
private javax.swing.JMenuBar menuBar;
private javax.swing.JTabbedPane tabbedPane;
private javax.swing.JCheckBoxMenuItem viewSpaces;                  

}

0 个答案:

没有答案