我正在制作一个战列舰程序,并且遇到了轻微的事故。我想标记两个网格(用户和计算机),所以我使用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
}
}
我在框架中的每个组件周围都有一个边框,只是为了使事情更容易。
其中JLabel ??。 –