繁体   English   中英

在 Swing 中设置关于 GridLayout 的 JPanel 的边距

[英]Set margin about a JPanel of GridLayout in Swing

在此处输入图像描述 我正在尝试设置它在GridLayout内的JPanel的边距,请参阅JFrame ,但我没有使用其他答案找到解决方案。 我不知道这是否是重要问题,但它也只显示我 go 之前的第一个按钮到每个按钮用鼠标。 图片是一个例子,我想设置JPanel从图片网格的角落开始,因为图片有边框(不是来自代码,而是来自装饰板),蓝色方块是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
}
}

我正在尝试设置它在 GridLayout 内的 jpanel 的边距

        //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(…)是一回事。 也就是说,组件将被添加到内容窗格中。 第二种格式只是第一种格式的捷径。

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

Swing 设计有父/子关系,所以看起来你想要类似的东西:

  • JFrame(内容窗格)
    • 图像面板
      • 棋盘

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

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 class 中,您还需要实现 Image 的getPreferresSize()方法。 这将允许 pack() 方法正常工作。 阅读 Swing 教程中关于自定义绘画的部分以获取工作示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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