我正在嘗試創建一個由 100 個正方形組成的網格。我下面的代碼非常錯誤,我不知道為什么。
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
public class snake extends JFrame
{
public static void main(String[] args)
{
Border whiteLine = BorderFactory.createLineBorder(Color.white);
//-----------FRAME
JFrame frame = new JFrame();
frame.setSize(1000,1000);
frame.setTitle("Snake");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.black);
frame.setVisible(true);
frame.setLayout(new GridLayout(10,10));
//-----------FRAME
//-----------PANELS
Dimension panelDimension = new Dimension(20,20);
int counter = 0;
JPanel[][] p = new JPanel[10][10];
for (int i = 0; i < 10; i )
{
for (int j = 0; j < 10; j )
{
p[i][j] = new JPanel();
//p[i][j].setPreferredSize(panelDimension);
p[i][j].setBackground(Color.red);
//p[i][j].setLocation(490,490);
p[i][j].setBorder(whiteLine);
p[i][j].setVisible(true);
frame.getContentPane().add(p[i][j]);
counter =1;
}
}
System.out.println("counter: " counter);
}
}
當我運行這樣的代碼時,它顯示了一個由 2 列組成的網格,第一列有 7 行,第二列有 6 行。有時它甚至顯示其他不正確的列數和行數。我不知道為什么它不創建一個 10 行 10 列的網格。
uj5u.com熱心網友回復:
你有幾個問題,包括:
- 呼叫
setVisible(true)上的JFrame之前添加組件,稱之前pack()的頂層視窗。這可能會導致我們的 GUI 中的定位組件不穩定,甚至 GUI 仍然為空 pack()添加組件后和設定可見之前不呼叫JFrame- 設定 JFrame 的大小。讓布局管理器、容器和組件為您執行此操作(這就是呼叫的
pack()目的) - 將其設定為錯誤的大小,一個“完美的正方形”,忽略作業系統添加的選單欄,
- ...
例如:
package foo01;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class SnakePanel extends JPanel {
private static final int CELL_WIDTH = 80;
private static final Dimension CELL_DIMENSION = new Dimension(CELL_WIDTH, CELL_WIDTH);
private static final int COLUMNS = 10;
private static final int GAP = 2;
private static final Color BG_COLOR = Color.WHITE;
private static final Color CELL_COLOR = Color.RED;
public SnakePanel() {
setBackground(BG_COLOR);
// add a white line around the grid
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
// create a grid with gaps that show the background (white) color
setLayout(new GridLayout(COLUMNS, COLUMNS, GAP, GAP));
for (int row = 0; row < COLUMNS; row ) {
for (int col = 0; col < COLUMNS; col ) {
JPanel cell = new JPanel(); // create a new cell
cell.setPreferredSize(CELL_DIMENSION); // cheating here. Better to override getPreferredSize()
cell.setBackground(CELL_COLOR);
add(cell);
// give the cell JPanel some simple behavior:
cell.addMouseListener(new MyMouse(col, row));
}
}
}
private class MyMouse extends MouseAdapter {
private int col;
private int row;
public MyMouse(int col, int row) {
this.col = col;
this.row = row;
}
@Override
public void mousePressed(MouseEvent e) {
System.out.printf("Mouse pressed row and column: [%d, %d]%n", row, col);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
// create the main JPanel
SnakePanel snakePanel = new SnakePanel();
// create the JFrame
JFrame frame = new JFrame("Snake");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// add the main JPanel to the JFrame
frame.add(snakePanel);
// pack the JFrame -- tells the layout managers to do their things
frame.pack();
// if we want to center the GUI:
frame.setLocationRelativeTo(null);
// only *now* do we display the GUI
frame.setVisible(true);
});
}
}
關于代碼的一些注釋:
傳遞給SwingUtilities.invokeLater(...)方法的 Runnable 中的任何代碼都會在Swing 事件執行緒上呼叫,這在創建 Swing GUI 時是明智的做法
SwingUtilities.invokeLater(() -> {
// ....
});
首先,創建由 JFrame 保存的主 JPanel:
SnakePanel snakePanel = new SnakePanel();
然后創建 JFrame,添加 JPanel 并呼叫pack(). pack 呼叫告訴布局管理器在那里做一些事情,在容器中布局組件,根據他們的首選大小和布局來調整大小:
JFrame frame = new JFrame("Snake");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(snakePanel);
frame.pack();
如果我們想讓 GUI 居中:
frame.setLocationRelativeTo(null);
只是現在我們顯示GUI
frame.setVisible(true);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/408905.html
標籤:
