[英]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.