Java swing put a label on top button năm 2024
I need to display the Down button on top of the JscrollPane-JTable to scroll to the last cell like in the messengers Show I tried to use OverlayLayout, but when I add code popupPanel is not displayed How to force popupPanel to be displayed over JScrollPane? all code By creating one or more lightweight containers that use Box.createRigidArea(size) 3, you can achieve some layouts for which the more complex Box.createRigidArea(size) 9 is often used. Box.createRigidArea(size) 3 is also useful in some situations where you might consider using Box.createHorizontalGlue() 1 or Box.createHorizontalGlue() 2. One big difference between Box.createRigidArea(size) 3 and many earlier layout managers is that Box.createRigidArea(size) 3 respects each component's maximum size and X/Y alignment. We'll discuss that later. The following figure shows a GUI that uses two instances of Box.createRigidArea(size) 3. In the top part of the GUI, a top-to-bottom box layout places a label above a scroll pane. In the bottom part of the GUI, a left-to-right box layout places two buttons next to each other. A Box.createHorizontalGlue() 2 combines the two parts of the GUI and ensures that any excess space is given to the scroll pane. You can find links for running ListDialog and for its source files in the for Using Swing Components.[PENDING: add boxes and callouts: Draw a box that includes the buttons (aligned with the buttons' top edges, even with the inside edge of the window-frame on the bottom, and stretching between the inside edges of the window-frame from side to side; in other words, 0 pixels above the buttons, 10 pixels below and to the right of the buttons, and 10 pixels further left than the list). Label this box "left-to-right box layout". Draw a second box that has the same lower borders as the first box and extends up to the window-frame's inner edge. Label this box "top-to-bottom box layout".] The following code, taken from Box.createHorizontalGlue() 7 , lays out the GUI. This code is in the constructor for the dialog, which is implemented as aBox.createHorizontalGlue() 8 subclass. The bold lines of code set up the box layouts and add components to them. JScrollPane listScroller = new JScrollPane(list); The first bold line creates a top-to-bottom box layout and sets it up as the layout manager for Box.createHorizontalGlue() 9. The two arguments to the Box.createRigidArea(size) 3 constructor are the container that it manages and the axis along with the components will be laid out. The next three bold lines add the label and scroll pane to the container, separating them with a rigid area — an invisible lightweight component used to add space between components. In this case, the rigid area has no width and puts exactly 5 pixels between the label and scroll pane. Rigid areas are discussed later, in . The next chunk of bold code creates a left-to-right box layout and sets it up for the Box.createVerticalGlue() 1 container. Then the code adds two buttons to the container, using a rigid area to put 10 pixels between the buttons. To place the buttons at the right side of their container, the first component added to the container is glue. This glue is an invisible lightweight component that grows as necessary to absorb any extra space in its container. Glue is discussed in . As an alternative to using invisible components, you can sometimes use empty borders to create space around components. For example, the preceding code snippet uses empty borders to put 10 pixels between all sides of the dialog and its contents, and between the two parts of the contents. Borders are completely independent of layout managers. They're simply how Swing components draw their edges. See How to Use Borders for more information.The following sections discuss Box.createRigidArea(size) 3 in more detail: Don't let the length of the Box.createRigidArea(size) 3 discussion scare you! You can probably use Box.createRigidArea(size) 3 with the information you already have. If you run into trouble or you want to take advantage of Box.createRigidArea(size) 3's power, read on. Box Layout FeaturesAs we said before, Using Invisible Components as FillerEach component controlled by a box layout butts up against its neighboring components. If you want to have space between components, you can either add an empty border to one or both components, or insert invisible components to provide the space. You can create invisible components with the help of the Fixing Alignment ProblemsTwo types of alignment problems sometimes occur with Specifying Component SizesAs we mentioned before, If you're running into trouble with a box layout and you've ruled out alignment problems, then the trouble might well be size-related. For example, if the container controlled by the box layout is taking up too much space, then one or more of the components in the container probably needs to have its maximum size restricted. You can use two techniques to track down size trouble in a box layout: Add a garish line border to the outside of the Swing components in question. This lets you see what size they really are. For example:Box.createRigidArea(size)Use good old button1.setAlignmentY(Component.BOTTOM_ALIGNMENT); button2.setAlignmentY(Component.BOTTOM_ALIGNMENT); 5 to print the components' minimum, preferred, and maximum sizes, and perhaps their bounds. The Box Layout API[PENDING: We will make the left columns of the following tables have links to the API doc.] |