我有一个全局变量框架,即 JFrame。
public static void setUp(final Wheel []player, final phraser p) throws IOException {
final JPanel scorePanel=new JPanel();
final JPanel namePanel=new JPanel();
panel=new JPanel(new GridLayout(1,player.length,1,1));
panel1=new JPanel();
panel2=new JPanel();
panel3=new JPanel(new GridLayout(2,1,1,1));
panel3.add(new JLabel("Dead letters/phrases:"));
panel3.add(LettersOrPhGuessed);
JMenuBar menuBar=new JMenuBar();
frame.setJMenuBar(menuBar);
clock=new JMenu();
JMenu file=new JMenu("File");
JMenuItem exit=new JMenuItem("Exit");
JMenuItem reset=new JMenuItem("Reset");
file.add(exit);
file.add(reset);
menuBar.add(file);
menuBar.add(clock);
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
reset.addActionListener(new ActionListener(){//help me
public void actionPerformed(ActionEvent e){
frame.removeAll();
frame.validate();
frame.setVisible(false);
try {
startApp();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
//for spinner
spinQuote.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(!first){
String result="You have already spun: "+check;
JOptionPane.showMessageDialog(null,result);
}
else
check=player[whichPlayer].spin();
pause=false;
}
});
//to buy a vowel
buyVowel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
buyVow=true;
if(player[whichPlayer].getPotentialScore()>=250||player[whichPlayer].getTurnScore()>=250){
buyVowel(player,whichPlayer,p);
}
else
JOptionPane.showMessageDialog(null,"Sorry, you don't have enough money to buy a vowel");
pause=false;
}
});
//to solve baord
solv.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
pause=false;
solvBoard=true;
boolean temp=false;
while(temp==false){
enterSolution();
if(cancelAnswer){
pause2=false;
go=true;
return;
}
else if(guess.equals("-1")){
JOptionPane.showMessageDialog(null,"Sorry, you took too long");
temp=true;
}
else if(guess.length()>1){
if(p.checkGuessedAnswer(guess))
JOptionPane.showMessageDialog(null,"Sorry, that phrase has already been guessed");
else if(player[whichPlayer].checkForWin(guess)){
newGame=true;
replaceCorrectLetter(underScores,guess);
player[whichPlayer].setFinalScore(player[whichPlayer].getFinalScore()+player[whichPlayer].getPotentialScore()+player[whichPlayer].getTurnScore());
JOptionPane.showMessageDialog(null,"Congrats you won the round!");
whichPlayer=0;
temp=true;
}
else{
JOptionPane.showMessageDialog(null,"Sorry, better luck next time!");
p.addGuessedAnswer(guess);
LettersOrPhGuessed.setText(p.getAlreadyGuessed()+"");
temp=true;
}
}
else
JOptionPane.showMessageDialog(null,"Sorry,That is an invalid guess!");
}
go=false;
pause2=false;
}
});
scorePanel.add(new JLabel("Score Board:"));
panel4 = test;
panel4.setSize(800, 600);//this does nothing in the program
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
namePanel.add(playerName,gbc);
frame.getContentPane().add(namePanel, gbc);
frame.getContentPane().add(scorePanel, gbc);
frame.getContentPane().add(panel, gbc);
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
frame.getContentPane().add(panel4, gbc);
gbc.weighty = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
frame.getContentPane().add(panel2, gbc);
frame.getContentPane().add(panel1, gbc);
frame.getContentPane().add(panel3, gbc);
frame.setVisible(true);
}
startApp 方法
public static void startApp() throws IOException{
howManyPlayers();
Wheel[] player=new Wheel[numberOfPlayers];
fixScore(player);
phraser p=new phraser();
answer=p.pickPhrase();
for(int i = 0; i < player.length; i++) {
player[i] = new Wheel(answer);
}
setUp(player,p);
setName(player);
scoreBoard(player);
panel2.add(buyVowel);
panel2.add(spinQuote);
panel2.add(solv);
int gamesplayed=0;
while(gamesplayed<3){
if(gamesplayed!=0)
intializeAnswer(player,p);
underScores=drawSpaces();
check=0;
whichPlayer=0;
newGame=false;
test.intializeWheel();
p.setPositionStart(0);
while(newGame==false){
go=true;
intro(player);
nextTurn=false;
cancelGuess=false;
cancelAnswer=false;
first=true;
while(go){
int starter=p.getPositionStart();
playerName.setText("It is "+player[whichPlayer].getName()+"'s turn!");
pause=true;
pause2=true;
while(pause){//makes sure user hits spin or solve before it does anything
try {
Thread.sleep(250);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (pause==false){
if(solvBoard==false&&buyVow==false&&cancelGuess==false)
test.spinner(starter,player[whichPlayer].getPosition());//wheel thing
break;
}
}
cancelGuess=false;
cancelAnswer=false;
if(buyVow){buyVow=false;}
else if(solvBoard){
while(pause2){//makes sure to pause for user
try {
Thread.sleep(250);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (pause2==false)
break;
}
solvBoard=false;
}
else if(check==-1){
JOptionPane.showMessageDialog(null,"Bankrupt!" );
player[whichPlayer].setTurnScore(0);
player[whichPlayer].setPotentialScore(0);
go=false;
}
else if(check==-2){
JOptionPane.showMessageDialog(null,"Sorry, you lost your turn!");
player[whichPlayer].setTurnScore(0);
go=false;
}
else if(check==-3){
JOptionPane.showMessageDialog(null,"Spin again!");
}
else{
if(first)
JOptionPane.showMessageDialog(null,"You spun: "+check);
go=interpretAnswer(player,underScores,p);
}
if(newGame==false)
updateScoreBoardInTurn(player);
p.setPositionStart(player[whichPlayer].getPosition());
}
if(newGame==false){
player[whichPlayer].setPotentialScore((player[whichPlayer].getPotentialScore()+player[whichPlayer].getTurnScore()));
updateScoreBoard(player);
if(whichPlayer==player.length-1)
whichPlayer=0;
else
whichPlayer++;
}
player[whichPlayer].setTurnScore(0);
}
for(int i=0;i<player.length;i++){
player[i].setPotentialScore(0);
}
updateScoreBoardAfter(player);
clear(underScores);
reset(player,p);
LettersOrPhGuessed.setText(p.getAlreadyGuessed()+"");
gamesplayed++;
}
calcWinner(player);
results(player);
}
如您所见,我在重置按钮中添加了一个 actionListener,但我不确定如何通过调用 startApp() 来完全重启框架。我的问题是,当我重置框架时,它回来时非常混乱,没有任何效果。有什么建议么??