[英]JLabel not showing in my grid of JButtons
Creating a game called reversi also known as Othello and I am trying to add the starting position of my Black and Whites counters (using JLabel
, labelled 'W'
and 'B'
) in the middle of the board diagonally opposite from each other but for some reason only 2 show up and the other 2 don't show, which I don't understand why.创建一个名为黑白棋的游戏,也称为奥赛罗,我正在尝试将我的黑白计数器的起始 position (使用
JLabel
,标记为'W'
和'B'
)在棋盘中间对角相对,但是为了出于某种原因,只有 2 个出现,而另外 2 个没有出现,我不明白为什么。
How do I go about fixing it?我该如何修复它?
import java.io.*;
import java.util.*;
import java.lang.*;
import java.awt.*;
import javax.swing.*;
public class Reversi
{
// instance variables - replace the example below with your own
private JFrame frame;
private JLabel userName1;
private JLabel userName2;
private JTextField textUsername1;
private JTextField textUsername2;
private JButton startButton;
private JButton [][] squares = new JButton[8][8];
private JLabel whites = new JLabel("W");
private JLabel blacks = new JLabel("B");
/**
*
*/
public Reversi()
{
// initialise instance variables
gui();
}
/**
*
*/
public void gui()
{
frame = new JFrame("Reversi Game");
frame.setSize(800,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setLayout(new BoxLayout(frame, BoxLayout.X_AXIS));
JPanel userInterface = new JPanel(new GridBagLayout());
userInterface.setBackground(Color.LIGHT_GRAY);
userInterface.setBorder(BorderFactory.createBevelBorder(0));
//userInterface.setPreferredSize(new Dimension(350,700));
GridBagConstraints c = new GridBagConstraints();
//c.anchor = GridBagConstraints.NORTH;
JPanel board = new JPanel();
board.setBackground(Color.GRAY);
board.setBorder(BorderFactory.createBevelBorder(1));
board.setPreferredSize(new Dimension(750, 700));
userName1 = new JLabel("Enter First player: ");
userName2 = new JLabel("Enter Second player: ");
textUsername1 = new JTextField(15);
textUsername2 = new JTextField(15);
startButton = new JButton("START");
c.gridx = 0;
c.gridy = 0;
userInterface.add(userName1, c);
c.gridx = 1;
userInterface.add(textUsername1, c);
c.gridx = 0;
c.gridy = 1;
userInterface.add(userName2, c);
c.gridx = 1;
userInterface.add(textUsername2, c);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 2;
c.anchor = GridBagConstraints.SOUTH;
userInterface.add(startButton, c);
userInterface.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Players/Scoreboard"));
board.setLayout(new GridLayout(8,8));
for(int i = 0; i< 8; i++){
for (int j = 0; j < 8; j++)
{
squares[i][j] = new JButton();
squares[i][j].setBackground(Color.GREEN);
board.add(squares[i][j]);
}
}
squares[4][3].add(blacks);
squares[4][4].add(whites);
squares[3][4].add(blacks);
squares[3][3].add(whites);
frame.add(board, BorderLayout.CENTER);
JPanel wrapper = new JPanel();
wrapper.add(userInterface);
frame.add(wrapper, BorderLayout.LINE_END);
makeMenuBar();
frame.pack();
frame.setVisible(true);
}
private void makeMenuBar()
{
//Finish coding later
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu fileMenu = new JMenu("File");
menubar.add(fileMenu);
JMenuItem saveFile = new JMenuItem("Save File");
fileMenu.add(saveFile);
JMenuItem quitGame = new JMenuItem("Quit");
fileMenu.add(quitGame);
quitGame.addActionListener(ev -> {quit(); });
}
private void quit()
{
System.exit(0);
}
}
Each component (ie your JLabels
( whites
and blacks
)) can only be added to a container once, if you need to add more labels, even if they have the same String
inside, you have to create a new object for those, otherwise these will be shown in the last container you've added them.每个组件(即您的
JLabels
( whites
和blacks
))只能添加到容器中一次,如果您需要添加更多标签,即使它们内部有相同的String
,您也必须为它们创建一个新的 object,否则这些将显示在您添加它们的最后一个容器中。
squares[4][3].add(blacks);
squares[4][4].add(whites);
squares[3][4].add(blacks); // Only this one is added
squares[3][3].add(whites); // Only this one is added
You'll need something like this, or have an array of JLabels
and add them to all your squares
, then just call yourJLabelArray[i][j].setText("W")
(or "B"
)你需要这样的东西,或者有一个
JLabels
数组并将它们添加到你所有的squares
中,然后只需调用yourJLabelArray[i][j].setText("W")
(或"B"
)
squares[4][3].add(new JLabel("B"));
squares[4][4].add(new JLabel("W"));
squares[3][4].add(new JLabel("B"));
squares[3][3].add(new JLabel("W"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.