java JPanel 中按钮的居中对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16267883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:28:14  来源:igfitidea点击:

Center alignment of buttons in JPanel

javaswingjpanel

提问by Hovercraft Full Of Eels

How do I align the buttons (S, S, D & D and A, U, D & d) to the middle of the panels (the blue and red)? The panels can grow (height not width), but the buttons should stay in the middle and keep the same size. So GridLayout is out.

如何将按钮(S、S、D 和 D 以及 A、U、D 和 d)与面板的中间(蓝色和红色)对齐?面板可以增长(高度而不是宽度),但按钮应该保持在中间并保持相同的大小。所以 GridLayout 出来了。



    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTable;

    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;

    import javax.swing.JButton;
    import java.awt.BorderLayout;
    import java.awt.Color;


    public class TestFrame extends JFrame {
        /**
         * 
         */
        private static final long serialVersionUID = 5671798241966272024L;

        public TestFrame() {

            FlowLayout layout = new FlowLayout();

            JButton moveUpButton = new JButton("U");
            JButton moveAllUpButton = new JButton("A");

            JButton moveDownButton = new JButton("D");
            JButton moveAllDownButton = new JButton("D");

            JButton selectButton = new JButton("S");
            JButton selectAllButton = new JButton("S");

            JButton deselectButton = new JButton("D");
            JButton deselectAllButton = new JButton("D");


            setSize(600, 400);

            getContentPane().setLayout(new GridLayout());


            JPanel selectButtonsPanel = new JPanel();
            selectButtonsPanel.setBackground(Color.BLUE);

            selectButtonsPanel.setLayout(layout);
            selectButtonsPanel.setPreferredSize(new Dimension(50, 0));
            selectButtonsPanel.add(selectAllButton);
            selectButtonsPanel.add(selectButton);
            selectButtonsPanel.add(deselectButton);
            selectButtonsPanel.add(deselectAllButton);

            JPanel moveButtonsPanel = new JPanel(layout);
            moveButtonsPanel.setBackground(Color.RED);
            moveButtonsPanel.setLayout(layout);
            moveButtonsPanel.setPreferredSize(new Dimension(50, 0));
            moveButtonsPanel.add(moveAllUpButton);
            moveButtonsPanel.add(moveUpButton);
            moveButtonsPanel.add(moveDownButton);
            moveButtonsPanel.add(moveAllDownButton);


            JPanel sourcePanel = new JPanel ();
            sourcePanel.setLayout(new BorderLayout());
            sourcePanel.add(new JTable(), BorderLayout.CENTER);
            sourcePanel.add(selectButtonsPanel, BorderLayout.EAST);


            JPanel destinationPanel = new JPanel ();
            destinationPanel.setLayout(new BorderLayout());
            destinationPanel.add(new JTable(), BorderLayout.CENTER);
            destinationPanel.add(moveButtonsPanel, BorderLayout.EAST);


            getContentPane().add (sourcePanel);
            getContentPane().add(destinationPanel);


        }

    }

回答by Hovercraft Full Of Eels

Consider nesting JPanels each using their own layout. The BoxLayout could allow you to center your inner JPanel that holds the JButtons. The inner panel uses GridLayout and the BoxLayout using outer JPanel has glue added to the top and bottom:

考虑使用自己的布局嵌套 JPanel。BoxLayout 可以让您将包含 JButton 的内部 JPanel 居中。内部面板使用 GridLayout,而使用外部 JPanel 的 BoxLayout 在顶部和底部添加了胶水:

    JPanel innerSelectPanel = new JPanel(new GridLayout(0, 1, 0, 5));
    // innerSelectPanel.setPreferredSize(new Dimension(50, 0));
    innerSelectPanel.add(selectAllButton);
    innerSelectPanel.add(selectButton);
    innerSelectPanel.add(deselectButton);
    innerSelectPanel.add(deselectAllButton);
    innerSelectPanel.setOpaque(false);
    innerSelectPanel.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2));
    selectButtonsPanel.setLayout(new BoxLayout(selectButtonsPanel, BoxLayout.PAGE_AXIS));
    selectButtonsPanel.add(Box.createVerticalGlue());
    selectButtonsPanel.add(innerSelectPanel);
    selectButtonsPanel.add(Box.createVerticalGlue());

For example:

例如:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class TestFrame2 extends JPanel {
   public static final String[] COLS = { "One", "Two", "Three" };
   public static final String[][] BTN_LABELS = { { "S", "S", "D", "D" },
         { "A", "U", "D", "D" } };

   public TestFrame2() {
      setLayout(new GridLayout(1, 0));
      for (int i = 0; i < 2; i++) {
         add(createPanel(i));
      }
   }

   private JPanel createPanel(int row) {
      int gap = 3;
      JPanel btnPanel = new JPanel(new GridLayout(0, 1, 0, gap));
      btnPanel.setBorder(BorderFactory.createEmptyBorder(0, gap, 0, gap));
      btnPanel.setOpaque(false);
      for (int i = 0; i < BTN_LABELS[row].length; i++) {
         JButton btn = new JButton(BTN_LABELS[row][i]);
         btnPanel.add(btn);
      }
      btnPanel.setMaximumSize(btnPanel.getPreferredSize());

      JPanel rightPanel = new JPanel();
      rightPanel.setBackground(Color.red);
      rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.PAGE_AXIS));
      rightPanel.add(Box.createVerticalGlue());
      rightPanel.add(btnPanel);
      rightPanel.add(Box.createVerticalGlue());

      JPanel panel = new JPanel(new BorderLayout());
      panel.add(new JScrollPane(new JTable(new DefaultTableModel(COLS, 5))),
            BorderLayout.CENTER);
      panel.add(rightPanel, BorderLayout.LINE_END);
      return panel;
   }

   private static void createAndShowGui() {
      TestFrame2 mainPanel = new TestFrame2();

      JFrame frame = new JFrame("TestFrame2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

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