Motix
Motix

Reputation: 3

What are the new commands for JFrames?

I'm learning Java since about 4 weeks, and always found some code to copy and play around with, that's how I learned. I wanted to do something with GUIs, but everything I found was from 2007 and didn't work in eclipse, when I copied it in. Furthermore, I know that you probably shouldn’t just copy things from the Internet, but could someone send a piece of code (context does not matter) so I can see what the new code is? The code I have right now looks like this:

package GUIs;
public class JFrame {


    public static void main(String[] args) {
        JFrame frm = new JFrame();
        frm.setTitle("JFrame mit setSize()");

         
       frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frm.setSize(300,200);
        frm.setLocation(50,50);
        frm.setVisible(true);

    }

}

Thanks.

Upvotes: 0

Views: 38

Answers (1)

vincenzopalazzo
vincenzopalazzo

Reputation: 1645

How one of the comment said, your code has an only syntax error, unfortunately when you copy some java code from the web some time the code does not include all information about that.

How your code should look like?

package io.app.gui;
 
import javax.swing.JFrame  

public class MyFrame extends JFrame { 


    public static void main(String[] args) {
        MyFrame frm = new MyFrame();
        frm.setTitle("JFrame mit setSize()");

         
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setSize(300,200);
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);

    }

}
  1. Usually the name is written in lower case
  2. Usually there is the Main class and also another class that extends the JFrame, is not common to create a JFrame class inside the main directly, also because you can do customization to your JFrame. Es: add a JMenuBar and if you create a class that extends JFrame, it looks much better

In addition, I want to add a couple of things.

  • There is a new API from Swing, so in other words, Swing has not changed in the API during the time, it is also one of the good things that Java has from until java 8. There is one change of UI technology in Java from AWT to Swing, but swing is a child of AWT.

  • If you are things that Swing look like an old App, please search on the web there are a couple of good open-source project that makes Swing much modern without change API. Such as material-ui-swing where I'm the maintainer of this library, but there are others around the web

Upvotes: 1

Related Questions