简体   繁体   中英

How can I add JLabel and JTextField to only the bottom of the center of BorderLayout?

Is there any way too add JLabels and JTextField to only the bottom half of the center border layout. For aesthetic reasons I do not want anything on the upper half and have not found any way of doing this. I am still fairly new using swing and AWT... Any suggestions would be greatly appreciate. I added a picture of what I am trying to achieve.

例

Give the BorderLayout.CENTER JPanel a new GridLayout(2, 1) (for 2 rows, 1 column). Add a blank JLabel to the same JPanel for the top half blank space. Add another component, a JPanel, to the BorderLayout.CENTER JPanel and have it use a GridBagLayout for the bottom space, and then add your labels and fields using GridBagConstraints. The secret here is to nest JPanels, each using the layout manager of choice.

For example:

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.*;

public class Layouts {
   private static final int TF_COLS = 8;

   private static void createAndShowGui() {
      JPanel borderLayoutPanel = new JPanel(new BorderLayout());
      borderLayoutPanel.setBorder(BorderFactory
            .createTitledBorder("BorderLayout Panel"));

      borderLayoutPanel.add(createInnerPanel("North"), BorderLayout.PAGE_START);

      borderLayoutPanel.add(createInnerPanel("North"), BorderLayout.PAGE_START);
      borderLayoutPanel.add(createInnerPanel("South"), BorderLayout.PAGE_END);
      borderLayoutPanel.add(createInnerPanel("East"), BorderLayout.LINE_END);
      borderLayoutPanel.add(createInnerPanel("West"), BorderLayout.LINE_START);

      JPanel centerPanel = new JPanel(new GridLayout(2, 1));
      centerPanel.add(new JLabel());

      JPanel gblPanel = new JPanel(new GridBagLayout());
      gblPanel.add(new JLabel("Question 1:"), createGbc(0, 0));
      gblPanel.add(new JTextField(TF_COLS), createGbc(1, 0));
      gblPanel.add(new JLabel("Question 2:"), createGbc(0, 2));
      gblPanel.add(new JTextField(TF_COLS), createGbc(1, 2));

      centerPanel.add(gblPanel);

      borderLayoutPanel.add(centerPanel);

      JFrame frame = new JFrame("Layouts");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(borderLayoutPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   private static GridBagConstraints createGbc(int x, int y) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = x;
      gbc.gridy = y;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST;
      gbc.fill = GridBagConstraints.HORIZONTAL;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      int iGap = 5;
      gbc.insets = (x == 0) ? new Insets(iGap, iGap, iGap, 3 * iGap) : new Insets(
            iGap, iGap, iGap, iGap);
      return gbc;
   }

   private static JComponent createInnerPanel(String text) {
      JPanel panel = new JPanel();
      panel.add(new JLabel(text));
      panel.setBorder(BorderFactory.createEtchedBorder());
      return panel;
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

And yes, this is more initial work than simply setting bounds, but where you end up saving time and frustration is when you have to maintain this GUI later since if you use proper layouts, it will be much easier to debug, enhance or alter your GUI. It will also have a better chance of looking decent on all platforms and screen resolutions.

这可以通过EmptyBorder轻松完成:

centerPanel.setBorder(new EmptyBorder(top, left, bottom, right));

I usually use set bounds but feel free to look at other layouts like the gridbaglayout which allows you to make a grid like layout that you can put specific components in. Check out this link to see what it looks like. https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM