⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainframe.java

📁 一个简单的操作系统模拟程序 java+swing 可进行一般的文件操作 进程建立及删除等
💻 JAVA
📖 第 1 页 / 共 5 页
字号:

        jMenuBar1.add(systemMenu);

        helpMenu.setText("Help");
        aboutMenuItem.setText("About");
        aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutMenuItemActionPerformed(evt);
            }
        });

        helpMenu.add(aboutMenuItem);

        TipMenu.setText("Tips");
        cmdtipsMenuItem.setText("Commend Tips");
        cmdtipsMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdtipsMenuItemActionPerformed(evt);
            }
        });

        TipMenu.add(cmdtipsMenuItem);

        systemTipMenuItem.setText("System Tips");
        systemTipMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                systemTipMenuItemActionPerformed(evt);
            }
        });

        TipMenu.add(systemTipMenuItem);

        helpMenu.add(TipMenu);

        jMenuBar1.add(helpMenu);

        setJMenuBar(jMenuBar1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(diskUsageBar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
                        .addContainerGap())
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, cmdInputLabel)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, cmdInputField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, cmdOutputLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, cmdHistoryLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE))
                        .add(14, 14, 14))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(diskUsageLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 127, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(cmdInputLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(cmdInputField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 21, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(cmdHistoryLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(cmdOutputLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 227, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(diskUsageLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(diskUsageBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 138, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(13, Short.MAX_VALUE))
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void systemTipMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_systemTipMenuItemActionPerformed
// TODO add your handling code here:
        new SystemTips().setVisible(true);
    }//GEN-LAST:event_systemTipMenuItemActionPerformed

    private void cmdtipsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdtipsMenuItemActionPerformed
// TODO add your handling code here:
        new CommandTips().setVisible(true);
    }//GEN-LAST:event_cmdtipsMenuItemActionPerformed

    private void resumeMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resumeMenuItemActionPerformed
// TODO add your handling code here:
        sysClock.start();
    }//GEN-LAST:event_resumeMenuItemActionPerformed

    private void pauseMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pauseMenuItemActionPerformed
// TODO add your handling code here:
        sysClock.stop();
    }//GEN-LAST:event_pauseMenuItemActionPerformed
    
    /**the helding code for the Help menu -> About MenuItem*/
    private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed
// TODO add your handling code here:
        new AboutFrame().setVisible(true);
    }//GEN-LAST:event_aboutMenuItemActionPerformed
    
    /**the helding code for the System menu -> Shutdown System MenuItem*/
    private void shutdownMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_shutdownMenuItemActionPerformed
// TODO add your handling code here
        int result = JOptionPane.showConfirmDialog(null,"Really Want To Shut Down The System ?","Need Confirm",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
        if(result == JOptionPane.YES_OPTION){
            try{
                simDisk.saveToFile();
            }
            catch(IOException ext){}
            System.exit(0);
        }
    }//GEN-LAST:event_shutdownMenuItemActionPerformed

    /**this method is used to set the system shell prompt*/
    private void setSysPrompt(){
        sysPrompt = "root@" + ((DefaultMutableTreeNode) treeModel.getCurrentNode()).getUserObject().toString() + "/ :" ;
    }
    
    /**calculate the usage of the disk*/
    public void calculateDisk(){
        char[] fatContent;
        int count = 0;
        float diskUsage;
        
        diskUsageArea.setText("");//clear the text area first
        fatContent = simDisk.readFAT();
        for(int i = 0; i < fatContent.length;i++){
            if(fatContent[i] == 0){
                diskUsageArea.append("Block " + (i + 1) + ": UNUSED\n");
            } else {
                diskUsageArea.append("Block " + (i + 1) + ": USED\n");
                count++;
            }
        }
        
        diskUsage = (((float)count)/128)*100;
        diskUsageBar.setValue((int)diskUsage);
    }
    
    /**the commend input field helding code ,acturally is the shell for the system */
    private void cmdInputFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdInputFieldActionPerformed
// TODO add your handling code here:
        String input;//recieve the commend from the field
        String[] commend;//the input commend 
        String[] args;//the argument for the commend;
        input = cmdInputField.getText();
        cmdHistoryLabel.setText("Last Command: " + input);
        
        if( !input.equals("") ){
            commend = input.split("\\s+");
            if(commend[0].equals("touch")){ //the create commend 
                if((commend.length == 1) || commend[1].equals("/") || commend[1].equals(".") || commend[1].equals("..")){ // the invalid arguments
                    cmdOutputLabel.setText(sysPrompt + " touch: invalid argument.");
                } else if( (treeModel.getChild(commend[1].split("/"))) != null){
                            cmdOutputLabel.setText(sysPrompt + " touch: file already exist.");
                        } else if((treeModel.getParent(commend[1].split("/"))) == null){
                                    cmdOutputLabel.setText(sysPrompt + " touch: wrong path or dictory");
                                } else {
                                    String fileName;
                                    String[] dentry;
                                    String tmpFileName;
                                    
                                    //split the path
                                    dentry = commend[1].split("/");
                                    //get the file name
                                    tmpFileName = dentry[dentry.length - 1];
                                    if(tmpFileName.length() > 10){ //for the situation that the length of file name is larger than 10
                                        fileName = tmpFileName.substring(0,9);
                                    } else {
                                        fileName = tmpFileName;
                                    }
                                    //create the inode
                                    INode newINode = new INode(fileName,simDisk); 
                                    newINode.setExtName('e');//set the file type to excutable file
                                    DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(newINode);
                                    newChild.setAllowsChildren(false);
                                    //insert the new tree node into the tree
                                    try{
                                        DefaultMutableTreeNode parent = (DefaultMutableTreeNode)treeModel.getParent(dentry);
                                        treeModel.insertNodeInto(newChild,parent,parent.getChildCount());
                                        cmdOutputLabel.setText(sysPrompt + " touch: create file " + fileName + " scessfully !");
                                        
                                        TreeNode[]   nodes   =   treeModel.getPathToRoot(newChild);   
                                        TreePath   pathToRoot   =   new   TreePath(nodes);   
                                        fileTree.scrollPathToVisible(pathToRoot);
                                        this.calculateDisk();
                                    }
                                    catch(IllegalStateException exception){
                                        cmdOutputLabel.setText(sysPrompt + " touch: file is not allow to have children");
                                    }
                            }
//                    cmdOutputLabel.setText(sysPrompt + "create");
            } else if (commend[0].equals("mkdir")){ //the mkdir commend

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -