0

This is my program below and I am trying to figure out where my main method should be. I have seen a few examples of it being implemented at the very end of the program but there main is different from mine.

Main method (to be implemented):

               public class JFrame
                   {
                  public static void main(String[] args)
                   {


                   JFrame frame = new JFrame(); 

                    frame.setTitle("Components File");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true); 
                    }
                       }

My Program

  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JTextField;

  public class Lab_10 extends JFrame
  {

  private final double EARTHQUAKE_RATE= 8.0;
  private final int FRAME_WIDTH= 300;
  private final int FRAME_HEIGHT= 200;

  private JLabel rLabel;
  private JTextField eField;
  private JButton button;
  private JLabel earthLabel;

  public Lab_10()
  {
  JLabel earthLabel = new JLabel("Most structures fall");

  makeTextField();
  makeButton();
  makePanel();
  setSize(FRAME_WIDTH, FRAME_HEIGHT);
   }

  private void makeTextField()
   {
    JLabel rLabel = new JLabel("Richter");

    final int FIELD_WIDTH = 10;
    eField = new JTextField(FIELD_WIDTH);
    eField.setText("" + EARTHQUAKE_RATE);
    }


    class AddLabelListener implements ActionListener
     {
     public void actionPerformed(ActionEvent event)
      {
     earthLabel.setText("Most structures fall");
        }
       }

      private void makeButton()
       {
      JButton button = new JButton("Enter");

       ActionListener listener = new AddLabelListener();
       button.addActionListener(listener);
        }

       private void makePanel()
        {
       JPanel panel = new JPanel();
       panel.add(rLabel);
       panel.add(eField);
       panel.add(button);
       panel.add(earthLabel);
       add(panel);
        }
         }

Updated Code (which is compiling but and running but with logic errors because it implements an empty frame [guess as much from the main method I have]):

  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JTextField;

  public class Lab_10 extends JFrame
  {

  private final double EARTHQUAKE_RATE= 8.0;
  private final int FRAME_WIDTH= 300;
  private final int FRAME_HEIGHT= 200;

  private JLabel rLabel;
  private JTextField eField;
  private JButton button;
  private JLabel earthLabel;

  public Lab_10()
  {
  JLabel earthLabel = new JLabel("Most structures fall");

  makeTextField();
  makeButton();
  makePanel();
  setSize(FRAME_WIDTH, FRAME_HEIGHT);
   }

   private void makeTextField()
    {
   JLabel rLabel = new JLabel("Richter");

    final int FIELD_WIDTH = 10;
     eField = new JTextField(FIELD_WIDTH);
     eField.setText("" + EARTHQUAKE_RATE);
     }


         class AddLabelListener implements ActionListener
          {
         public void actionPerformed(ActionEvent event)
           {
         earthLabel.setText("Most structures fall");
            }
           }

          private void makeButton()
          {
          JButton button = new JButton("Enter");

          ActionListener listener = new AddLabelListener();
           button.addActionListener(listener);
            }

          private void makePanel()
           {
           JPanel panel = new JPanel();
          panel.add(rLabel);
          panel.add(eField);
          panel.add(button);
          panel.add(earthLabel);
          add(panel);
           }
          public static void main(String[] args){
          javax.swing.SwingUtilities.invokeLater(new Runnable(){
           @Override
             public void run()
          { JFrame frame = new JFrame(); 

            frame.setTitle("Components File");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setVisible(true); 
             }



             });
             }
             }
  • Why have you kept import statements in the middle? Take them to the TOP. And you can keep main anywhere in the Java class, it won't impact functionality. However, it is generally kept as the first or the last method in the class – Infinite Recursion Dec 04 '13 at 05:19
  • More info : http://stackoverflow.com/questions/7911829/preferable-location-of-main-method-in-java-class-file – Infinite Recursion Dec 04 '13 at 05:19
  • Generally speaking it won't make much difference where you put it, so long as it's within scope correctly. Personally, I like it towards the top, as it's easier to find (IMHO). You may also want to take a look at [Initial Threads](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) which might explain why you `main` method is different from other peoples... – MadProgrammer Dec 04 '13 at 05:24
  • @payeli oh no the are at the top I should have separated it with a text saying my program instead...my main method in my program is not like that i was just asking if I could use this and how. – user3019552 Dec 04 '13 at 05:29
  • @MadProgrammer **SwingUtilities.invokeLater(new Runnable() { public void run() {JFrame frame = new JFrame(); frame.setTitle("Components File"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }** Would my method look like this? – user3019552 Dec 04 '13 at 05:33
  • Take a look at the examples from the [Swing Tutorial](http://docs.oracle.com/javase/tutorial/uiswing/TOC.html). They always put the main() method ad the end, which makes sense to me because the code is basically the same every time as it just invokes your class. So the unique code for you class should be at the top where it is more prominent. – camickr Dec 04 '13 at 05:36
  • The other "potential" problem (and I don't know if it's part of the question), is you're simply creating a `JFrame` instead of an instance of your `Lab_10` class... – MadProgrammer Dec 04 '13 at 05:38
  • ok i will make the necessary changes and post it soon – user3019552 Dec 04 '13 at 05:44

1 Answers1

1

Your 'main' probably looks different because you are not using SwingUtilities.invokeLater(Runnable doRun) ?

Well, to put it simply, you must use it always. So, modify your code and use this:

SwingUtilities.invokeLater(new Runnable(){
   @Override
   public void run(){
      // copy-paste your main() code
   }
});  

Also, why is your class named JFrame ? Refrain from using names that are already used by Java classes already.

An SO User
  • 24,612
  • 35
  • 133
  • 221
  • Like this **SwingUtilities.invokeLater(new Runnable(){ @Override public void run(){ JFrame frame = new JFrame(); frame.setTitle("Components File"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }** – user3019552 Dec 04 '13 at 05:36
  • @user3019552 Edit your question and post an update under the heading: `Update 1` but looks like you got it =) – An SO User Dec 04 '13 at 05:36