本文介绍了GridLayout没有填充JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到GridLayout问题而整个JPanel没有被填充。我有一个N * M网格,我用N * M Tiles填充它(它们扩展了JPanel)。添加所有这些图块后,JPanel的底部和右侧仍有空间。我认为GridLayout应该填满整个JPanel,并使其中的所有内容均匀分布?



编辑:我不想发布所有代码,因为有多个班级。想想也许有一个简单的解决方案。

 公共类MainFrame扩展JFrame {

private static final long serialVersionUID = 3985493842977428355L;
private final int FRAME_HEIGHT = 800;
private final int FRAME_WIDTH = 700;

私人MainPanel主面板;

public MainFrame(){
super(Test);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(FRAME_HEIGHT,FRAME_WIDTH);
setLocationRelativeTo(null);
mainPanel = new MainPanel();
getContentPane()。add(mainPanel);
setVisible(true);
}
}

公共类MainPanel扩展JPanel {
/ **
*
* /
私有静态最终长serialVersionUID = 3421071253693531472L;

私人RoutePanel routePanel;
私有ControlPanel controlPanel;
private GridBagConstraints gridBagConstraints;
private GridBagLayout gridBagLayout;

public MainPanel(){
routePanel = new RoutePanel();
controlPanel = new ControlPanel();
setBackground(Color.black);
gridBagLayout = new GridBagLayout();
setLayout(gridBagLayout);
gridBagConstraints = new GridBagConstraints();
createLayout();
}

private void createLayout(){
gridBagConstraints.weightx = 1;
gridBagConstraints.weighty = 1;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.anchor = GridBagConstraints.NORTH;
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagLayout.setConstraints(routePanel,gridBagConstraints);
add(routePanel);

gridBagConstraints.weightx = 1;
gridBagConstraints.weighty = .05;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = GridBagConstraints.SOUTH;
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagLayout.setConstraints(controlPanel,gridBagConstraints);
add(controlPanel);
}
}

公共类RoutePanel扩展JPanel {
/ **
*
* /
私有静态最终长serialVersionUID = 4366703088208967594L;
private final int GRID_ROWS = 30;
private final int GRID_COLS = 47;


public RoutePanel(){
setLayout(new GridLayout(GRID_ROWS,GRID_COLS,0,0));
for(int i = 0; i< GRID_ROWS * GRID_COLS; i ++){
add(new Tile());
}
setBackground(Color.orange);
}
}

public ControlPanel(){

}

public Tile(){
setBackground (Color.gray);
边界黑线;
blackline = BorderFactory.createLineBorder(Color.black);
setBorder(blackline);
}


解决方案

由于你没有发帖代码,我们被迫猜测(这不好)。



我的猜测:您可能正在设置某些组件或GUI的大小或首选大小,这会导致GUI边缘出现间隙。 / p>

解决方案:不要这样做。当在GUI上调用 pack()时,让组件通过其首选大小和布局管理器自行调整大小。



如需更多有用的帮助,请创建并发布您的。对于这样的问题,代码确实是强制性的,实际上我没有发布你的代码,我有点惊讶。






编辑1



例如,请注意GUI生成的差异:

  import java.awt.Color; 
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing。*;

公共类GapExample {
private static final int M = 5;
private static final int N = 6;
private static final int PREF_W = 700;
private static final int PREF_H = 500;

@SuppressWarnings(serial)
private static void createAndShowGui(){

// ***尝试1:设置mainPanel JPanel的preferredSize ** *
JPanel mainPanel = new JPanel(new GridLayout(M,N));
mainPanel.setBorder(BorderFactory.createLineBorder(Color.red));
mainPanel.setPreferredSize(new Dimension(PREF_W,PREF_H));

for(int i = 0; i< M; i ++){
for(int j = 0; j< N; j ++){
String text = String .format(Foo [%d,%d],i,j);
JLabel label = new JLabel(text,SwingConstants.CENTER);
label.setBorder(BorderFactory.createLineBorder(Color.blue));
mainPanel.add(label);
}
}
JOptionPane.showMessageDialog(null,mainPanel,
尝试1,mainPane的setPreferredSize,
JOptionPane.PLAIN_MESSAGE);

// ***尝试2:覆盖
//网格中JLabel单元格的getPreferredSize ***
mainPanel = new JPanel(新GridLayout(M,N) ));
mainPanel.setBorder(BorderFactory.createLineBorder(Color.red));
final Dimension labelPrefSize = new Dimension(PREF_W / N,PREF_H / M);
for(int i = 0; i< M; i ++){
for(int j = 0; j< N; j ++){
String text = String.format( Foo [%d,%d],i,j);
JLabel label = new JLabel(text,SwingConstants.CENTER){
@Override
public Dimension getPreferredSize(){
return labelPrefSize;
}
};
label.setBorder(BorderFactory.createLineBorder(Color.blue));
mainPanel.add(label);
}
}
JOptionPane
.showMessageDialog(null,mainPanel,
尝试2,覆盖网格中JLabel单元格的getPreferredSize,
JOptionPane .PLAIN_MESSAGE);
}

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

在第二个对话框中,我让JLabels返回一个首选大小,让容纳它们的容器设置为显示GUI的最佳大小,并且您将进行第二次尝试,网格适合更好。



此显示对于第一次(坏)尝试内部和外部组件之间的差距:





第二次(好)尝试没有显示这样的差距:




