Reputation: 19
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);
}
Upvotes: 1
Views: 226
Reputation: 9374
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:
Upvotes: 1