1

Is there a way to add objects to a JFrame without using Layout Manager? I have tile objects(for the game 2048) that I am trying to add to a JFrame so I can call the JFrame then have a loop forever where the tiles are forever repainting themselves and I can press arrows to make them move depending on constraints(like if it can move in a specific direction.

Why I don't want to use a specific layout manager - my objects are tiles in the game 2048- which means they are constantly changing position which would mess with the layout manager setup ie flowlayout where all the JPanel objects are in a specific order and position.

Heres where I am trying to instantiate the JFrame:

   public static void main(String[] args) throws InterruptedException {
       //set up JFrame, tile objects
       frame = new JFrame();
        a = new tile(100, 100, frame);
        b = new tile(200, 200, frame);
       frame.addKeyListener(a);
        frame.add(a); 
        frame.add(b);


        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);

               


        frame.setSize(500, 500);
        frame.setVisible(true); 
        //a loop so that it is continuously repainting and when i press a key something else happens
        while(true) {
            a.repaint();
            b.repaint();
            Thread.sleep(10);
        } 
4
  • have a loop forever - don't use an infinite loop. For animation you should use a Swing Timer. I can press arrows to make them move - how do you know which tile to move? Generally for a game where you have multiple objects to paint you would use custom painting. Check out: stackoverflow.com/questions/54028090/…. It is not a game, but it demonstrates a Swing Timer with animation. Commented Jun 27, 2021 at 14:56
  • @camickr I will fix that later but for now I am interested in how to add objects to JFRame thanks Commented Jun 27, 2021 at 17:55
  • Create a drawing JPanel and draw the 2048 squares on the drawing JPanel. Create a logical model of the values of your tiles, and draw the tiles on the drawing JPanel based on the logical model. If this is confusing, take a look at my 2048 game on GitHub. Commented Jun 27, 2021 at 22:25
  • ok thanks guys, will try Commented Jun 27, 2021 at 22:28

1 Answer 1

1

It is possible to work with Swing without LayoutManager. Not using a LayoutManager allows and requires the application to have full control on the absolute position of the components.

Check out these fine resources:

In a nutshell, creating a container without a layout manager involves the following steps:

  • Set the container's layout manager to null by calling setLayout(null).
  • Call the Component class's setBounds() method for each of the container's children.
  • Call the Component class's repaint() method.
Sign up to request clarification or add additional context in comments.

1 Comment

Just because you can do something, doesn't mean you should do something. Swing was designed to be used with layout managers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.