GUI对象JLabel更新

时间:2015-12-07 00:31:02

标签: java swing methods jframe

我刚刚完成了GUI猜测游戏的代码编写。每次一个人玩,他们失去1“Zipoid”,并取决于他们如何做,他们要么获得一定数量的“Zipoids”,否则他们什么也得不到。无论如何,我希望amtRemaining双倍在每场比赛开始时减少1,但也会增加玩家在之前的游戏中获胜的数量,并将其代表资金JLabel。 我只是想弄清楚如何让资金JLabel在每场比赛开始时显示有多少个Zipoids。最后,我如何让新号码JButton开始游戏,但保留玩家名称和金额?谢谢!

这是我的代码:

package assignment.pkg18;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
import java.util.Random;
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import java.lang.Object;



public class GOM extends JFrame implements ActionListener, KeyListener
{
    Container content = this.getContentPane();
    //top
JTextField theGuess = new JTextField(10);
double amtRemaining = 100.0;
JLabel bankroll = new JLabel(  " Zipoids");

//bottom
JButton newplayer = new JButton("New Player"); 
JButton newnumber = new JButton("New Number"); 
JTextField thePlayer = new JTextField(20);

//center
JTextArea theoutput = new JTextArea("");

//invisible
String playerName;
int theNumber;
int numTries = 0;
int numGames = 0;



   JScrollPane scrollArea = new JScrollPane(theoutput); 



 public GOM()
{
    this.setVisible(true);
       this.setSize(500,400);
       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Guess O'Matic");

        //top panel

        JPanel p1 = new JPanel();
        p1.add(new JLabel("Make Your Guess"));
        p1.add(theGuess);
        p1.add(bankroll);
        content.add(p1, BorderLayout.NORTH);

        //bottom panel
        JPanel p2 = new JPanel();
         p2.add(newplayer);
         p2.add(thePlayer);
         p2.add(newnumber);

         content.add(p2, BorderLayout.SOUTH);

         // finishing touches
         content.add(new JLabel("  "), BorderLayout.WEST);
         content.add(new JLabel("  "), BorderLayout.EAST);  
         content.add(scrollArea, BorderLayout.CENTER);
         newplayer.addActionListener(this);
         newnumber.addActionListener(this);
         thePlayer.addKeyListener(this);
         theGuess.addKeyListener(this);

         newPlayer();
}


public void newPlayer()
{
    String empty = ("");
    theoutput.setText("Enter your name and press enter to begin");
    theoutput.setEnabled(false);
    theGuess.setEnabled(false);
    newnumber.setEnabled(false);
    newplayer.setEnabled(false);
    theGuess.setBackground(Color.WHITE);
    thePlayer.setEnabled(true);
    thePlayer.setText(empty);
    thePlayer.setBackground(Color.YELLOW);
    thePlayer.requestFocusInWindow();

}

public void addPlayer()
{
    newGame();
    playerName = thePlayer.getText();
        if (playerName.equals(""))
        {
            theoutput.setText("Please enter a player name to play");
        }
        else

        {
amtRemaining = 100;
numGames = 0;

    thePlayer.setEnabled(false);
    thePlayer.setBackground(Color.WHITE);
    newplayer.setEnabled(true);
    newnumber.setEnabled(true);
    theGuess.setEnabled(true);
    theGuess.requestFocusInWindow();
    theGuess.setBackground(Color.YELLOW);
    displayInstructions();
    }
}
private void newGame()
{
   double num = 100.0;
   num--;
    Random rnd = new Random(System.currentTimeMillis());
    numGames++;
    theNumber = rnd.nextInt(100);
    numTries = 0;
    amtRemaining = num;



}
public void displayInstructions()
    {
        theoutput.setText("I'm thinking of a number between 0 and 100"); 
        theoutput.append("\nif you guess the number in fewer than 9 tries, you'll earn");
        theoutput.append("\n\t\t 1 try \t 2.0 Zipoids");
        theoutput.append("\n\t\t 2 tries \t 1.75 Zipoids");
        theoutput.append("\n\t\t 3 tries \t 1.50 Zipoids");
        theoutput.append("\n\t\t 4 tries \t 1.25 Zipoids");
        theoutput.append("\n\t\t 5 tries \t 1.00 Zipoids");
        theoutput.append("\n\t\t 6 tries \t 0.75 Zipoids");
        theoutput.append("\n\t\t 7 tries \t 0.50 Zipoids");
        theoutput.append("\n\t\t 8 tries \t 0.25 Zipoids");
    }

 private void updateScore()
 {

     String status = bankroll.getText();
     String sm = (amtRemaining + " " + status);
     bankroll.setText(sm);


 }

 private void newGuess()
 {
     String curstr = theGuess.getText();
     int curguess = Integer.parseInt(curstr);
     numTries++;
     theoutput.append("\n");
     theoutput.append("Guess # " + numTries + "\n");
     theoutput.append("Your Current Guess " + curguess + "\n");

     if(curguess == theNumber)
     {
         gameWon();
     }
     else if(curguess < theNumber)
     {
         theoutput.append("Incorrect! Guess Higher \n" );
     }
     else if(curguess > theNumber) 
     {
         theoutput.append("Incorrect! Guess lower \n" );
     }
 }

 public void gameWon()
 {
     double winnings;
     theoutput.setText("You Win! \n" );
     theoutput.append("Amount Wagered: 1 Zipoid \n" );
     theoutput.append("" + numTries + "\n");

     switch(numTries)
        {
            case 1:
                winnings = 2.00;

                break;
            case 2:
                winnings = 1.75;
                break;
            case 3:
                winnings = 1.50;
                break;
            case 4:
                winnings = 1.25;
                break;
                 case 5:
                winnings = 1.00;
                break;
                      case 6:
                winnings = 0.75;
                break;
                           case 7:
                winnings = 0.50;
                break;
                                    case 8:
                winnings = 0.25;
                break;
            default:
                winnings = 0;
    }
     theoutput.append("Amount won " + winnings + " Zipoids \n");
     double a = amtRemaining + winnings;
     theoutput.append(playerName + "'s Amount Remaining: " + a + "\n");
     theoutput.append("Press the New Number button to continue");
     updateScore();
     theGuess.setText("");
     theGuess.setBackground(Color.WHITE);
     theGuess.setEnabled(false);
     newnumber.requestFocusInWindow();


 }

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        JButton btn = (JButton) e.getSource(); // cast value to JButton

        if(btn == newplayer)
        {
            newPlayer();
        }
        else
        {
            newnumber.setEnabled(true);
            newnumber.requestFocus();
            newnumber.setBackground(Color.YELLOW);


        }
    }

    @Override
    public void keyTyped(KeyEvent e) 
    {

    }

    @Override
    public void keyPressed(KeyEvent e) 
    {

    }

    @Override
    public void keyReleased(KeyEvent e) 
    {
      JTextField tf = (JTextField) e.getSource();
      int key = e.getKeyCode();

      if(key == KeyEvent.VK_ENTER)
      {
          if(tf == thePlayer)
          {

              addPlayer();

          }
          else
          {
              newGuess();

          }
      }
    }

}

以下是应用程序现在的样子enter image description here

0 个答案:

没有答案