gpt4 book ai didi

java - GUI 文本对齐

转载 作者:行者123 更新时间:2023-11-29 07:47:34 25 4
gpt4 key购买 nike

我正在研究一个战舰项目,我遇到了一个小事故。我想标记两个网格(用户和计算机),所以我使用 GridBagLayout 来控制组件的大小。现在,当我将文本居中放置在它所在标签的中间时,它什么也没做,它停留在同一个地方。这是我的代码:

package buttongrid;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.Border;

public class ButtonGrid {

JFrame frame=new JFrame(); //creates frame
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
JPanel panel6 = new JPanel();
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel[][] grid; //names the grid of buttons
JLabel[][] enemyGrid;
String COLS = "ABCDEFGHIJK";
GridBagConstraints c = new GridBagConstraints();
Border border = BorderFactory.createLineBorder(Color.BLACK, 1);

public ButtonGrid() throws MalformedURLException{ //constructor
URL urlPic = new URL("http://i47.tinypic.com/14wswi9.gif");
ImageIcon waterPic = new ImageIcon(urlPic);

frame.setLayout(new GridBagLayout()); //set layout
frame.setResizable(false);

//ADDING TOP LEFT PANEL
label1.setText("Your Grid");
label1.setHorizontalTextPosition(SwingConstants.CENTER);
label1.setVerticalTextPosition(SwingConstants.CENTER);
panel4.add(label1);
panel4.setBorder(border);
c.gridx = 0;
c.gridy = 0;
c.ipady = 10;
frame.add(panel4, c);

//ADDING TOP MIDDLE PANEL
panel5.setBorder(border);
c.gridx = 1;
c.gridy = 0;
c.ipady = 10;
frame.add(panel5, c);

//ADDING TOP MIDDLE PANEL
label2.setText("Enemy Grid");
label2.setHorizontalTextPosition(SwingConstants.CENTER);
label2.setVerticalTextPosition(SwingConstants.CENTER);
panel6.add(label2);
panel6.setBorder(border);
c.gridx = 2;
c.gridy = 0;
c.ipady = 10;
frame.add(panel6, c);

//USER GRID
c.gridx = 0;
c.gridy = 1;
panel1.setLayout(new GridLayout(11,11));
grid=new JLabel[11][11]; //allocate the size of grid
for(int y=0; y<11; y++){
for(int x=0; x<11; x++){
ImageIcon icon2 = new ImageIcon(new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB));
grid[x][y]= new JLabel(icon2);
grid[x][y].setBorder(border);
panel1.add(grid[x][y]);
}
}

frame.add(panel1, c);

for(int y=1; y<11; y++){
grid[y][0].setText(Integer.toString(y));
grid[y][0].setHorizontalTextPosition(SwingConstants.CENTER);
for(int x=1; x<11; x++){
grid[x][y].setIcon(waterPic);
}
}

for(int x = 1; x < 11; x++){
grid[0][x].setText(COLS.substring(x - 1, x));
grid[0][x].setHorizontalTextPosition(SwingConstants.CENTER);
}

//EMPTY SPACE IN BETWEEN GRIDS
c.fill = GridBagConstraints.VERTICAL;
c.gridx = 1;
c.gridy = 1;
c.ipadx = 10;
panel2.setBorder(border);
frame.add(panel2, c);

//ENEMY GRID
c.gridx = 2;
c.gridy = 1;
panel3.setLayout(new GridLayout(11,11));
enemyGrid=new JLabel[11][11]; //allocate the size of grid
for(int y=0; y<11; y++){
for(int x=0; x<11; x++){
ImageIcon icon2 = new ImageIcon(new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB));
enemyGrid[x][y]= new JLabel(icon2); //
enemyGrid[x][y].setBorder(border);
panel3.add(enemyGrid[x][y]); //
}
}

frame.add(panel3, c);

for(int y=1; y<11; y++){
enemyGrid[y][0].setText(Integer.toString(y));
enemyGrid[y][0].setHorizontalTextPosition(SwingConstants.CENTER);
for(int x=1; x<11; x++){
enemyGrid[x][y].setIcon(waterPic);
}
}

for(int x = 1; x < 11; x++){
enemyGrid[0][x].setText(COLS.substring(x - 1, x));
enemyGrid[0][x].setHorizontalTextPosition(SwingConstants.CENTER);
}

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack(); //sets appropriate size for frame
frame.setVisible(true); //makes frame visible
}
public static void main(String[] args) throws MalformedURLException{
new ButtonGrid();//makes new ButtonGrid with 2 parameters
}
}

为了简化操作,我在框架中的每个组件周围都添加了边框。

最佳答案

我假设你的意思是标题标签...

enter image description here

这是由布局管理器而不是标签本身引起的。 JPanel 默认使用 FlowLayout。考虑使用不同的布局管理器,例如,GridBagLayout

JPanel panel4 = new JPanel(new GridBagLayout());
JPanel panel6 = new JPanel(new GridBagLayout());

enter image description here

如果你想要更多关于标签的填充,你可以使用 CompoundBorder 并将当前边框与 EmptyBorder 混合,或者使用 GridBagConstraints 来定义额外的插图...

例如……

GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(8, 8, 8, 8);

//...
panel4.add(label1, gbc);
//...
panel6.add(label2, gbc);

enter image description here

不过,您可能会获得更好的结果,将 UI 分解为多个部分。网格是可重复的组件,因此首先要使网格成为它自己的组件。

您可以使用 BorderLayout 来定位标题和网格本身,然后使用另一个容器,使用 GridLayout 添加 GridPanel让他们并排,例如......

关于java - GUI 文本对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24254684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com