【问题标题】:GridBagLayout, adding space between JButtonsGridBagLayout,在JButtons之间添加空间
【发布时间】:2015-10-15 14:36:28
【问题描述】:

我试图在按钮之间添加一些空间是徒劳的,我尝试了很多技巧,如 emptyBorder 或胶水,但我没有成功。 我也无法解释“下一个”和“上一个”上的白色边框,所以我无法删除它

这就是我所拥有的:

这里是一个可运行的 sn-p :

package gridspace;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GridSpace extends JPanel {

   public static void main(String[] args) {
      GridSpace test = new GridSpace();
   }

   public GridSpace() {
      JFrame frame = new JFrame();
      frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setResizable(false);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      frame.setLayout(new BorderLayout());
      JPanel firstPanel = new JPanel();
      JPanel buttonPanel = new JPanel(new GridLayout(5, 4));
      JPanel buttonPanelOption = new JPanel();
      firstPanel.setBackground(Color.RED);
      firstPanel.setLayout(new GridBagLayout());

      JButton button[] = new JButton[20];
      JButton buttonNext = new JButton("Next");
      JButton buttonPrevious = new JButton("Previous");

      GridBagConstraints c = new GridBagConstraints();
      Insets insets = new Insets(10, 10, 10, 10);
      c.insets = insets;
      c.weightx = 1;
      c.gridwidth = 30;

      buttonNext.setPreferredSize(new Dimension(100, 20));
      buttonPrevious.setPreferredSize(new Dimension(100, 20));
      for (int i = 0; i < 20; i++) {
         button[i] = new JButton(Integer.toString(i + 1));
         buttonPanel.add(button[i]);
      }
      c.gridx = 3;
      c.gridy = 0;

      firstPanel.add(buttonPanel, c);

      buttonPanelOption.add(buttonPrevious);
      buttonPanelOption.add(buttonNext);

      c.gridx = 3;
      c.gridy = 1;

      firstPanel.add(buttonPanelOption, c);
      frame.getContentPane().add(firstPanel);
      frame.pack();

   }
}

【问题讨论】:

    标签: java swing jbutton layout-manager gridbaglayout


    【解决方案1】:

    这与 GridBagLayout 无关。而是改进您的 GridLayout 构造函数调用。变化:

    JPanel buttonPanel = new JPanel(new GridLayout(5, 4));
    

    // to give a 5 point gap around each item in the grid.
    JPanel buttonPanel = new JPanel(new GridLayout(5, 4, 5, 5)); 
    

    关于:

    我也无法解释“下一个”和“上一个”上的白色边框,所以我无法删除它

    那是因为持有它的 JPanel 是不透明的。将其设置为不透明,主容器的背景颜色会显示出来。

    例如:

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridLayout;
    import javax.swing.*;
    
    public class Test3 extends JPanel {
       private static final int BTN_COUNT = 20;
       private static final int GAP = 5;
       private static final Color BG = Color.RED;
    
       public Test3() {
          JPanel btnPanel = new JPanel(new GridLayout(0, 4, GAP, GAP));
          for (int i = 0; i < BTN_COUNT; i++) {
             btnPanel.add(new JButton(String.valueOf(i + 1)));
          }
          btnPanel.setOpaque(false);
    
          JPanel southPanel = new JPanel(new GridLayout(1, 0, GAP, 0));
          southPanel.add(new JButton("Previous"));
          southPanel.add(new JButton("Next"));
          southPanel.setOpaque(false);
    
          setBackground(BG);
          setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
          setLayout(new BorderLayout(GAP, GAP));
          add(btnPanel, BorderLayout.CENTER);
          add(southPanel, BorderLayout.PAGE_END);
       }
    
       private static void createAndShowGui() {
          JFrame frame = new JFrame("Test3");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new Test3());
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-12-28
      • 1970-01-01
      • 1970-01-01
      • 2017-12-17
      • 2022-11-11
      • 2013-07-12
      • 2018-06-15
      • 1970-01-01
      • 2017-06-06
      相关资源
      最近更新 更多