I'm having a problem with GridLayout and the entire JPanel not being filled. I have a N * M Grid, and I'm filling it with N * M Tiles (They extend JPanel). After adding all these tiles there is still space on the bottom and the right side of the JPanel. I thought GridLayout was supposed to fill up the entire JPanel, and make everything in it evenly sized?

Edit: I didn't want to post all the code since there are multiple classes. Thought maybe there was a simple solution.

public class MainFrame extends JFrame {

    private static final long serialVersionUID = 3985493842977428355L;
    private final int FRAME_HEIGHT = 800;
    private final int FRAME_WIDTH = 700;

    private MainPanel mainPanel;

    public MainFrame() {
        super("Test");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(FRAME_HEIGHT, FRAME_WIDTH);
        setLocationRelativeTo(null);
        mainPanel = new MainPanel();
        getContentPane().add(mainPanel);
        setVisible(true);
    }
}

public class MainPanel extends JPanel {
    /**
     *
     */
    private static final long serialVersionUID = 3421071253693531472L;

    private RoutePanel routePanel;
    private ControlPanel controlPanel;
    private GridBagConstraints gridBagConstraints;
    private GridBagLayout gridBagLayout;

    public MainPanel() {
        routePanel = new RoutePanel();
        controlPanel = new ControlPanel();
        setBackground(Color.black);
        gridBagLayout = new GridBagLayout();
        setLayout(gridBagLayout);
        gridBagConstraints = new GridBagConstraints();
        createLayout();
    }

    private void createLayout() {
        gridBagConstraints.weightx = 1;
        gridBagConstraints.weighty = 1;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = GridBagConstraints.NORTH;
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagLayout.setConstraints(routePanel, gridBagConstraints);
        add(routePanel);

        gridBagConstraints.weightx = 1;
        gridBagConstraints.weighty = .05;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = GridBagConstraints.SOUTH;
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagLayout.setConstraints(controlPanel, gridBagConstraints);
        add(controlPanel);
    }
}

public class RoutePanel extends JPanel{
    /**
     *
     */
    private static final long serialVersionUID = 4366703088208967594L;
    private final int GRID_ROWS = 30;
    private final int GRID_COLS = 47;


    public RoutePanel() {
        setLayout(new GridLayout(GRID_ROWS, GRID_COLS, 0, 0));
        for(int i = 0; i < GRID_ROWS * GRID_COLS; i++) {
            add(new Tile());
        }
        setBackground(Color.orange);
    }
}

public ControlPanel() {

    }

public Tile() {
        setBackground(Color.gray);
        Border blackline;
        blackline = BorderFactory.createLineBorder(Color.black);
        setBorder(blackline);
    }
解决方案

Since you're not posting code, we are forced to guess (and this is not good).

My guess: you may be setting the size or the preferred sizes of some of your components or the GUI, and this is causing gaps at the edges of your GUI.

Solution: don't do this. Let the components size themselves via their preferred sizes and the layout managers when pack() is called on the GUI.

For more helpful help, create and post your sscce. For a question like this, code is really mandatory, and I'm a bit surprised actually that you didn't post yours.


Edit 1

For example, note the difference in the GUI's produced:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.*;

public class GapExample {
   private static final int M = 5;
   private static final int N = 6;
   private static final int PREF_W = 700;
   private static final int PREF_H = 500;

   @SuppressWarnings("serial")
   private static void createAndShowGui() {

      // *** attempt 1: set preferredSize of the mainPanel JPanel ***
      JPanel mainPanel = new JPanel(new GridLayout(M, N));
      mainPanel.setBorder(BorderFactory.createLineBorder(Color.red));
      mainPanel.setPreferredSize(new Dimension(PREF_W, PREF_H));

      for (int i = 0; i < M; i++) {
         for (int j = 0; j < N; j++) {
            String text = String.format("Foo [%d, %d]", i, j);
            JLabel label = new JLabel(text, SwingConstants.CENTER);
            label.setBorder(BorderFactory.createLineBorder(Color.blue));
            mainPanel.add(label);
         }
      }
      JOptionPane.showMessageDialog(null, mainPanel,
            "Attempt 1, setPreferredSize of mainPane",
            JOptionPane.PLAIN_MESSAGE);

      // *** attempt 2: override the getPreferredSize of the JLabel cells in the
      // grid ***
      mainPanel = new JPanel(new GridLayout(M, N));
      mainPanel.setBorder(BorderFactory.createLineBorder(Color.red));
      final Dimension labelPrefSize = new Dimension(PREF_W / N, PREF_H / M);
      for (int i = 0; i < M; i++) {
         for (int j = 0; j < N; j++) {
            String text = String.format("Foo [%d, %d]", i, j);
            JLabel label = new JLabel(text, SwingConstants.CENTER) {
               @Override
               public Dimension getPreferredSize() {
                  return labelPrefSize;
               }
            };
            label.setBorder(BorderFactory.createLineBorder(Color.blue));
            mainPanel.add(label);
         }
      }
      JOptionPane
            .showMessageDialog(null, mainPanel,
                  "Attempt 2, override getPreferredSize of the JLabel cells in the grid",
                  JOptionPane.PLAIN_MESSAGE);
   }

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

In the second dialog, I have the JLabels return a preferred size and let the containers that hold them set the best size to display the GUI, and you'll with the second attempt, the grid fits much better.

This displays for the first (bad) attempt a gap between the inner and outer components:

And the second (good) attempt shows no such gap:

这篇关于GridLayout没有填充JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 01:26