0

I am new to Java and I've been trying to setup a frame, but my code does not work, either because the code is just wrong, or because there is something wrong with my software. I am using Eclipse.

So this is my code:

package Frame;

import javax.swing.JFrame;


public class App {


 class FrameApp extends JFrame {

     public static void main(String[] args) {

        JFrame frame = new JFrame("FirstFrame");
        frame.setTitle("MFF");
        frame.setSize(300, 700);
        frame.setVisible(true);

     }  
  }

}

it returns

The method main cannot be declared static; static methods can only be declared in a static or top level type

Alwe17
  • 25
  • 5
  • 1
    Possible duplicate of [Java: when to use static methods](http://stackoverflow.com/questions/2671496/java-when-to-use-static-methods) – Grzegorz Górkiewicz Apr 01 '17 at 23:02
  • Try to move `main` function directly to the App class. – Matt Apr 01 '17 at 23:05
  • Get rid of the line: "class FrameApp extends JFrame {" and one closing bracket of the bottom. You do not need the FrameApp class in this example. Your frame object is the JFrame. – Costis Aivalis Apr 01 '17 at 23:12

2 Answers2

2

the problem is that you are declaring the main method in an inner class. that's what the thrown exception means. This will work for you.

package Frame;

import javax.swing.JFrame;

class FrameApp extends JFrame{
public FrameApp(String name){
super(name);
}
} 


public class App {


 public static void main(String[] args) {

    FrameApp frame = new FrameApp("FirstFrame");
    frame.setTitle("MFF");
    frame.setSize(300, 700);
    frame.setVisible(true);

 }  

}

Enjoy! :)

Rafik
  • 395
  • 1
  • 2
  • 12
0

You can do it this way, which is not extending JFrame.

import javax.swing.JFrame;

public class App{
    public App() {
        JFrame frame = new JFrame("FirstFrame"); // This sets the title
        frame.setTitle("MFF"); // Then you're resetting the title
        frame.setSize(300, 700);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
    public static void main(String[] args) {
        new App();
    }

}

Or you can extend it

import javax.swing.JFrame;

public class App extends JFrame{
    public App() {
        // When you extend JFrame you're essentially creating a JFrame
        // This gets into OOP (which you of course need to learn)
        // Since you've extended the JFrame you can directly access its methods 
        // like setTitle, setSize....
        setTitle("MFF"); 
        setSize(300, 700);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
    public static void main(String[] args) {
        new App();
    }

}

I'm definitely not very experienced with Swing, but when choosing one approach over the other. I choose not to extend the Frame. And to answer your question, there's no reason to put another class within the App class when just creating this simple JFramethat is why I changed to the above.

XXIV
  • 348
  • 1
  • 5
  • 24