我已经在这个项目上工作了很长时间,并且在离开它一个月之后才回来工作。我最后一次工作,一切都很好,我正在清理东西,以便完成它。今天当我打开eclipse时,出现了很多没有解决方案的错误,其中大多数都与括号有关,但其中一些与actionPerformed方法有关。如果有帮助,我指出错误在哪些行。只是想知道是否有人能告诉我是否存在真正的错误或者是否存在错误。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.*;
public class main extends JFrame implements ActionListener{
JButton rock;
JButton scissors;
JButton paper;
JLabel left;
ImageIcon rock1;
ImageIcon scissors1;
ImageIcon scissors2;
ImageIcon paper2;
ImageIcon paper1;
ImageIcon rock2;
ImageIcon back;
ImageIcon scissorsb;
ImageIcon youWin;
ImageIcon youLose;
ImageIcon Loading;
JLabel background;
JLabel right;
String [] Rock3 = {};
String [] RPS = {"Rock","Paper","Scissors"};
Random rand = new Random ();
Random Prize = new Random ();
int num = rand.nextInt(RPS.length);
int yourChoice = -1;
JLabel winner;
Container contentPane = this.getContentPane();
SpringLayout layout = new SpringLayout();
String Tie = "Tie";
String compwins = "Computer wins!";
String youwin = "You win!";
Font winnerFont = new Font("Font", Font.PLAIN, 35);
int yourScore = 0;
int compScore = 0;
String yourNum = Integer.toString(yourScore);
String compNum = Integer.toString(compScore);
JLabel yourPoints = new JLabel();
JLabel compPoints;
private Timer t;
private Timer t2;
private Timer t3;
public main (){
super("Rock Paper Scissors");
back = new ImageIcon("space-wallpaper-space-32897775-1920-1080.jpg");
youWin = new ImageIcon("you win.png");
youLose = new ImageIcon("you lose.png");
paper1 = new ImageIcon("paper1.png");
paper2 = new ImageIcon("paper2.png");
scissors1 = new ImageIcon("scissors1.png");
scissors2 = new ImageIcon("scissors2.png");
rock1 = new ImageIcon("rock1.png");
rock2 = new ImageIcon("rock2.png");
Loading = new ImageIcon("loadingbig.png");
background = new JLabel();
left = new JLabel();
right = new JLabel();
winner = new JLabel();
compPoints = new JLabel();
yourPoints = new JLabel();
right.setVisible(true);
this.setVisible(true);
this.setSize(1920,1080);
winner.setFont(winnerFont);
winner.setHorizontalAlignment( SwingConstants.CENTER );
contentPane.add(compPoints);
contentPane.add(yourPoints);
contentPane.setLayout(layout);
contentPane.add(winner);
contentPane.add(left);
contentPane.add(right);
rock = new JButton("Rock");
paper = new JButton ("Paper");
scissors = new JButton ("Scissors");
layout.putConstraint(SpringLayout.WEST, rock,
500,
SpringLayout.WEST, contentPane);
this.add(rock, BorderLayout.NORTH);
layout.putConstraint(SpringLayout.SOUTH, rock,
-30,
SpringLayout.SOUTH, contentPane);
layout.putConstraint(SpringLayout.WEST, rock,
40,
SpringLayout.NORTH, contentPane);
this.add(paper, BorderLayout.NORTH);
layout.putConstraint(SpringLayout.SOUTH, paper,
-30,
SpringLayout.SOUTH, contentPane);
layout.putConstraint(SpringLayout.WEST, paper,
200,
SpringLayout.NORTH, contentPane);
this.add(scissors, BorderLayout.NORTH);
layout.putConstraint(SpringLayout.SOUTH, scissors,
-30,
SpringLayout.SOUTH, contentPane);
layout.putConstraint(SpringLayout.WEST, scissors,
360,
SpringLayout.NORTH, contentPane);
rock.setBackground(Color.GRAY);
rock.setPreferredSize(new Dimension(140,50));
paper.setBackground(Color.WHITE);
paper.setPreferredSize(new Dimension(140,50));
scissors.setBackground(Color.LIGHT_GRAY);
scissors.setPreferredSize(new Dimension(140,50));
rock.repaint();
scissors.repaint();
paper.repaint();
Background();
}
public static void main(String args[]){
main framer = new main();
}
@Override
/*Error Here*/public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getSource() ==rock){
left.setIcon(rock1);
yourChoice = 0;
}else if(arg0.getSource() ==paper){
left.setIcon(paper1);
yourChoice = 1;
}else if(arg0.getSource() ==scissors){
left.setIcon(scissors1);
yourChoice = 2;
}
computerMove();
Score();
winner();
}
public void Background(){
rock.setVisible(false);
scissors.setVisible(false);
paper.setVisible(false);
contentPane.add(background);
background.setIcon(Loading);
t = new Timer(4000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
background.setIcon(back);
rock.setVisible(true);
scissors.setVisible(true);
paper.setVisible(true);
}
});
t.setRepeats(false);
t.start();
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
rock.doClick();
paper.doClick();
scissors.doClick();
rock.addActionListener(this);
paper.addActionListener(this);
scissors.addActionListener(this);
}
public void Score(){
yourPoints.setFont(winnerFont);
compPoints.setFont(winnerFont);
layout.putConstraint(SpringLayout.WEST, yourPoints,
100,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, yourPoints,
800,
SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, compPoints,
1790,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, compPoints,
800,
SpringLayout.NORTH, contentPane);
if ((num==0 && yourChoice ==0)||(num==2 && yourChoice ==2)||(num==1 && yourChoice ==1)){
layout.putConstraint(SpringLayout.NORTH, winner,
50,
SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, winner,
890,
SpringLayout.WEST, contentPane);
winner.setText(Tie);
winner.repaint();
t = new Timer(1400, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
winner.setText(null);
}
});
t.setRepeats(false);
t.start();
}else if((num==0 && yourChoice ==2)||(num==2 && yourChoice ==1)||(num==1 && yourChoice ==0)){
compScore ++;
compPoints.setText(compNum);
layout.putConstraint(SpringLayout.NORTH, winner,
50,
SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, winner,
810,
SpringLayout.WEST, contentPane);
winner.setText(compwins);
winner.repaint();
compPoints.repaint();
/*Error Here*/t2 = new Timer(1400, new ActionListener() {
@Override
/*Error Here*/public void actionPerformed(ActionEvent e) {
winner.setText(null);
/*Error Here*/}
/*Error Here*/});
t2.setRepeats(false);
t2.start();
/*Error Here*/}else if((num==2 && yourChoice ==0)||(num==0 && yourChoice ==1)||(num==1 && yourChoice ==2)){
yourScore ++;
yourPoints.setText(yourNum);
layout.putConstraint(SpringLayout.NORTH, winner,
50,
SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, winner,
850,
SpringLayout.WEST, contentPane);
winner.setText(youwin);
winner.repaint();
yourPoints.repaint();
t3 = new Timer(1400, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
winner.setText(null);
}
});
t3.setRepeats(false);
/*Error Here*/t3.start();
/*Error Here*/}
yourNum = Integer.toString(yourScore);
compNum = Integer.toString(compScore);
yourPoints.setText(yourNum);
compPoints.setText(compNum);
winner.repaint();
yourPoints.repaint();
compPoints.repaint();
winner.setForeground(Color.RED);
yourPoints.setForeground(Color.RED);
compPoints.setForeground(Color.RED);
/*Error Here*/}
public void winner(){
if(yourScore == 10){
background.setIcon(youWin);
contentPane.add(background);
background.repaint();
left.setVisible(false);
right.setVisible(false);
yourPoints.setVisible(false);
compPoints.setVisible(false);
winner.setVisible(false);
rock.setVisible(false);
paper.setVisible(false);
scissors.setVisible(false);
t = new Timer(3000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
computerMove();
Score();
winner();
}
});
t.setRepeats(false);
t.start();
}else if(compScore == 10){
background.setIcon(youLose);
contentPane.add(background);
background.repaint();
left.setVisible(false);
right.setVisible(false);
yourPoints.setVisible(false);
compPoints.setVisible(false);
winner.setVisible(false);
rock.setVisible(false);
paper.setVisible(false);
scissors.setVisible(false);
/*Error Here*/t = new Timer(3000, new ActionListener() {
@Override
/*Error Here*/public void actionPerformed(ActionEvent e) {
computerMove();
Score();
winner();
}
/*Error Here*/});
t.setRepeats(false);
t.start();
}
}
public void computerMove(){
num = rand.nextInt(RPS.length);
if (num==0){
right.setIcon(rock2);
}else if(num==1){
right.setIcon(paper2);
}else if(num==2){
right.setIcon(scissors2);
}
/*Error Here*/}
答案 0 :(得分:1)
每当你遇到与eclipse有关的奇怪问题时,请尝试执行以下操作:
Project
- > Clean...
。Project
- > Open Project
/ Close Project
。答案 1 :(得分:1)
你错过了computerMove
的右大括号。
当解决这样的问题时,尝试注释掉方法直到得到编译的内容,然后取消注释小块代码,直到找到有问题的块为止。
答案 2 :(得分:1)
看起来你错过了文件末尾的右括号}
。
相当糟糕的缩进和命名使人们很难注意到。看看Code Conventions for the Java Programming Language。