Component positioning (how to place several buttons on the central screen of the same size)

A solid, recommended way to place components exactly where a JFrame is required?

In my case, I have a simple Jframe acting like a menu. The frame consists of a label at the top and three buttons in the middle. Using a free design, the 3 buttons seem to float above the frame, and when I start the project, they switch to ugly positions. Only when the three buttons are dragged to the maximum length (on each side of the frame) will they behave as “locked in place”. This is very ugly. I would like the 3 buttons to be centered in the middle of the frame.

I am using Netbeans. Free design is what I use, but it is only good when there are many components so that they can “snap” to each other's positions. With the help of other job layout managers, hasent worked. How will someone cope with such a problem with experience?

I am interested in thorough training. If this question is painfully amateurish, can someone at least recommend a textbook or other source for teaching the GUI? (I've already twice "Objects first with java", and they have only basic explanations for building a graphical interface).

Thanks for any direction.

0
source share
2 answers

The thing is pretty simple, but of course you have to search a little more yourself.

By the way, you have a problem with layout control and button size.

Please forget with any layout creator, because they generate confusion code, and it’s not good to understand what is really happening.

Usually for beginners I’m talking about a specific layout, that is, BoxLayout [edit: of course, there is a much simpler way to achieve the goal, this answer has a learning goal and wants to be big enough to be understood, and not just “here, make this copy” )

First of all, we need to know how the box layout works. It has two types of AXIS (this is how you added the components located on top) (PAGE_AXIS) and from left to right (LINE_AXIS). Using this two axes and a nested different layout, you can do whatever you want.

The first thing to know is to insert more layout.

To insert the layout, we will work with JPanel, each with a different axis, which will be placed one inside the other.

Begin your case study. You have an inscription above the three buttons in the center of the panel. We can start from the inside panel, with Components; we need PAGE_AXIS (top to botton), which will include:

// +---------------------+ // | white space | // | | // + - - - - - - - - - - + // |JLABEL JLABEL JLABEL| // + - - - - - - - - - - + // | white space | // |_____________________| // | button | // |---------------------| // |_____________________| // | button | // |---------------------| // |_____________________| // | button | // |---------------------| // | white space | // +---------------------+ 

As you can see, the panel is suitable for matching the width of the components, the trick is that BoxLayout prefers to give the component the maximum size. In this case, there are no fields, so we need an external other JPanel with LINE_AXIS to place the left and right fields, the result will be like this:

 // +---+---------------------+---+ // | | white space | | // | | | | // | + - - - - - - - - - - + | // | W |JLABEL JLABEL JLABEL| W | // | H + - - - - - - - - - - + H | // | I | white space | I | // | T _____________________ T | // | E | button | E | // | --------------------- | // | S _____________________ S | // | P | button | P | // | A --------------------- A | // | C _____________________ C | // | E | button | E | // | --------------------- | // | | white space | | // +-----------------------------+ 

So, the first thing we need to know is a hot layout setting for each panel.

 // Initializing JPanel outside = new JPanel(); JPanel inside = new JPanel(); // setting a layout with horizontal alignment outside.setLayout(new BoxLayout(outside, BoxLayout.LINE_AXIS)); // setting a layout with vertical alignment inside.setLayout(new BoxLayout(inside, BoxLayout.PAGE_AXIS)); 

After that we have to fill in the panel. Starting from the side. An outsider needs (looking at my image) first a horizontal space, then an inner panel, then another horizontal space. I keep adding them.

 // create an horizontal space of 20px outside.add(Box.createHorizontalStrut(20)); outside.add(inside); outside.add(Box.createHorizontalStrut(20)); 

Now we go inside, we need a big white space, a shortcut, still white, a button, a little white, a button, a little white, a third button and a big white. I continue to fill the inner panel with this.

 // create a vertical space of 20px inside.add(Box.createVerticalStrut(20)); JLabel title = new JLabel("THE TITLE"); inside.add(title); inside.add(Box.createVerticalStrut(20); JButton btt1 = new JButton("BUTTON ONE"); // create a new dimension object Dimension d = new Dimension(200,40); // set the four kind of size, the button CANNOT be different than the dimension I choose btt1.setSize(d); btt1.setMinimumSize(d); btt1.setMaximumSize(d); btt1.setPreferredSize(d); JButton btt2 = new JButton("BUTTON TWO"); btt2.setSize(d); btt2.setMinimumSize(d); btt2.setMaximumSize(d); btt2.setPreferredSize(d); JButton btt3 = new JButton("BUTTON THREE"); btt3.setSize(d); btt3.setMinimumSize(d); btt3.setMaximumSize(d); btt3.setPreferredSize(d); // Now that the button are ready we put them in the panel. inside.add(btt1); inside.add(Box.createVerticalStrut(5)); inside.add(btt2); inside.add(box.createVerticalStrut(5)); inside.add(btt3); // Last white space, the bottom margin: inside.add(Box.createVerticalStrut(20)); 

Now I have a good structure. Just make it visible and all that he has done. Of course, you need to put it in a JFrame or JDialog, and, of course, the first panel can be JFrame or JDialog, because BoxLayout can be installed for any Component.

In this basic tutorial, I hope you understand the basics of programming such structures. But you will need to read this to continue and complicate the situation: http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html http://docs.oracle.com/javase/tutorial/uiswing/ layout / box.html

have a nice day.

+5
source

The layouts are incredibly powerful and time-saving ... Coming from other languages ​​such as Delphi and VB, I can tell you that one of the best features of Swing is layout managers.

enter image description here

 public class TestLayout06 { public static void main(String[] args) { new TestLayout06(); } public TestLayout06() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException ex) { } catch (InstantiationException ex) { } catch (IllegalAccessException ex) { } catch (UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new MenuPanel()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } protected class MenuPanel extends JPanel { public MenuPanel() { JLabel label = new JLabel("Menu"); setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; add(label, gbc); gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridy++; add(new JButton("Option 1"), gbc); gbc.gridy++; add(new JButton("Option 2"), gbc); gbc.gridy++; add(new JButton("Option 3"), gbc); } } } 

Take a look at

+4
source

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


All Articles