Adding a button anywhere in JPanel

Without using the Swing GUI on Eclipse, I tried to add a button in the JFrame anywhere in the frame (so there is no BorderLayout.CENTER ). I can not pass by:

 JPanel panel = new JPanel(); JButton OKButton = new JButton("OK"); OKButton.addActionListener(new MyAction()); panel.add(OKButton,BorderLayout.CENTER); 

So will something like this be completely redesigned or is there something I am missing?

EDIT: Anywhere (since I plan to add a few frames / labels to the frame), I meant, possibly, the coordinate in the frame. So, except for the dead center, (for example) 75% on the left and 25% down.

+4
source share
1 answer

You can use different panels with different LayoutMangers to arrange the GUI as you like.

Check out some common LayoutManagers: http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

Otherwise, you can use the null layout (panel.setLayout (null)) and place all components by setting the position. But I would recommend LayoutMangers to you.

+4
source

Source: https://habr.com/ru/post/1448039/


All Articles