我有一个全局变量框架,即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()来完全重启帧。我的问题是,当我重置它的框架回来时非常混乱,没有任何作用。有什么建议??
答案 0 :(得分:6)
您需要为您的班级提供reset()
方法,该方法会遍历所有必填字段并将其重置为初始状态。没有一个适合所有解决方案 - 你只需要编写代码。然后可以通过添加到重置JButton的ActionListener调用此方法。
为了实现这一点,任何需要重置的字段必须是类的字段,而不是方法或构造函数的本地字段,否则重置方法将无法看到它。
修改强>
您在评论中说明:
我的startApp()调用这个方法,setUp(),理论上,不应该有每个变量的新实例吗?而我的主要问题是gui本身搞砸了,就像它没有完全或正确显示一样
我会避免创建新的变量实例,而是尝试重新设置类字段的状态,因为这样做更容易,并且更容易维护和升级。如果你正在做一个财富类型的游戏,你将不得不重新创建一些领域,尤其是显示拼图的JLabel,但这就是它。不应重新创建保存此显示的JPanel。
编辑2
请注意,您的setUp()
和startUp()
方法只不过是巨大的静态“上帝”方法。我强烈建议你不要这样做。为您的类创建和使用非静态字段和方法,并重构任何大的或尝试做太多的方法。
如果我是您的导师,我建议您废弃所有这些并使用良好的OOP原则重新启动该程序。你不会后悔这样做。
编辑3
事实上,我建议您将GUI细分为单独的类,例如Wheel类,AnswerDisplay类,WrongGuess类,Player类和Game类来运行它们,我会给每个类自己的reset()
方法,完全负责重置自己。然后,只要需要重置,Game类所要做的就是在其组成部分调用reset()
。
编辑4
回复您的最新评论:
我明白你的意思,但值得赞扬的是,我确实重置了所有的可用物品,而不是在所有情况下制作它们的新实例,除了最后一个:试图重新启动整个应用程序 - 我已经得到了一些懒惰。
这不是一个懒惰的问题,而是创建一个易于调试和易于维护的程序。此外,我怀疑你的许多变量都是静态的,而且情况并非如此。
我也看到你对“上帝”方法的看法,但是在setUp()的情况下,我需要这个,因为很多代码都是actionListener或布局代码。
不,你这是非常错的。同样,如果将程序分解为单独的类,则重置按钮的ActionListener可以如下所示:
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
game.reset();
}
});
就是这样。
startApp()可能会被分解为单独的方法,但在这种情况下它真的不值得。
我不是想要烦人但又是你错了。相信我,我可能不是一个专业人士,但我已经写了很多 Swing应用程序,有些相当复杂,如果按照我的建议重构你的程序,调试,调整会更容易,增强和维护。
答案 1 :(得分:0)
以下是用于在Java中重置TicTacToe游戏界面GUI的重置方法示例
Code for TicTacToe game
GameInterface contains the GUI
Decide class decides who wins.
l1,l2,l3 are the labels to the buttons pressed
p1,p2 are the player turns
import javax.swing.*;
public class Main1{
initValues();
public static gameinterface g;
public static Decide d;
public static int p1,p2,l1,l2,l3,l4,l5,l6,l7,l8,l9;
public static void main(String[] args){
JOptionPane.showMessageDialog(null,"Player1: Red \n Player 2: Green \n Start Playing");
g= new gameinterface();
d= new Decide();
}
//Restart Method initialize new GUI like it does in Main Method.
//Also reset the label values so the player turns to default and Buttons
//data is restored to default
public static void restart(){
g=new gameinterface();
initValues();
}
//Set Player turn and Labels to default like no button is pressed
public static void initValues(){
p2=0; p1=1; l1=0; l2=0; l3=0;l4=0;l5=0; l6=0; l7=0; l8=0; l9=0;
}
}