GUI文本对齐

时间:2014-06-17 02:01:30

标签: java swing alignment layout-manager

我正在制作战舰计划,我遇到了一些轻微的不幸事故。我想标记两个网格(用户和计算机),所以我使用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
        }
}

我在框架中的每个组件周围都包含一个边框,只是为了让事情更容易。

2 个答案:

答案 0 :(得分:3)

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

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定位标题和网格本身,然后使用其他容器,使用GridPanel添加GridLayout以将它们并排放置,例如...

答案 1 :(得分:2)

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

您可以在每个标签上使用边框来控制所需的间距。例如,您可以创建一个边框,如:

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。然后边界将占据整个空间,这将控制两个网格之间的水平空间。

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

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

label.setHorizontalAlignment(SwingConstants.CENTER);

垂直对齐的默认值是CENTER,因此您无需担心。