Loads of extra code!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kid Programmer
    New Member
    • Mar 2008
    • 176

    Loads of extra code!

    This is pretty stupid. You can create the same program one using this much code:
    Code:
    /*
     * blah.java
     *
     * Created on May 26, 2008, 9:35 AM
     */
    
    package blah;
    
    /**
     *
     * @author  Ned
     */
    public class blah extends javax.swing.JFrame {
    
        /** Creates new form blah */
        public blah() {
            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() {
    
            jButton1 = new javax.swing.JButton();
            jScrollPane1 = new javax.swing.JScrollPane();
            jTextArea1 = new javax.swing.JTextArea();
            jButton2 = new javax.swing.JButton();
            num1 = new javax.swing.JTextField();
            num2 = new javax.swing.JTextField();
            calculate_average = new javax.swing.JButton();
            average = new javax.swing.JLabel();
    
            jButton1.setText("jButton1");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jTextArea1.setColumns(20);
            jTextArea1.setRows(5);
            jScrollPane1.setViewportView(jTextArea1);
    
            jButton2.setText("jButton2");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            num1.setText("Number 1");
            num1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    num1ActionPerformed(evt);
                }
            });
    
            num2.setText("Number 2");
            num2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    num2ActionPerformed(evt);
                }
            });
    
            calculate_average.setText("Calculate Average");
            calculate_average.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    calculate_averageActionPerformed(evt);
                }
            });
    
            average.setText("Average");
    
            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()
                    .add(45, 45, 45)
                    .add(num1, 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(num2, 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(calculate_average)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(average)
                    .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(num1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(num2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(calculate_average)
                        .add(average))
                    .addContainerGap(271, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
        
    }                                        
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
        
    }                                        
    
    private void num1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    }
    
    private void num2ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    }
    
    private void calculate_averageActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
        int number1, number2, number_average;
        number1 = (int)((Double.parseDouble(num1.getText())));
        number2 = (int)((Double.parseDouble(num2.getText())));
        number_average = number1 + number2 / 2;
        average.setText("" + number_average);
    }
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new blah().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify
        private javax.swing.JLabel average;
        private javax.swing.JButton calculate_average;
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextArea jTextArea1;
        private javax.swing.JTextField num1;
        private javax.swing.JTextField num2;
        // End of variables declaration
    
    }
    and one using this much code:
    Code:
    package NumberAverager;
    
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    abstract class NumberAverager implements ActionListener
    {
    	public static void main(String[] args)
    	{
    		//create and set the title for a new frame
    		JFrame frame = new JFrame();
    		frame.setTitle("Number Averager");
    		
    		//create a container
    		Container contentPane = frame.getContentPane();
    		
    		//create a layout and apply it to the contentPane
    		FlowLayout layout = new FlowLayout();
    		frame.setLayout(layout);
    		
    		//create components
    		final JTextField num1 = new JTextField("Number 1");
    		final JTextField num2 = new JTextField("Number 2");
    		
    		JButton calculate_average = new JButton("Calculate average");
    		
    		final JLabel average = new JLabel("Average");
    		
    		//add the components
    		contentPane.add(num1);
    		contentPane.add(num2);
    		
    		contentPane.add(calculate_average);
    		
    		contentPane.add(average);
    		
    		//frame settings
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.pack();
    		frame.setVisible(true);
    
    		//actions performed
    		calculate_average.addActionListener(new ActionListener() 
    			{
    				public void actionPerformed(ActionEvent e) 
    				{
    					int number1, number2, number_average;
    					number1 = (int)((Double.parseDouble(num1.getText())));
    					number2 = (int)((Double.parseDouble(num2.getText())));
    					number_average = number1 + number2 / 2;
    					average.setText("" + number_average);
    				}
    			});
    		}
    	}
    They have the same output but one is wwwwwwwwwwwwwww wwwwwwwwwwaaaaa aaaaaaaaaaaaaaa aaaaaaayyyyyyyy yyyyy longer.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    And your question is?

    kind regards,

    Jos

    Comment

    • Kid Programmer
      New Member
      • Mar 2008
      • 176

      #3
      Originally posted by JosAH
      And your question is?

      kind regards,

      Jos
      My question is why would someone bother to write down the first code in the previous post when you could have a lot less to write in the second listing.

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by Kid Programmer
        My question is why would someone bother to write down the first code in the previous post when you could have a lot less to write in the second listing.
        I haven't looked at the code closely, but the first version uses GroupLayout, which suggest the code was made using a tool.

        Comment

        • Laharl
          Recognized Expert Contributor
          • Sep 2007
          • 849

          #5
          Yeah, the top one looks to have been generated by Netbeans.

          Comment

          • Kid Programmer
            New Member
            • Mar 2008
            • 176

            #6
            Originally posted by Laharl
            Yeah, the top one looks to have been generated by Netbeans.
            Yes it has been. But what is the point of it. They could make it so much simpler.

            Comment

            • BigDaddyLH
              Recognized Expert Top Contributor
              • Dec 2007
              • 1216

              #7
              Originally posted by Kid Programmer
              Yes it has been. But what is the point of it. They could make it so much simpler.
              Some GUI layout tools allow you to select the LayoutManager for a given container, but it seems that the default preference is for absolute positioning.

              In any case, people who care about their code eschew those tools.

              Comment

              Working...