Java GUI文本对齐

Java GUI文本对齐,java,swing,alignment,layout-manager,Java,Swing,Alignment,Layout Manager,我在一个战舰项目上工作,我遇到了一点小麻烦。我想标记两个网格(用户和计算机),因此我使用GridBagLayout来控制组件的大小。现在,当我把文本放在中间,把它放在标签的中间,它什么也不做,它停留在同一个地方。这是我的密码: package buttongrid; import java.awt.Color; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayou

我在一个战舰项目上工作,我遇到了一点小麻烦。我想标记两个网格(用户和计算机),因此我使用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
        }
}
packagebuttongrid;
导入java.awt.Color;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.GridLayout;
导入java.awt.image.buffereImage;
导入java.net.MalformedURLException;
导入java.net.URL;
导入javax.swing.BorderFactory;
导入javax.swing.Icon;
导入javax.swing.ImageIcon;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.SwingConstants;
导入javax.swing.border.border;
公共类ButtonGrid{
JFrame frame=新JFrame();//创建帧
JPanel panel1=新的JPanel();
JPanel panel2=新的JPanel();
JPanel panel3=新的JPanel();
JPanel panel4=新的JPanel();
JPanel panel5=新的JPanel();
JPanel panel6=新的JPanel();
JLabel label1=新的JLabel();
JLabel label2=新的JLabel();
JLabel[][]网格;//命名按钮的网格
JLabel[][]enemyGrid;
字符串COLS=“ABCDEFGHIJK”;
GridBagConstraints c=新的GridBagConstraints();
Border Border=BorderFactory.createLineBorder(Color.BLACK,1);
public ButtonGrid()抛出错误的DurLexException{//构造函数
URL urlPic=新URL(“http://i47.tinypic.com/14wswi9.gif");
ImageIcon waterPic=新的ImageIcon(urlPic);
frame.setLayout(新的GridBagLayout());//设置布局
frame.setresizeable(false);
//添加左上面板
标签1.setText(“您的网格”);
标签1.设置水平文本位置(SwingConstants.中心);
标签1.设置垂直文本位置(SwingConstants.中心);
面板4.添加(标签1);
第4组:边界命令;
c、 gridx=0;
c、 gridy=0;
c、 ipady=10;
框架。添加(第4组,c);
//添加顶部中间面板
第5组:边界命令;
c、 gridx=1;
c、 gridy=0;
c、 ipady=10;
框架。添加(面板5,c);
//添加顶部中间面板
标签2.setText(“敌人网格”);
标签2.设置水平文本位置(SwingConstants.中心);
标签2.设置垂直文本位置(SwingConstants.中心);
面板6.添加(标签2);
第6组:边界命令;
c、 gridx=2;
c、 gridy=0;
c、 ipady=10;
框架。添加(第6组,c);
//用户网格
c、 gridx=0;
c、 gridy=1;
面板1.设置布局(新网格布局(11,11));
grid=newjlabel[11][11];//分配网格的大小

对于(int y=0;y我假设您指的是标题标签

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

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

如果您想对标签进行更多填充,可以使用
CompoundBorder
并将当前边框与
EmptyBorder
混合,或者使用
gridbagstraints
定义其他插入

例如

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

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

不过,您可能会得到更好的结果,您可以将UI分解为多个部分。网格是可重复的组件,所以首先让网格成为它自己的组件


您可以使用
BorderLayout
定位标题和网格本身,然后使用另一个容器,使用
GridLayout
添加
GridPanel
s以并排获得它们,例如…

我假设您指的是标题标签

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

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

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

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

如果您想对标签进行更多填充,可以使用
CompoundBorder
并将当前边框与
EmptyBorder
混合,或者使用
gridbagstraints
定义其他插入

例如

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

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

不过,您可能会得到更好的结果,您可以将UI分解为多个部分。网格是可重复的组件,所以首先让网格成为它自己的组件


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

根据Mad的建议,问题在于面板的布局管理器,我将进一步说现在,你甚至不需要一个JPanel来保存标签。只需将标签直接添加到网格中,然后你甚至需要担心面板

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

//...
panel4.add(label1, gbc);
//...
panel6.add(label2, gbc);
您可以通过在每个标签上使用边框来控制所需的间距。例如,您可以创建如下边框:

Border labelBorder = BorderFactory.createEmptyBorder(5, 10, 5, 10);
然后,将标签添加到网格的代码为:

label1.setBorder(labelBorder);
//panel4.add(label1);
//panel4.setBorder(border);
c.gridx = 0;
c.gridy = 0;
//c.ipady = 10;
//frame.add(panel4, c);
frame.add(label1, c);
对于中间的组件,您只需创建一个没有文本的JLabel。然后,边框将占据整个空间,从而控制两个网格之间的水平空间

此外,无需创建BuffereImage将其用作图标。网格的大小将由“水”图像的大小决定

要使文本在网格中居中,应使用以下方法:

label.setHorizontalAlignment(SwingConstants.CENTER);
垂直对齐的默认值为“中心”,因此您无需担心