我添加了一个while循环,它将在播放器之间改变,如下面的代码所示。在添加此while循环之前,它会显示网格,按钮等,但是在添加此循环时,面板只是纯白色的。我不知道为什么。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.security.auth.x500.X500Principal;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.omg.CORBA.PRIVATE_MEMBER;
public class Game
{
boolean p1;
private int counterY1 = 515, counterY2 = 515, counterY3 = 515, counterY4 = 515;
boolean playerTurn = true;
boolean playerTurn2 = false;
boolean moveLoop = true;
public void moveC1Up()
{
counterY1 -= 51 * diceRoll();
}
public void moveC2Up()
{
counterY2 -= 51 * diceRoll();
}
public void moveC3Up()
{
counterY3 -= 51 * diceRoll();
}
public void moveC4Up()
{
counterY4 -= 51 * diceRoll();
}
public int diceRoll()
{
int randGen = (int)(Math.random()*1) + 1;
System.out.print(randGen);
return randGen;
}
private JButton moveC1But, moveC2But, rollDiceButton;
private JLabel amountRolledLabel;
public Game()
{
JFrame window = new JFrame ("Main Game");
final JPanel firstPanel = new JPanel(new GridLayout(3,1))
{
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
super.paintComponent(g2d);
int width = getWidth() / 3;
int height = getHeight() / 11;
for (int i = 0; i < 4; i++) {
g.drawLine(i * width, 0, i * width, getHeight());
}
for (int i = 0; i < 11; i++) {
g.drawLine(0, i * height, getWidth(), i * height);
}
g.setColor(Color.DARK_GRAY);
g.drawOval(220, counterY1, 40, 40);
g.fillOval(220, counterY1, 40, 40);
g.drawOval(300, counterY2, 40, 40);
g.fillOval(300, counterY2, 40, 40);
g.setColor(Color.LIGHT_GRAY);
g.drawOval(410, counterY3, 40, 40);
g.fillOval(410, counterY3, 40, 40);
g.drawOval(490, counterY4, 40, 40);
g.fillOval(490, counterY4, 40, 40);
}
};
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(firstPanel, BorderLayout.CENTER);
JPanel rightSidePanel = new JPanel(new GridLayout(10,1));
moveC1But = new JButton("Move Counter 1");
moveC2But = new JButton("Move Counter 2");
rollDiceButton = new JButton("Roll Dice");
rightSidePanel.add(moveC1But, BorderLayout.LINE_START);
rightSidePanel.add(moveC2But, BorderLayout.LINE_END);
rightSidePanel.add(rollDiceButton, BorderLayout.SOUTH);
mainPanel.add(rightSidePanel, BorderLayout.EAST);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(mainPanel);
window.setSize(700, 600);
window.setLocationRelativeTo(null);
window.setVisible(true);
window.setResizable(false);
while(moveLoop == true)
{
moveC1But.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae)
{
diceRoll();
moveC1Up();
firstPanel.repaint();
System.out.print(diceRoll());
System.out.print("Test1");
playerTurn = false;
playerTurn2 = true;
moveLoop = false;
}
});
moveC2But.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae)
{
diceRoll();
moveC3Up();
firstPanel.repaint();
System.out.print(diceRoll());
System.out.print("Test1");
playerTurn = false;
playerTurn2 = true;
moveLoop = false;
}
});
}
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new Game();
}
});
}
}
编辑:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.security.auth.x500.X500Principal;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.omg.CORBA.PRIVATE_MEMBER;
public class Game
{
boolean p1;
private int counterY1 = 515, counterY2 = 515, counterY3 = 515, counterY4 = 515;
boolean playerTurn = true;
boolean playerTurn2 = false;
boolean moveLoop = true;
public void moveC1Up()
{
counterY1 -= 51 * diceRoll();
}
public void moveC2Up()
{
counterY2 -= 51 * diceRoll();
}
public void moveC3Up()
{
counterY3 -= 51 * diceRoll();
}
public void moveC4Up()
{
counterY4 -= 51 * diceRoll();
}
public int diceRoll()
{
int randGen = (int)(Math.random()*1) + 1;
System.out.print(randGen);
return randGen;
}
private JButton moveC1But, moveC2But, rollDiceButton;
private JLabel amountRolledLabel;
public Game()
{
JFrame window = new JFrame ("Main Game");
final JPanel firstPanel = new JPanel(new GridLayout(3,1))
{
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
super.paintComponent(g2d);
int width = getWidth() / 3;
int height = getHeight() / 11;
for (int i = 0; i < 4; i++) {
g.drawLine(i * width, 0, i * width, getHeight());
}
for (int i = 0; i < 11; i++) {
g.drawLine(0, i * height, getWidth(), i * height);
}
g.setColor(Color.DARK_GRAY);
g.drawOval(220, counterY1, 40, 40);
g.fillOval(220, counterY1, 40, 40);
g.drawOval(300, counterY2, 40, 40);
g.fillOval(300, counterY2, 40, 40);
g.setColor(Color.LIGHT_GRAY);
g.drawOval(410, counterY3, 40, 40);
g.fillOval(410, counterY3, 40, 40);
g.drawOval(490, counterY4, 40, 40);
g.fillOval(490, counterY4, 40, 40);
}
};
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(firstPanel, BorderLayout.CENTER);
JPanel rightSidePanel = new JPanel(new GridLayout(10,1));
moveC1But = new JButton("Move Counter 1");
moveC2But = new JButton("Move Counter 2");
rollDiceButton = new JButton("Roll Dice");
rightSidePanel.add(moveC1But, BorderLayout.LINE_START);
rightSidePanel.add(moveC2But, BorderLayout.LINE_END);
rightSidePanel.add(rollDiceButton, BorderLayout.SOUTH);
mainPanel.add(rightSidePanel, BorderLayout.EAST);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(mainPanel);
window.setSize(700, 600);
window.setLocationRelativeTo(null);
window.setVisible(true);
window.setResizable(false);
if(playerTurn == true)
{
moveC1But.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae)
{
diceRoll();
moveC1Up();
firstPanel.repaint();
System.out.print(diceRoll());
System.out.print("Test1");
playerTurn = false;
playerTurn2 = true;
moveLoop = false;
}
});
moveC2But.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae)
{
diceRoll();
moveC2Up();
firstPanel.repaint();
System.out.print(diceRoll());
System.out.print("Test1");
playerTurn = false;
playerTurn2 = true;
moveLoop = false;
}
});
}else if (playerTurn2 == true)
{
moveC1But.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae)
{
diceRoll();
moveC3Up();
firstPanel.repaint();
System.out.print(diceRoll());
System.out.print("Test1");
playerTurn = false;
playerTurn2 = true;
moveLoop = false;
}
});
moveC2But.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae)
{
diceRoll();
moveC4Up();
firstPanel.repaint();
System.out.print(diceRoll());
System.out.print("Test1");
playerTurn = false;
playerTurn2 = true;
moveLoop = false;
}
});
}
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new Game();
}
});
}
最佳答案
您的问题很普遍:您在Swing事件线程上运行了一段长时间运行的代码,从而完全束缚了它。由于此线程负责所有Swing绘图和用户交互,因此GUI基本上会冻结,直到长时间运行的代码完成为止。可能的解决方案包括:
在您的游戏循环中使用Swing计时器,避免在Swing事件线程上使用while (true)
。
将后台线程用于长时间运行的代码。这可以通过SwingWorker方便地完成。
都不做。查看您的代码,我认为没有理由首先使用while (true)
循环。它所做的就是多次将ActionListeners添加到JButtons中,因此完全没有价值。因此,摆脱它,仅添加一次ActionListeners。对于您的代码,这是您最好的选择-重新考虑您的代码,使其更有意义。
编辑
你问:
是的,我知道ifs在做什么,并已将其删除。但基本上我有两个按钮,移动计数器1和移动计数器2,每个玩家都有2个计数器,但是只有两个按钮。我希望玩家1开始并击中他们想移动的计数器,然后允许第二个玩家移动其计数器,反之亦然。
逻辑必须在ActionListener中。例如:
moveC1But.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
diceRoll();
if (player1Turn) {
// logic for player turn 1
} else {
// logic for player turn 2
}
}
});
关于java - 当我放置while循环时,JFrame显示白色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23329020/