我的程序是显示4个按钮,每个按钮都有一个数字,然后根据按下的按钮,下一个屏幕将显示不同数量的JTextFields,但是当按下任何按钮时,下一个面板是空白的?非常感谢任何帮助和建议。
public class TestGui {
static int playersSelected = 0;
public static void main(String[] args) {
JFrame gameWindow = new JFrame("Trivial Pursuit");
gameWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameWindow.setSize(800, 650);
gameWindow.setVisible(true);
JPanel mainMenu = new JPanel();
//create layout for main menu
mainMenu.setLayout(new FlowLayout(FlowLayout.CENTER));
//add the contents of the main menu panel to the application window
gameWindow.add(mainMenu);
JButton[] playerButton = new JButton[4];
for (int i = 0; i < playerButton.length; i++) {
final int FinalI = i;
playerButton[i] = new JButton("Button " + i);
mainMenu.add(playerButton[i]);
playerButton[i].setOpaque(true);
playerButton[i].setPreferredSize(new Dimension(271, 123));
playerButton[i].setContentAreaFilled(true);
playerButton[i].setBorder(BorderFactory.createEmptyBorder());
playerButton[i].addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseEntered(java.awt.event.MouseEvent evt) {
playerButton[FinalI].setBorder(BorderFactory.createLoweredBevelBorder());
}
@Override
public void mouseExited(java.awt.event.MouseEvent evt) {
playerButton[FinalI].setBorder(BorderFactory.createEmptyBorder());
}
});
}
JLabel[] playerNumberText = new JLabel[5];
JTextField[] createPlayers = new JTextField[5];
JPanel createPlayersPanel = new JPanel();
createPlayersPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
createPlayersPanel.setVisible(true);
for (int i = 1; i <= playersSelected; i++) {
final int finalI = i;
createPlayers[i] = new JTextField("Player " + i + " input name here");
createPlayers[i].setPreferredSize(new Dimension(160, 20));
playerNumberText[i] = new JLabel();
playerNumberText[i].setText("Player " + i);
playerNumberText[i].setPreferredSize(new Dimension(350, 100));
playerNumberText[i].setHorizontalAlignment(SwingConstants.CENTER);
createPlayersPanel.add(playerNumberText[i]);
createPlayers[i].addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (createPlayers[finalI].getText().equals("Player " + finalI + " input name here")) {
createPlayers[finalI].setText("");
}
}
});
}
for (int i = 0; i < playerButton.length; i++) {
final int finalI = i;
playerButton[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
gameWindow.remove(mainMenu);
gameWindow.add(createPlayersPanel);
switch (finalI) {
case 0:
playersSelected = 2;
break;
case 1:
playersSelected = 3;
break;
case 2:
playersSelected = 4;
break;
case 3:
break;
}
gameWindow.revalidate();
gameWindow.repaint();
System.out.println(playersSelected);
}
});
}
}
}
答案 0 :(得分:1)
在创建时,createPlayersPanel
JPanel
为空,因为playerSelected
为0。
在您的动作侦听器中,此面板永远不会更新,因此它仍然是一个空面板。
您可以做的是创建一种动态构建面板的方法,而不是仅在启动时构建它。
由于此处仅使用playerSelected
,因此您可以删除此静态变量并直接从动作侦听器传递值。
private static JPanel buildPlayersPanel(int playerSelected) {
JPanel createPlayersPanel = new JPanel();
createPlayersPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
createPlayersPanel.setVisible(true);
for (int i = 1; i <= playersSelected; i++) {
final int finalI = i;
createPlayers[i] = new JTextField("Player " + i + " input name here");
createPlayers[i].setPreferredSize(new Dimension(160, 20));
playerNumberText[i] = new JLabel();
playerNumberText[i].setText("Player " + i);
playerNumberText[i].setPreferredSize(new Dimension(350, 100));
playerNumberText[i].setHorizontalAlignment(SwingConstants.CENTER);
createPlayersPanel.add(playerNumberText[i]);
createPlayersPanel.add(createPlayers[i]);
createPlayers[i].addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (createPlayers[finalI].getText().equals("Player " + finalI + " input name here")) {
createPlayers[finalI].setText("");
}
}
});
}
return createPlayersPanel;
}
在动作监听器中:
@Override
public void actionPerformed(ActionEvent ae) {
int playersSelected = 0;
switch (finalI) {
case 0:
playersSelected = 2;
break;
case 1:
playersSelected = 3;
break;
case 2:
playersSelected = 4;
break;
case 3:
break;
}
gameWindow.remove(mainMenu);
gameWindow.add(buildPlayersPanel(playersSelected));
gameWindow.revalidate();
gameWindow.repaint();
System.out.println(playersSelected);
}