0

I need to use Custom Locations for my JFrame components, I have tried looking in Java's Document about Using the insets object for making a custom location but i dont really understand that well...

if you got any ways to add components in custom locations or a good tutorial/web/other that i can easily learn how to use custom locations.

davethecoder
  • 3,856
  • 4
  • 35
  • 66
  • Possible duplicate of [set custom location for a component in box layout](https://stackoverflow.com/questions/36227097/set-custom-location-for-a-component-in-box-layout) – ParkerHalo Sep 26 '17 at 09:16
  • Start with [How to use layout managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/layoutlist.html) - Don't get caught by the idea that pixel perfect (or null) layouts are easier, they aren't by a long shot - Take the time to learn how to use the layout managers and save yourself a lot of head aches – MadProgrammer Sep 26 '17 at 09:33

1 Answers1

0

if you haven't tried null layout, then check out this code, may be of help

public static void main(String[] args) {
    SwingUtilities.invokeLater(NullLayout::new);
}

NullLayout() {
    JFrame frame = new JFrame("Basket Game");
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

    for (int i = 0; i < 4; i++) {
        JPanel strip = new JPanel();
        strip.setMaximumSize(new Dimension(Integer.MAX_VALUE, 50));
        strip.setBorder(BorderFactory.createTitledBorder("Strip " + i));
        strip.add(new JLabel("Strip " + i));
        mainPanel.add(strip);
    }

    JPanel gamearea = new JPanel();
    gamearea.setLayout(null);
    mainPanel.add(gamearea);

    for (int i = 0; i < 5; i++) {
        int x = i * 100, y = i * 100;
        JPanel basket = new JPanel();
        basket.setSize(200, 50);
        basket.setLocation(x, y);
        basket.setBackground(Color.YELLOW);
        basket.add(new JLabel("x = " + x + ", y = " + y));
        gamearea.add(basket);
    }

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(mainPanel);
    frame.pack();
    frame.setResizable(false);
    frame.setSize(600, 600);

    frame.setVisible(true);
}

}

Stevoski
  • 11
  • 4