-1
Good afternoon,
I’m having trouble running my program in Java, when I compile there is no problem, but when I try to run, says that can not find the main(String[]),
(class and file have the same name)
//Bibliotecas import java.awt.; import java.awt.event.; import javax.swing.*; import java.util.Random;
class setup extends Object{
private String jogador1;
private String jogador2;
private int pontos1;
private int pontos2;
public void setplayer1(String x){this.player1 = x;} public void setlayer2(String x){this.player2 = x;} public void setpontos1(int x){this.pontos1 = x;} public void setpontos2(int x){this.pontos2 = x;}
public String getplayer1(){Return player1;} public String getplayer2(){Return player2;} public int getpontos1(){Return points1;} public int getpontos2(){Return points2;} }
public class game_prisoner extends Frame Implements Actionlistener
{ //Global Data
//setup jogador1 = new setup();
//setup jogador2 = new setup();
static setup j = new setup();
private static final long serialVersionUID = 1L;
//Constantes
public static final int WINPOINTS = 10;
public static final int ENDGAMEDISABLEDBUTTONS = 16;
public static final String STRINGFORMAT = "%-10s %-7s %-10s %-7s\n";
String mitem1[]= {"Setup", "Jogar", "Listar", "Gráficos","-", "Abandonar"};
String mitem2[]= {"Programa", "Linguagem","-", "Autor"};
MenuBar barra;
Menu menu, menu2;
MenuItem item1[] = new MenuItem[mitem1.length];
MenuItem item2[] = new MenuItem[mitem2.length];
String st[] = {"lb0","lb1","lb2","I","lb4","lb5","lb6","lb7","lb8","II","III","IV","lb12","lb13","lb14","V","VI","VII","VIII",
"IX","lb20","X","XI","XII", "XIII", "XIV", "XV","XVI"};
Button but[] = new Button[st.length];
String st_bot[] = {"Começar 1", "Acabar 1", "Começar 2", "Acabar 2", "Abandonar"};
Button but_bot[] = new Button[st_bot.length];
Panel meio, sul;
Label lb;
Font fonte = new Font("TimesRoman", Font.BOLD, 22);
Font fonte2 = new Font("TimesRoman", Font.BOLD, 50);
Graphics stats;
int playerFirstButtonIndex = -1;
boolean firstGame = true;
int player;
int starterPlayer;
boolean isPlaying;
TextArea ta;
int numOfGames;
int desactivatedButtons;
public jogo_prisioneiro() {
super("Jogo Prisioneiro");
meio = novopanelcentro(but, st, fonte2, Color.blue, Color.green);
add(meio,BorderLayout.CENTER);
sul = novopanelsul(but_bot, st_bot, fonte, Color.blue, Color.green);
add(sul,BorderLayout.SOUTH);
barra = criamenu(menu, "File", item1, mitem1,
menu2, "About", item2, mitem2);
isPlaying = false;
numOfGames = 0;
ta = new TextArea(10,50);
ta.setFont(new Font("monospaced", Font.PLAIN, 12));
String string1 = String.format(STRINGFORMAT, "Jogador 1", "Pontos", "Jogador 2", "Pontos");
ta.append(string1);
Random ale = new Random();
starterPlayer = ale.nextInt(2);
setMenuBar(barra);
setResizable(false);
setSize(1280, 720);
setVisible(true);
}
public void actionPerformed (ActionEvent e){
if (e.getSource() == item1[0]){
//int opcao = CriarDialogoEntrada("\n\tNome Jogador 1: ", "Dialogo Setup", JOptionPane.QUESTION_MESSAGE);
j.setjogador1(CriarDialogoEntrada("\n\tNome jogador 1: ", "Dialogo Setup", JOptionPane.QUESTION_MESSAGE));
System.out.printf("%s\n",j.getjogador1());
j.setjogador2(CriarDialogoEntrada("\n\tNome jogador 2: ", "Dialogo Setup", JOptionPane.QUESTION_MESSAGE));
System.out.printf("%s\n",j.getjogador2());
item1[1].setEnabled(true);
item1[2].setEnabled(true);
item1[3].setEnabled(true);
}
if (e.getSource() == item1[1]){
newGame();
}
if(e.getSource() == item1[2]){
CriaDialogoListagem1(ta, "Listagem", JOptionPane.INFORMATION_MESSAGE);
}
if (e.getSource() == item1[5]) {
int opcao = CriarDialogoConfirmacao("\n\tConfirma que quer sair\nSair da aplicacao\n","Dialogo abandonar",JOptionPane.YES_NO_OPTION);
if (opcao == JOptionPane.YES_OPTION){
System.gc();
System.exit(0);
}//if
}//it5
if (e.getSource() == item2[0]) {
CriaDialogoSaida("\n\tJogo : Jogo do Prisioneiro\n\tVersao : 1.0\n\tData : 14-03-2017\n\tProjecto: Jogo do Prisioneiro\n\tUFCD : 5117","Dialogo do Programa",JOptionPane.INFORMATION_MESSAGE);
}//end
if (e.getSource() == item2[1]) {
CriaDialogoSaida("\n\tLinguagem : Java\n\tVersão : 8.0X\n\tSoftware House : Oracle Corporation\n\tLema : Write Once Run Anywahere","Diálogo Linguagem ",JOptionPane.INFORMATION_MESSAGE);
}//end
if (e.getSource() == item2[3]) {
CriaDialogoSaida("\n\tNome: Trainee\n\tFuncao : Java training\n\tMaxima: Makes things easy","Diálogo Autor",JOptionPane.INFORMATION_MESSAGE);
}//end
if(e.getSource() == but_bot[0])
{
int ct;
String output = j.getjogador1();
CriaDialogoSaida("A jogar: " +output,"Dialogo",JOptionPane.INFORMATION_MESSAGE);
but_bot[0].setEnabled(false);
but_bot[1].setEnabled(true);
isPlaying = true;
player = 1;
}
if(e.getSource() == but_bot[1])
{
int ct;
String output = j.getjogador2();
CriaDialogoSaida("A jogar: " +output,"Dialogo",JOptionPane.INFORMATION_MESSAGE);
but_bot[1].setEnabled(false);
but_bot[2].setEnabled(true);
playerFirstButtonIndex = -1;
isPlaying = false;
}
if(e.getSource() == but_bot[2])
{
int ct;
String output = j.getjogador2();
CriaDialogoSaida("A jogar: " +output,"Dialogo",JOptionPane.INFORMATION_MESSAGE);
but_bot[2].setEnabled(false);
but_bot[3].setEnabled(true);
isPlaying = true;
player = 2;
}
if(e.getSource() == but_bot[3])
{
int ct;
String output = j.getjogador1();
CriaDialogoSaida("A jogar: " +output,"Dialogo",JOptionPane.INFORMATION_MESSAGE);
but_bot[3].setEnabled(false);
but_bot[0].setEnabled(true);
playerFirstButtonIndex = -1;
isPlaying = false;
}
if(e.getSource() == but_bot[4])
{
int opcao = CriarDialogoConfirmacao("\n\tConfirma que quer sair\nSair da aplicacao\n","Dialogo abandonar",JOptionPane.YES_NO_OPTION);
if (opcao == JOptionPane.YES_OPTION){
System.gc();
System.exit(0);
}
}
//GAME BUTTONS EVENTS
if(e.getSource() == but[3]) {
clickButton(3);
}
if(e.getSource() == but[9]) {
clickButton(9);
}
if(e.getSource() == but[10]) {
clickButton(10);
}
if(e.getSource() == but[11]) {
clickButton(11);
}
if(e.getSource() == but[15]) {
clickButton(15);
}
if(e.getSource() == but[16]) {
clickButton(16);
}
if(e.getSource() == but[17]) {
clickButton(17);
}
if(e.getSource() == but[18]) {
clickButton(18);
}
if(e.getSource() == but[19]) {
clickButton(19);
}
if(e.getSource() == but[21]) {
clickButton(21);
}
if(e.getSource() == but[22]) {
clickButton(22);
}
if(e.getSource() == but[23]) {
clickButton(23);
}
if(e.getSource() == but[24]) {
clickButton(24);
}
if(e.getSource() == but[25]) {
clickButton(25);
}
if(e.getSource() == but[26]) {
clickButton(26);
}
if(e.getSource() == but[27]) {
clickButton(27);
}
}
// PRIVATE METHODS
/*
/ CREATE A NEW GAME
*/
private void newGame() {
//Increase number of games
numOfGames++;
desactivatedButtons = 0;
isPlaying = false;
int oldStarter = starterPlayer;
if(numOfGames > 1 ) {
if(starterPlayer == 1)
starterPlayer = 0;
else
starterPlayer = 1;
CriaDialogoSaida("Jogo passado começou o jogador " + (oldStarter+1) + " este jogo começa o jogador " + (starterPlayer+1),"NOVO JOGO",JOptionPane.INFORMATION_MESSAGE);
}
resetGameButtons();
if (starterPlayer == 0){
but_bot[0].setEnabled(true);
but_bot[1].setEnabled(false);
but_bot[2].setEnabled(false);
but_bot[3].setEnabled(false);
but_bot[4].setEnabled(true);
}
else
{
but_bot[0].setEnabled(false);
but_bot[1].setEnabled(false);
but_bot[2].setEnabled(true);
but_bot[3].setEnabled(false);
but_bot[4].setEnabled(true);
}
}
/*
/ RESET GAME BUTTONS
*/
private void resetGameButtons() {
for(int ct = 0; ct < st.length; ct++) {
playerFirstButtonIndex=-1;
if(ct != 0 && ct != 1 && ct != 2 && ct != 4 && ct != 5 && ct != 6 && ct != 7 && ct != 8 && ct !=12 && ct != 13 && ct != 14 && ct != 20 ) {
but[ct].setEnabled(true);
}
}
}
/*
/ Finish the game
*/
private void finishGame() {
String j1 = j.getjogador1();
String j2 = j.getjogador2();
int p1 = j.getpontos1();
int p2 = j.getpontos2();
if(player == 1){
CriaDialogoSaida("\n\tO jogador " + j2 + " ganhou", "WINNER", JOptionPane.INFORMATION_MESSAGE);
j.setpontos2(WINPOINTS);
j.setpontos1(0);
} else {
CriaDialogoSaida("\n\tO jogador " + j1 + " ganhou", "WINNER", JOptionPane.INFORMATION_MESSAGE);
j.setpontos1(WINPOINTS);
j.setpontos2(0);
}
String string2 = String.format(STRINGFORMAT, j.getjogador1(), j.getpontos1(), j.getjogador2(), j.getpontos2());
ta.append(string2);
//DAR DISABLE AOS BUTOES
for(int y=0; y < st_bot.length - 1; y++){
but_bot[y].setEnabled(false);
}
}
/*
/ Check if the game is finished (if the disabled buttons is equal to ENDGAMEDISABLEDBUTTONS)
*/
private boolean checkEndGame() {
if(desactivatedButtons == ENDGAMEDISABLEDBUTTONS)
return true;
else
return false;
}
/*
/ CLICK A SPECIFIC GAME BUTTON (buttonIndex)
*/
private void clickButton(int buttonIndex) {
if(!isPlaying) {
CriaDialogoSaida("\n\tTens que começar a tua jogada", "CHECKLINE", JOptionPane.INFORMATION_MESSAGE);
return ;
}
if(playerFirstButtonIndex != -1) {
checkLine(buttonIndex, playerFirstButtonIndex);
} else {
//TODO: DISABLE BUTTON
but[buttonIndex].setEnabled(false);
playerFirstButtonIndex = buttonIndex;
desactivatedButtons++;
}
if(checkEndGame()) {
finishGame();
}
}
/*
/ CHECK IF THE PLAYER CAN DESACTIVATE THE BUTTON
*/
private void checkLine(int actualButtonIndex, int firstButtonIndex) {
boolean canDesactivate = true;
if( (firstButtonIndex == 3 && actualButtonIndex != 3) ||
( (firstButtonIndex >=9 && firstButtonIndex <= 11) && (actualButtonIndex < 9 || actualButtonIndex > 11) ) ||
( (14 < firstButtonIndex && firstButtonIndex < 20) && (actualButtonIndex <= 14 || actualButtonIndex >= 20) ) ||
( (20 < firstButtonIndex && firstButtonIndex < 28) && (actualButtonIndex <= 20 || actualButtonIndex >= 28) ))
{
CriaDialogoSaida("\n\tNão podes mudar de linha", "CHECKLINE", JOptionPane.INFORMATION_MESSAGE);
} else {
but[actualButtonIndex].setEnabled(false);
desactivatedButtons++;
}
}
public void graph(Graphics gra) {
gra.setColor(Color.red);
gra.fillOval(330-5,220-5,10,10);
}
private Panel novopanelcentro(Button bt[], String sr[], Font font, Color cor, Color cor2){
int ct;
Panel mid;
mid = new Panel(new GridLayout(4,7,10,5));
for (ct=0; ct<but.length; ct++)
{
if(ct == 0 || ct == 1 || ct == 2 || ct == 4 || ct == 5 || ct == 6 || ct == 7 || ct == 8 || ct ==12 || ct == 13 || ct == 14 || ct == 20 )
{
lb = new Label(" ");
mid.add(lb);
}
else
{
bt[ct] = new Button(sr[ct]);
bt[ct].setFont(font);
bt[ct].setBackground(cor);
bt[ct].setForeground(cor2);
bt[ct].setEnabled(false);
bt[ct].addActionListener(this);
mid.add(bt[ct]);
}
}
return mid;
}
private Panel novopanelsul (Button bt[], String sr[], Font font, Color cor, Color cor2){
int ct;
Panel bot;
bot = new Panel(new FlowLayout(FlowLayout.CENTER,25,10));
for (ct=0; ct<but_bot.length; ct++){
bt[ct] = new Button(sr[ct]);
bt[ct].setFont(font);
bt[ct].setBackground(cor);
bt[ct].setForeground(cor2);
bt[ct].setEnabled(false);
bt[ct].addActionListener(this);
bot.add(bt[ct]);
}
return bot;
}
private MenuBar criamenu (Menu mu1, String st1, MenuItem mi1[], String dados1[], Menu mu2, String st2, MenuItem mi2[], String dados2[]){
MenuBar mbarra1;
int ct;
mbarra1 = new MenuBar();
mu1 = new Menu(st1);
for (ct=0;ct<dados1.length;ct++){
mi1[ct]= new MenuItem(dados1[ct]);
mi1[ct].addActionListener(this);
mu1.add(mi1[ct]);
}
mbarra1.add(mu1);
mi1[1].setEnabled(false);
mi1[2].setEnabled(false);
mi1[3].setEnabled(false);
mu2 = new Menu(st2);
for (ct=0;ct<dados2.length;ct++){
mi2[ct]= new MenuItem(dados2[ct]);
mi2[ct].addActionListener(this);
mu2.add(mi2[ct]);
}
mbarra1.add(mu2);
return mbarra1;
}
private int CriarDialogoConfirmacao(String ms, String t, int i) {
int devolve = 0;
devolve = JOptionPane.showConfirmDialog(null,
ms,
t,
i);
return devolve;
}
private void CriaDialogoSaida(String ms, String t, int i) {
JOptionPane.showMessageDialog(null, ms, t, i);
;
}
private String CriarDialogoEntrada(String ms, String t, int i)
{
String devolve = "";
devolve = JOptionPane.showInputDialog(null, ms, t, i);
return devolve;
}
private void CriaDialogoListagem1(TextArea text, String ms, int i) {
JOptionPane.showMessageDialog(null,text,ms,i);
}
//MAIN
public static void main (String[] args){
jogo_prisioneiro app = new jogo_prisioneiro();
app.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.gc();
System.exit(0);
}
});
}
}
Can someone tell me what’s wrong?
extends Object is useless. Every java class already extends Object mandatorily, msm q vc never define, this is java working rule.
– Blastoise Opressor