java - 在 Swing 中设置 GridLayout 的 JPanel 的边距

标签 java swing layout-manager grid-layout

enter image description here 我正在尝试设置 GridLayout 内的 JPanel 的边距,引用 JFrame,但我没有找到使用其他答案的解决方案。我不知道这是否是一个重要的问题,而且在我用鼠标转到每个按钮之前它只显示第一个按钮。 该图像是一个示例,我想将 JPanel 设置为从图像网格的角开始,因为图像有边框(不是来自代码,而是来自装饰板),蓝色方 block 是 GridView 内的按钮,但我试图使用 set 属性(使用像素比例)使 gridView 适合图像绘制网格。

public class Gui extends JPanel implements View {
  private final JPanel gui = new JPanel(new BorderLayout(3, 3));
  private JButton[][] chessBoardSquares = new JButton[5][5];
  private JPanel chessBoard;
  private ImageIcon ArrayWithoutPlayer[] = new ImageIcon[7]; //{1,2,3,4,10,11,12}
  private ImageIcon ArrayWithPlayer[] = new ImageIcon[3]; //{1,2,3}

private JFrame frame; //This is the whole frame

public Gui() {
    createAndShowGUI();
}

private void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("TextDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Display the window.
    frame.setSize(800, 800);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    //frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
    //frame.pack();
    frame.getContentPane().add(new ImagePanel( setImageIconFromUrl("/home/amministratore/Documenti/Java/ing-sw-2020-palini-rigutti-vangi/image/SantoriniBoardR.png",800,800).getImage()));

    chessBoard = new JPanel(new GridLayout(0, 5));
    chessBoard.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
    //chessBoard.setLayout(new BoxLayout());
    //chessBoard.setPreferredSize(new Dimension(400, 100));
    chessBoard.setBackground(Color.blue);

    //chessBoard.setAlignmentX((float) (2.2/21)*frame.getWidth());
    //chessBoard.setAlignmentY((float) (2.2/21)*frame.getHeight());
    //chessBoard.setMaximumSize(new Dimension((16/21)*frame.getWidth(),(16/21)*frame.getHeight()));
    //chessBoard.setAlignmentX(JLabel.LEFT_ALIGNMENT);
    //chessBoard.setBorder(new LineBorder(Color.BLACK));

    Insets buttonMargin = new Insets(0,0,0,0);
    for (int ii = 0; ii < chessBoardSquares.length; ii++) {
        for (int jj = 0; jj < chessBoardSquares[ii].length; jj++) {
            JButton b = new JButton();
            b.setMargin(buttonMargin);

            b.setBorder(null);
            b.setBorderPainted(false);
            b.setContentAreaFilled(false);
            b.setOpaque(false);
            chessBoardSquares[ii][jj] = b;
            b.setText("AA");
            chessBoard.add(chessBoardSquares[ii][jj]);
        }
    }

    //chessBoard.setOpaque( false );
    chessBoard.setBackground(new Color(255,0,0,0));
    frame.dispose();
    frame.add(chessBoard);
    frame.setVisible(true);

    //chessBoardSquares[0][0].setIcon( ArrayWithoutPlayer[0]); //This is the method to set Icon inside the button
}
}

最佳答案

I'm trying to setup the margin about the jpanel that it has inside the GridLayout

        //b.setMargin(buttonMargin);
        //b.setBorder(null);
        //b.setBorderPainted(false);

我认为您不需要所有这些代码。

只需设置按钮的边框:

b.setBorder( new EmptyBorder(5, 5, 5, 5) );

编辑:

frame.getContentPane().add(new ImagePanel(...));
…
frame.add(chessBoard);

首先,frame.getContentPane().add(…)frame.add(…) 是同一件事。也就是说,该组件将被添加到内容 Pane 中。第二种格式只是第一种格式的快捷方式。

因此,您正在尝试向 BorderLayout.CENTER 添加两个组件。这不起作用,因为 BorderLayout 只支持任何位置的单个组件。

Swing 的设计具有父/子关系,因此看起来您想要类似的东西:

  • JFrame (content pane)
    • ImagePanel
      • chessBoard

所以你的逻辑应该是这样的:

ImagePanel background = new ImagePanel(…);
background.setLayout( new BorderLayout() );
background.add(chessPanel, BorderLayout.CENTER);
frame.add(background, BorderLayout.CENTER);

现在您已经有了组件之间的父/子关系。

frame.setSize(800, 800); 

不要设置框架的大小。 (800, 800) 的尺寸错误。如果您的 ImagePanel 是 (800, 800),则框架必须更大,因为框架还包括标题栏和边框。

所以你的逻辑应该是:

frame.pack();
frame.setVisible(true);

pack() 方法将允许框架在所有组件添加到框架后确定其自己的首选大小。

注意:

在 ImagePanel 类中,您还需要实现 Image 的 getPreferresSize() 方法。这将使 pack() 方法正常工作。阅读 Swing 教程中关于 Custom Painting 的部分一个工作示例。

关于java - 在 Swing 中设置 GridLayout 的 JPanel 的边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62453362/

相关文章:

java - 如何将 JTextArea 添加到 Closable TabbedPane

java - 如何在同一个 JPanel 中实现南北部分

java - OpenCV - 测试分类器的准确性?

java - 一个简单的多线程闹钟

java - 从 Android Processing Sketch 保存图像

java - 通过按下按钮将文本从 JTextfield 复制到剪贴板

java - 访问辅助类中的实例变量

Java - 将 JFrame 设置为全屏时,屏幕变黑

java - JAVA 中编译错误 - (<anonymous ListSelectionListener>)

java - 如何根据可用空间制作 JLabel 的文本换行?