GridLayout 与尺寸不成比例
GridLayout not proportional to dimensions
我一直在 java 为一个项目做填字游戏。但是,我无法解决将 CrossWord 绘制到 JPanel
时遇到的一个问题
该程序从二维字符数组中获取数据,并使用它生成自定义类型的 JLabel 的网格。这是代码:
public void update(Observable o, Object o1) {
if(model.getMatrix() != null){
configurePanel(model.getRows(), model.getCols());
this.add(panel);
this.pack();
this.revalidate();
this.repaint();
}
}
private void configurePanel(int w, int h) {
if (panel!=null){
this.remove(panel);
}
panel = new JPanel();
panel.setBounds(40, 40, w*50, h*50);
panel.setLayout(new GridLayout(w, h));
labels = createLabels(model.getMatrix());
for (int i = 0; i < w; i++){
for(int j = 0; j < h; j++){
panel.add(labels[i][j]);
}
}
}
private CWlabel[][] createLabels(char[][] matrix) {
int w = matrix.length;
int h = matrix[0].length;
labels = new CWlabel[w][h];
for (int i = 0; i < w; i++){
for(int j = 0; j < h; j++){
char c = matrix[i][j];
labels[i][j] = new CWlabel();
labels[i][j].setBorder(BorderFactory.createLineBorder(Color.black));
labels[i][j].setOpaque(true);
if (c != ' '){
labels[i][j].setBackground(Color.white);
} else {
labels[i][j].setBackground(Color.black);
}
}
}
return labels;
}
我的主要问题是在 configurePanel() 中,其中面板的大小设置为与填字游戏尺寸成比例,这应该确保其中的每个组件都是完美的正方形,但事实并非如此 as seen here
在所示示例中,差异很细微,但仍然很明显。奇怪的是,如果我手动替换,
panel.setBounds(40, 40, w*50, h*50);
与
panel.setBounds(40, 40, 400, 450);
如图所示,结果似乎与行数和列数成正比 here
根据我的统计,您有 9
行和 8
列,因此调用 configurePanel
如下所示:
configurePanel(model.getRows(), model.getCols());
将导致 450
的宽度和 400
的高度,但您想要 400
的宽度和 450
的高度。因此切换第一个和第二个参数,如下所示:
configurePanel(model.getCols(), model.getRows());
注意:您不应该使用绝对定位,它会导致很多格式化问题(调整组件大小、添加组件等)。您应该改用具有更多自定义功能的布局管理器,例如 GridBagLayout
或 MigLayout.
我一直在 java 为一个项目做填字游戏。但是,我无法解决将 CrossWord 绘制到 JPanel
时遇到的一个问题该程序从二维字符数组中获取数据,并使用它生成自定义类型的 JLabel 的网格。这是代码:
public void update(Observable o, Object o1) {
if(model.getMatrix() != null){
configurePanel(model.getRows(), model.getCols());
this.add(panel);
this.pack();
this.revalidate();
this.repaint();
}
}
private void configurePanel(int w, int h) {
if (panel!=null){
this.remove(panel);
}
panel = new JPanel();
panel.setBounds(40, 40, w*50, h*50);
panel.setLayout(new GridLayout(w, h));
labels = createLabels(model.getMatrix());
for (int i = 0; i < w; i++){
for(int j = 0; j < h; j++){
panel.add(labels[i][j]);
}
}
}
private CWlabel[][] createLabels(char[][] matrix) {
int w = matrix.length;
int h = matrix[0].length;
labels = new CWlabel[w][h];
for (int i = 0; i < w; i++){
for(int j = 0; j < h; j++){
char c = matrix[i][j];
labels[i][j] = new CWlabel();
labels[i][j].setBorder(BorderFactory.createLineBorder(Color.black));
labels[i][j].setOpaque(true);
if (c != ' '){
labels[i][j].setBackground(Color.white);
} else {
labels[i][j].setBackground(Color.black);
}
}
}
return labels;
}
我的主要问题是在 configurePanel() 中,其中面板的大小设置为与填字游戏尺寸成比例,这应该确保其中的每个组件都是完美的正方形,但事实并非如此 as seen here
在所示示例中,差异很细微,但仍然很明显。奇怪的是,如果我手动替换,
panel.setBounds(40, 40, w*50, h*50);
与
panel.setBounds(40, 40, 400, 450);
如图所示,结果似乎与行数和列数成正比 here
根据我的统计,您有 9
行和 8
列,因此调用 configurePanel
如下所示:
configurePanel(model.getRows(), model.getCols());
将导致 450
的宽度和 400
的高度,但您想要 400
的宽度和 450
的高度。因此切换第一个和第二个参数,如下所示:
configurePanel(model.getCols(), model.getRows());
注意:您不应该使用绝对定位,它会导致很多格式化问题(调整组件大小、添加组件等)。您应该改用具有更多自定义功能的布局管理器,例如 GridBagLayout
或 MigLayout.