Paso 3: El programa
Y aquí está mi programa (redoble...),
Import java.awt.*;
Import java.awt.event.*;
Import java.util.Random;
Import javax.swing.*;
clase GUI_RPS
{
Estas son las variables miembro. Están disponibles en una instancia de una clase.
JFrame RPS;
JButton piedra, papel, tijeras;
PRDs JTextField;
JTextField CRPS;
Resultados de JTextField;
Contador de JTextField;
JPanel hInput;
char hChoice = ' ';
char cfavorecida = ' ';
Char [] cc = {'R', 'P', de '};
int r3;
lazo de int = 0;
int computerwins = 0;
int humanwins = 0;
RGen aleatorio = new Random();
Se trata de un constructor. Se llama cuando se llama a GUI_RPS() nueva de principal.
GUI_RPS() pública
{
RPS = new JFrame ("piedra, papel, tijeras");
RPS.setSize (600, 600);
Roca = nuevo JButton("Rock");
Papel = nuevo JButton("Paper");
Tijeras = nuevo JButton("Scissors");
hInput = new JPanel();
PRDs = nuevo JTextField(20);
CRP = nuevo JTextField(20);
Resultados = new JTextField(20);
Contador = nuevo JTextField(30);
Rock.addActionListener (new ActionListener()
{
público void actionPerformed (ActionEvent e)
{
HRPS.setText ("usted eligió a roca");
hChoice = 'R';
Play();
}
});
Paper.addActionListener (new ActionListener()
{
público void actionPerformed (ActionEvent e)
{
HRPS.setText ("elige papel");
hChoice = 'P';
Play();
}
});
Scissors.addActionListener (new ActionListener()
{
público void actionPerformed (ActionEvent e)
{
HRPS.setText ("elige tijeras");
hChoice = de ';
Play();
}
});
hInput.setLayout (nuevo FlowLayout());
hInput.add(HRPS);
hInput.add(CRPS);
hInput.add(Counter);
hInput.add(Rock);
hInput.add(Paper);
hInput.add(Scissors);
RPS.getContentPane () Add (hInput, BorderLayout.CENTER);
RPS.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
RPS.pack();
RPS.setVisible(true);
}
Método de agregado
play() void privado
{
R3 = rGen.nextInt(3);
cfavorecida = cc [r3];
interruptor (cfavorecida)
{
caso 'R':
CRPS.setText ("equipo eligió a roca");
rotura;
caso 'P':
CRPS.setText ("equipo eligió papel");
rotura;
de caso ':
CRPS.setText ("equipo eligió Scisssors");
rotura;
}
if(hChoice == cChoice)
{
lazo ++;
}
Si (hChoice == 'R' & & cfavorecida == de ')
{
humanwins ++;
}
Si (hChoice == 'P' & & cfavorecida == 'R')
{
humanwins ++;
}
Si (hChoice == de ' & & cfavorecida == 'P')
{
humanwins ++;
}
Si (cfavorecida == 'R' & & hChoice == de ')
{
computerwins ++;
}
Si (cfavorecida == 'P' & & hChoice == 'R')
{
computerwins ++;
}
Si (cfavorecida == de ' & & hChoice == 'P')
{
computerwins ++;
}
Counter.setText ("los lazos =" + lazo + ", gana el humano =" + humanwins + ", gana el equipo =" + computerwins);
}
principal vacío estático público (String [] args)
{
Establecer la apariencia parece Java Swing
tratar de
{
UIManager.setLookAndFeel (
UIManager.getCrossPlatformLookAndFeelClassName());
} catch (excepción e) {}
Aplicación de GUI_RPS = new GUI_RPS();
}
}