【发布时间】:2014-11-26 18:12:13
【问题描述】:
我正在开发这个 RPSGUI;就逻辑而言,一切都很顺利,但大约一周左右前我进入了最后阶段,从那以后我一直被困在这里。我无法更新我的标签。所以说用户赢了,计数器会从 0 变为 1、2 等等。我有很多东西被注释掉了,所以你可以看到我已经尝试过了。我实际上删除了我评论的几个部分,因为我感到困惑,但是是的。我还想让 gui 说出播放的内容,并且不确定从哪里开始:-/。我希望有人可以帮助我解决这个问题。这对我来说意义重大。
这是没有驱动程序类的程序。除了显示之外,驱动程序类中没有任何内容。
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class GamePanel extends JPanel{
private JButton rockButton, paperButton, scissorsButton;
private JLabel userLabel, computerLabel, resultLabel, winLabel, tieLabel, loseLabel;
private int winInt, tieInt, loseInt = 0;
private String rockButtonText = "Rock";
private String paperButtonText = "Paper";
private String scissorsButtonText = "Scissors";
private JPanel buttonPanel, labelPanel, resultPanel;
public int computerInt;
String[] attackArray = {"Rock", "Paper", "Scissors"};
public GamePanel(){
String[] attackArray = {"Rock", "Paper", "Scissors"};
setLayout(new GridLayout(5,3));
setBackground(Color.white.brighter());
setForeground(Color.ORANGE.darker());
//make the buttons
JButton rockButton = new JButton(rockButtonText);
JButton paperButton = new JButton(paperButtonText);
JButton scissorsButton = new JButton(scissorsButtonText);
//style the buttons
rockButton.setBackground(Color.orange);
rockButton.setForeground(Color.black.darker());
paperButton.setBackground(Color.orange);
paperButton.setForeground(Color.black.darker());
scissorsButton.setBackground(Color.orange);
scissorsButton.setForeground(Color.black.darker());
//add labels
JLabel userLabel = new JLabel("User:");
JLabel computerLabel = new JLabel("Computer:");
JLabel winLabel = new JLabel("Win: " + winInt);
JLabel tieLabel = new JLabel("Tie: " + tieInt);
JLabel loseLabel = new JLabel("Lose:" + loseInt);
//style Labels
userLabel.setBackground(Color.orange);
userLabel.setForeground(Color.black.darker());
computerLabel.setBackground(Color.orange);
computerLabel.setForeground(Color.black.darker());
winLabel.setBackground(Color.orange);
winLabel.setForeground(Color.black.darker());
tieLabel.setBackground(Color.orange);
tieLabel.setForeground(Color.black.darker());
loseLabel.setBackground(Color.orange);
loseLabel.setForeground(Color.black.darker());
//add buttons to panel
add(rockButton);
//add(Box.createRigidArea(new Dimension (0,10)));
add(paperButton);
//add(Box.createRigidArea(new Dimension (0,10)));
add(scissorsButton);
//add(Box.createRigidArea(new Dimension (0,10)));
add(userLabel);
//add(Box.createRigidArea(new Dimension (0,10)));
add(computerLabel);
//add(Box.createRigidArea(new Dimension (0,10)));
add(winLabel);
//add(Box.createRigidArea(new Dimension (0,10)));
add(tieLabel);
//add(Box.createRigidArea(new Dimension (0,10)));
add(loseLabel);
//add(Box.createRigidArea(new Dimension (0,10)));
// add action listeners
rockButton.addActionListener(new ButtonListener());
paperButton.addActionListener(new ButtonListener());
scissorsButton.addActionListener(new ButtonListener());
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ae){
int computerSourceAI = (int) (Math.random() * 2); //random generator
String oppChoice = attackArray[computerSourceAI]; // the choice of your opponent
String yourChoice = ((JButton) ae.getSource()).getText(); // casts source from object to button and gets the text
int outcome = determineWinner(yourChoice, oppChoice);
// 0 = tie
//1 == win
//2 = lose
if (outcome == 1)
{
// win message
//System.out.println("You win!");
winLabel.setText("You Won" + winInt++);
}
else if (outcome == 0)
{
// tie message
//System.out.println("Tie");
tieLabel.setText("You tied" + tieInt++);
}
else
{
loseLabel.setText("You lose" + loseInt++);
}
}
}
public int determineWinner(String you, String opponent)
{
if(you.equals("Rock") && (opponent.equals("Rock"))){
//tieInt++;
tieLabel.setText("You tied" + tieInt++);
return tieInt++;
}else if(you.equals("Rock") && (opponent.equals("Rock"))){
//tieInt++;
tieLabel.setText("You tied" + tieInt++);
return tieInt++;
}else if(you.equals("Scissors") && (opponent.equals("Scissors"))){
//tieInt++;
tieLabel.setText("You tied" + tieInt++);
return tieInt++;
}else if(you.equals("Rock") && (opponent.equals("Scissors"))){
//winInt++;
winLabel.setText("You Won" + winInt++);
return winInt++;
}else if(you.equals("Paper") && (opponent.equals("Rock"))){
//winInt++;
//return 1;
winLabel.setText("You Won" + winInt++);
return winInt++;
}else if(you.equals("Scissors") && (opponent.equals("Paper"))){
//winInt++;
//return 1;
winLabel.setText("You Won" + winInt++);
return winInt++;
}else if(you.equals("Scissors") && (opponent.equals("Rock"))){
//loseInt++;
//return -1;
loseLabel.setText("You lose" + loseInt++);
return loseInt++;
}else if(you.equals("Rock") && (opponent.equals("Paper"))){
//loseInt++;
//return -1;
loseLabel.setText("You lose" + loseInt++);
return loseInt++;
}else
if(you.equals("Paper") && (opponent.equals("Scissors"))){
//loseInt++;
//return -1;
loseLabel.setText("You lose" + loseInt++);
return loseInt++;
}
return 0;
}
【问题讨论】:
标签: java swing user-interface label logic