如何添加清除菜单按钮?

时间:2014-10-21 23:09:42

标签: java swing

我的任务是创建一个Swing程序来计算圆形,三角形或矩形的面积。我做到了现在,如何添加clearMenu按钮来清除菜单?

/////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// /////////////////////

/*

THIS PROGRAM USES SWING TO CALCULATE THE AREA OF EITHER A
RECTANGLE, CIRCLE, OR TRIANGLE
*/
package AreaCalc;

/**
 *
* @author alexcarr
*/
public class SwingAreaCalculator extends javax.swing.JFrame {

    /**
     * Creates new form SwingAreaCalculator
     */
    public SwingAreaCalculator() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        circleButton = new javax.swing.JRadioButton();
        triangleButton = new javax.swing.JRadioButton();
        rectangleButton = new javax.swing.JRadioButton();
        dimension1 = new javax.swing.JLabel();
        dimension2 = new javax.swing.JLabel();
        dimension1TextField = new javax.swing.JTextField();
        dimension2TextField = new javax.swing.JTextField();
        dimension1Inches = new javax.swing.JLabel();
        dimension2Inches = new javax.swing.JLabel();
        calculateButton = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Area Calculator");

        buttonGroup1.add(circleButton);
        circleButton.setText("Circle");
        circleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                circleButtonActionPerformed(evt);
            }
        });

        buttonGroup1.add(triangleButton);
        triangleButton.setText("Triangle");

        buttonGroup1.add(rectangleButton);
        rectangleButton.setText("Rectangle");

        dimension1.setText("Dimension 1:");

        dimension2.setText("Dimension 2:");

        dimension2TextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dimension2TextFieldActionPerformed(evt);
            }
        });

        dimension1Inches.setText("inches");

        dimension2Inches.setText("inches");

        calculateButton.setText("Calculate Area");
        calculateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                calculateButtonActionPerformed(evt);
            }
        });

        jLabel5.setText("The area is:");

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel6))
                    .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(calculateButton)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(dimension2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(dimension2TextField, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dimension2Inches))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(circleButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(triangleButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rectangleButton))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(dimension1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dimension1TextField, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dimension1Inches)
                        .addGap(65, 65, 65)))
                .addContainerGap(91, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(circleButton)
                    .addComponent(triangleButton)
                    .addComponent(rectangleButton))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(dimension1)
                        .addGap(18, 18, 18))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(dimension1TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(dimension1Inches))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dimension2)
                    .addComponent(dimension2TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dimension2Inches))
                .addGap(18, 18, 18)
                .addComponent(calculateButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addContainerGap(89, Short.MAX_VALUE))
        );

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

    private void circleButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
    }                                            

    private void dimension2TextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                                    
        // TODO add your handling code here:
    }                                                   

    private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
    double area = 0; //placeholder area
    //circle execution
        if (circleButton.isSelected()) 
        { 
            String strRadius = dimension1TextField.getText(); 
            int radius = Integer.parseInt(strRadius); 
            area = areaCircle(radius); 
        } 
        String strArea = String.valueOf(area); 
        jLabel6.setText(strArea + " square inches"); 
        //rectangle execution
             if (rectangleButton.isSelected())
        {    
            String strLength = dimension1TextField.getText();
            double length = Integer.parseInt(strLength);
            String strWidth = dimension2TextField.getText();
            double width = Integer.parseInt(strWidth);
            area = areaRectangle(length, width);
             String strArea1 = String.valueOf(area); 
            jLabel6.setText(strArea1 + " square inches"); 
        }
        //triangle execution
            if (triangleButton.isSelected())
            {
            String strLength = dimension1TextField.getText();
            double length = Integer.parseInt(strLength);
            String strWidth = dimension2TextField.getText();
            double width = Integer.parseInt(strWidth);
            area = areaTriangle(length, width);
            String strArea1 = String.valueOf(area); 
            jLabel6.setText(strArea1 + " square inches");
            }

    }                                               

        //circle calculation
        public static double areaCircle (double radius)
    {
        //Area formula for circle
        double areaCircle = (int) (Math.pow(radius, 2) * (Math.PI));

        return areaCircle;
    }
    //rectangle calculation
    public static double areaRectangle (double length, double width)
    {
        //Area formula for rectangle
        int areaRectangle = (int) (length * width);
        //Print area
        return areaRectangle;
    }        
    //triangle calculation
    public static double areaTriangle (double length, double width)
    {
        int areaTriangle = (int) ((length*width)/2);
        return areaTriangle;
    }
    /**
     * @param args the command line arguments
     */
    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(SwingAreaCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SwingAreaCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SwingAreaCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SwingAreaCalculator.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 SwingAreaCalculator().setVisible(true);
            }

        });
    }


    // Variables declaration - do not modify                     
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton calculateButton;
    private javax.swing.JRadioButton circleButton;
    private javax.swing.JLabel dimension1;
    private javax.swing.JLabel dimension1Inches;
    private javax.swing.JTextField dimension1TextField;
    private javax.swing.JLabel dimension2;
    private javax.swing.JLabel dimension2Inches;
    private javax.swing.JTextField dimension2TextField;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JRadioButton rectangleButton;
    private javax.swing.JRadioButton triangleButton;
    // End of variables declaration                   
}

0 个答案:

没有答案
相关问题