Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Printing Using Swing.

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Printing Using Swing.

    I have my GUI All made, and this is my code below, I know this is what I have to use to output the area in my GUI, but does it go seperately after each if statement or at the end of them all?
    String strArea = String.valueOf(area); 
            outputLabel.setText(strArea + " square inches");


    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package swingarea;
     
    /**
     *
     * @author Logan
     */
    public class AreaCalculator extends javax.swing.JFrame {
     
        public AreaCalculator() {
            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();
            dimension1Label = new javax.swing.JLabel();
            rectangleRadioButton = new javax.swing.JRadioButton();
            triangleRadioButton = new javax.swing.JRadioButton();
            dimension2Label = new javax.swing.JLabel();
            dimension1Text = new javax.swing.JTextField();
            dimension2Text = new javax.swing.JTextField();
            inchesLabel1 = new javax.swing.JLabel();
            inchesLabel2 = new javax.swing.JLabel();
            calcAreaButton = new javax.swing.JButton();
            areaLabel = new javax.swing.JLabel();
            outputLabel = new javax.swing.JLabel();
            circleRadioButton = new javax.swing.JRadioButton();
            jMenuBar1 = new javax.swing.JMenuBar();
            jMenu1 = new javax.swing.JMenu();
            clearMenuItem = new javax.swing.JMenu();
            exitMenuItem = new javax.swing.JMenu();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Area Calculator");
     
            dimension1Label.setText("Dimension 1:");
     
            buttonGroup1.add(rectangleRadioButton);
            rectangleRadioButton.setText("Rectangle");
     
            buttonGroup1.add(triangleRadioButton);
            triangleRadioButton.setText("Triangle");
     
            dimension2Label.setText("Dimension 2:");
     
            inchesLabel1.setText("inches");
     
            inchesLabel2.setText("inches");
     
            calcAreaButton.setText("Calculate Area");
            calcAreaButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    calcAreaButtonActionPerformed(evt);
                }
            });
     
            areaLabel.setText("Area is:");
     
            buttonGroup1.add(circleRadioButton);
            circleRadioButton.setText("Circle");
     
            jMenu1.setText("File");
            jMenuBar1.add(jMenu1);
     
            clearMenuItem.setText("Clear");
            clearMenuItem.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    clearMenuItemActionPerformed(evt);
                }
            });
            jMenuBar1.add(clearMenuItem);
     
            exitMenuItem.setText("Exit");
            exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    exitMenuItemActionPerformed(evt);
                }
            });
            jMenuBar1.add(exitMenuItem);
     
            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()
                    .addGap(33, 33, 33)
                    .addComponent(circleRadioButton)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(10, 10, 10)
                                    .addComponent(areaLabel))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(calcAreaButton)
                                    .addComponent(outputLabel)))
                            .addGap(48, 48, 48))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(dimension1Label)
                                        .addComponent(dimension2Label))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(dimension2Text, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(dimension1Text, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGap(18, 18, 18)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(inchesLabel1)
                                        .addComponent(inchesLabel2)))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(16, 16, 16)
                                    .addComponent(rectangleRadioButton)
                                    .addGap(18, 18, 18)
                                    .addComponent(triangleRadioButton)))
                            .addContainerGap(34, Short.MAX_VALUE))))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(circleRadioButton)
                        .addComponent(rectangleRadioButton)
                        .addComponent(triangleRadioButton))
                    .addGap(41, 41, 41)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(dimension1Label)
                        .addComponent(dimension1Text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(inchesLabel1))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(dimension2Label)
                        .addComponent(dimension2Text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(inchesLabel2))
                    .addGap(31, 31, 31)
                    .addComponent(calcAreaButton)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(areaLabel)
                    .addGap(20, 20, 20)
                    .addComponent(outputLabel))
            );
     
            pack();
        }// </editor-fold>
     
        private void calcAreaButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
     double area = 0; 
     double areaCircle = 0;
            if (circleRadioButton.isSelected()) 
            { 
                String strRadius = dimension1Text.getText(); 
                int radius = Integer.parseInt(strRadius);          
                area = Math.PI*radius*radius; 
            } 
     
            else if (rectangleRadioButton.isSelected())
            {      
                String strLength = dimension1Text.getText();
                String strWidth = dimension2Text.getText();
                int length = Integer.parseInt(strLength);
                int width = Integer.parseInt(strWidth);
                area = length*width;
            }
            else if (triangleRadioButton.isSelected())
            {
                String triBase = dimension1Text.getText();
                String triHeight = dimension2Text.getText();
                int base = Integer.parseInt(triBase);
                int height = Integer.parseInt(triHeight);
                area = base*height/2;
            }
            String strArea = String.valueOf(area); 
            outputLabel.setText(strArea + " square inches");   
        }                                              
     
        public static double areaTriangle(double triBase,double triHeight, double triOutput, int shapeNum) 
    {
            triOutput = (triBase*triHeight)/2;
              if (shapeNum == 1 ) {
             return triOutput;
                  }
                }
    public static double areaCircle (double circOutput,double circRadi, int shapeNum ) 
    {
        {
     
        circOutput = (Math.pow (circRadi, 2)*Math.PI);   { 
                 if(shapeNum == 2 ) 
                   return circOutput;  
     
           }
     
    }
    }
     
    public static double areaRectangle(int rectOutput, int lengthRect, int widthRect, int shapeNum) {
        {   
        rectOutput = lengthRect*widthRect;
              if(shapeNum == 3 ) {
            return rectOutput;
     
      }  
         }
    }
        private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                             
            System.exit(0);
        }                                            
     
        private void clearMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                              
     
        }                                             
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
             double area = 0; 
     
            //<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(AreaCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(AreaCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(AreaCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(AreaCalculator.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 AreaCalculator().setVisible(true);
                }
            });
        }
         double area = 0; 
     
        // Variables declaration - do not modify
        private javax.swing.JLabel areaLabel;
        private javax.swing.ButtonGroup buttonGroup1;
        private javax.swing.JButton calcAreaButton;
        private javax.swing.JRadioButton circleRadioButton;
        private javax.swing.JMenu clearMenuItem;
        private javax.swing.JLabel dimension1Label;
        private javax.swing.JTextField dimension1Text;
        private javax.swing.JLabel dimension2Label;
        private javax.swing.JTextField dimension2Text;
        private javax.swing.JMenu exitMenuItem;
        private javax.swing.JLabel inchesLabel1;
        private javax.swing.JLabel inchesLabel2;
        private javax.swing.JMenu jMenu1;
        private javax.swing.JMenuBar jMenuBar1;
        private javax.swing.JLabel outputLabel;
        private javax.swing.JRadioButton rectangleRadioButton;
        private javax.swing.JRadioButton triangleRadioButton;
        // End of variables declaration
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Printing Using Swing.

    Can you clarify your question a bit? Please assume that we have no idea what this program is trying to do, what your assignment currently is, or in what way exactly you're stuck.

Similar Threads

  1. Jtable printing!
    By Vagelism22678 in forum AWT / Java Swing
    Replies: 0
    Last Post: April 20th, 2012, 01:18 PM
  2. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  3. Printing Job
    By qwerty53 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 19th, 2011, 08:34 AM
  4. Printing an array
    By native in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 16th, 2011, 09:07 AM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM