I've been searching in google how to build something that looks like in the image. Text boxes, labels, buttons with actions and a graphic area all in one JFrame, but I only can find them separated. I mean, I understand how to build them separated but no how to put them together. I've been reading about using GridBagConstraints but is very confusing. I would appreciate any tip.
how to build a JFrame window with a graphic area paintComponent with JTextField, JButton and JLabel?
Asked
Active
Viewed 27 times
0
-
1*but no how to put them together.* - use a panel to logically group components. In your case you might have one panel for the components on the top. A JPanel uses a FlowLayout, so you will see then all in one line. The top panel is added to the BorderLayout.PAGE_START and the graph panel to the BorderLayout.CENTER of the frame. Read the Swing tutorial on [Layout Managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) for working examples. *but is very confusing* - is not a question. What part of the demo code in the tutorial do you not understand. – camickr Nov 21 '20 at 22:01
-
The center JPanel is a drawing JPanel. See the Oracle tutorial [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) to see a working example of building a drawing JPanel. – Gilbert Le Blanc Nov 22 '20 at 02:44
1 Answers
0
The easiest way is to use the BorderLayout and include your panels ("separated components") at the regions (north, south, east, west, or center) you want.
JFrame frame = new JFrame();
JPanel actions = new JPanel();
JPanel graph = new JPanel();
frame.setLayout(new BorderLayout());
frame.getContentPane().add(actions, BorderLayout.SOUTH);
frame.getContentPane().add(graph, BorderLayout.CENTER);

mrgatto
- 71
- 1
- 4