I have this code that loads 5 images and puts them into the frame using FlowLayout:
public class Main
{
private static final int verticalGap=50;
private static final int horizontalGap=30;
private static final int width= 800;
private static final int height= 800;
public static void main(String[] args)
{
FlowLayout layout=new FlowLayout(FlowLayout.LEADING,horizontalGap,verticalGap);
JButton button= new JButton("Discard");
ImagePanel[] panels= new ImagePanel[5];
Deck deck= new Deck();
JFrame frame= new JFrame("Poker");
frame.setSize(width, height);
frame.setLayout(layout);
frame.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
deck.mix();
for(int i=0; i<5; i++)
{
panels[i]= new ImagePanel();
panels[i].setImage(deck.getCard(i));
frame.getContentPane().add(panels[i]);
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
The code loads 5 cards and spot them correctly.
But the problem is that now I want to place a button to the frame.This button should be placed approximately in the center of the screen, but if I add it to the pane, the button is placed near the other panels, using the horizontal gap that the flow layout has set.
How do I place it in an absolute position without altering the position of the panels (so I want 5 panels to be added using flow layout, and one button to be added in an absolute position).
You can’t mix absolute-layout with a LayoutManager.
In this case:
Here is the code corresponding to that solution (but I could not test it because I don’t have your other classes).