2016-04-24 65 views
0

我試圖爲我的java類創建一個Hangman遊戲,我正在努力如何讓每個類一起工作。在這種情況下,Im試圖將我的短語從selectPhrase方法寫入mainPanel。林編程非常新,所以任何幫助將非常感激。 謝謝無法從另一個類中調用JPanel

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

/** 
* Created by Alex on 4/22/2016. 
*/ 
``public class HangmanGame extends JFrame implements ActionListener { 

    public HangmanGame() { 
     JPanel lettersPanel = new JPanel(new GridLayout(5,8,5,5)); 

     JButton jbtA = new JButton("A"); 
     JButton jbtB = new JButton("B"); 
     JButton jbtC = new JButton("C"); 
     JButton jbtD = new JButton("D"); 
     JButton jbtE = new JButton("E"); 
     JButton jbtF = new JButton("F"); 
     JButton jbtG = new JButton("G"); 
     JButton jbtH = new JButton("H"); 
     JButton jbtI = new JButton("I"); 
     JButton jbtJ = new JButton("J"); 
     JButton jbtK = new JButton("K"); 
     JButton jbtL = new JButton("L"); 
     JButton jbtM = new JButton("M"); 
     JButton jbtN = new JButton("N"); 
     JButton jbtO = new JButton("O"); 
     JButton jbtP = new JButton("P"); 
     JButton jbtQ = new JButton("Q"); 
     JButton jbtR = new JButton("R"); 
     JButton jbtS = new JButton("S"); 
     JButton jbtT = new JButton("T"); 
     JButton jbtU = new JButton("U"); 
     JButton jbtV = new JButton("V"); 
     JButton jbtW = new JButton("W"); 
     JButton jbtX = new JButton("X"); 
     JButton jbtY = new JButton("Y"); 
     JButton jbtZ = new JButton("Z"); 

     lettersPanel.add(jbtA); 
     lettersPanel.add(jbtB); 
     lettersPanel.add(jbtC); 
     lettersPanel.add(jbtD); 
     lettersPanel.add(jbtE); 
     lettersPanel.add(jbtF); 
     lettersPanel.add(jbtG); 
     lettersPanel.add(jbtH); 
     lettersPanel.add(jbtI); 
     lettersPanel.add(jbtJ); 
     lettersPanel.add(jbtK); 
     lettersPanel.add(jbtL); 
     lettersPanel.add(jbtM); 
     lettersPanel.add(jbtN); 
     lettersPanel.add(jbtO); 
     lettersPanel.add(jbtP); 
     lettersPanel.add(jbtQ); 
     lettersPanel.add(jbtR); 
     lettersPanel.add(jbtS); 
     lettersPanel.add(jbtT); 
     lettersPanel.add(jbtU); 
     lettersPanel.add(jbtV); 
     lettersPanel.add(jbtW); 
     lettersPanel.add(jbtX); 
     lettersPanel.add(jbtY); 
     lettersPanel.add(jbtZ); 

     JPanel mainPanel = new JPanel(new BorderLayout()); 
     setLayout(new GridLayout(2,2,5,5)); 

     mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 
     mainPanel.add(lettersPanel, BorderLayout.EAST); 
     mainPanel.setBackground(Color.BLACK); 

     //mainPanel.add() 



     add(mainPanel, BorderLayout.CENTER); 
    } 



    public static void main(String[] args) { 
     JFrame frame = new HangmanGame(); 
     frame.add(new HangmanGraphics()); 
     frame.setTitle("Hangman"); 
     frame.setSize(1020, 800); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
     frame.setResizable(false); 


    } 
    static String selectPhrase() 
    { 
     String currentGuess = "*"; 
     int[] previousValues = new int[5]; 
     int totalRounds = 0; 
     //set isValid equal to false and creates a random number between 1 and 6 
     //(between 1 and 6 because previousValues is used to store the old numbers, 
     //and if between 0 and 5 were used, 0 would trip every time because an uninitialized 
     //array has 0's (null) in every position) 
     boolean isValid = false; 
     int index = (int)(Math.random() * 5 + 1); 

     //enters loop because isValid is false, so !isValid is true 
     while(!isValid) 
     { 
      //sets isValid equal to true, so if it never gets changed during the loop, it will exit next time around 
      isValid = true; 
      for(int i = 0; i < totalRounds; i++) 
      { 
       //if the randomly generated value is one of the previous ones, make isValid false 
       if(index == previousValues[i]) 
       { 
        isValid = false; 
       } 
      } 

      //if isValid is false, create a new random number between 1 and 6 
      if(!isValid) 
      { 
       index = (int)(Math.random() * 5 + 1); 
      } 
     } 

     //once a value that's never been used before is created, save it in previousValues 
     //so that it is not used again 
     previousValues[totalRounds] = index; 
     totalRounds++; 

     //return phrase at the index of the random number 
     String[] phrase = {"NULL", "Go Team", "Hello World" , "Java Rocks", "Bread Bowl" , "Soup"}; 
     return phrase[index]; 


     for(int i=0; i<phrase.length; i++) { 
      currentGuess = currentGuess.concat("?"); 
     } 
     .setText(currentGuess); 

    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 

    } 
} 

class HangmanGraphics extends JPanel { 

    JPanel graphicsPanel = new JPanel(); 

    int incorrectGuesses =0; 

    @Override 
    public void paintComponent(Graphics g){ 

     super.paintComponent(g); 
     Graphics2D g2D = (Graphics2D) g; 
     BasicStroke aStroke = new BasicStroke(15.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); 
     g2D.setStroke(aStroke); 
     g2D.setColor(Color.BLUE); 

     switch (incorrectGuesses) { 
      case 10: 
       g2D.drawLine(200, 200, 150, 250); // Left leg 
      case 9: 
       g2D.drawLine(200, 200, 250, 250); // Right leg 
      case 8: 
       g2D.drawLine(150, 150, 200, 175); // Left arm 
      case 7: 
       g2D.drawLine(250, 150, 200, 175); // Right arm 
      case 6: 
       g2D.drawLine(200, 200, 200, 150); // Torso 
      case 5: 
       g2D.drawOval(175, 100, 50, 50); // Head 
      case 4: 
       g2D.drawLine(200, 100, 200, 50);// Noose 
      case 3: 
       g2D.drawLine(200, 50, 100, 50);//Plank 
      case 2: 
       g2D.drawLine(100, 300, 100, 50); //Post 
      case 1: 
       g2D.drawLine(20, 300, 300, 300); //Stage 
     } 

     repaint(); 

     graphicsPanel.add(new HangmanGraphics()); 

    } 


} 

回答

1

你的問題,這是許多如果不是大多數新的開發,是過度使用的static和缺乏面向對象編程,以及如何使用對象的理解。在你的情況下,你試圖在一個JFrame類型對象(HangmanGame)中創建一個主要方法,然後你有一個你想要調用的靜態方法,通常是因爲你試圖找出如何從static main方法調用它。您最好爲HangManGame創建一個單獨的「驅動程序」類,其中包含主要方法。 HangManGame類然後創建一個HangManFrame(調用new HangManFrame())的實例。

記住

的2個重要的點是:

1)在你的主要方法,創建主定義的類的實例,然後調用對象的方法。通過這樣做,你不需要聲明一堆靜態方法,並且可以避免現在遇到的很多痛苦。例如,這裏是HangManGame類的定義:

package hangman; 

public class HangManGame { 
    HangManFrame frame; 

    public HangManGame(){ 
     frame = new HangManFrame(); 
    } 

    public void play() { 
     frame.setVisible(true); 
     String selectedPhrase = selectPhrase(); 
     System.out.println("Selected phrase: " + selectedPhrase); 
    } 

    public String selectPhrase() { 
     String currentGuess = "*"; 
     int[] previousValues = new int[5]; 
     int totalRounds = 0; 
     //set isValid equal to false and creates a random number between 1 and 6 
     //(between 1 and 6 because previousValues is used to store the old numbers, 
     //and if between 0 and 5 were used, 0 would trip every time because an uninitialized 
     //array has 0's (null) in every position) 
     boolean isValid = false; 
     int index = (int)(Math.random() * 5 + 1); 

     //enters loop because isValid is false, so !isValid is true 
     while(!isValid) 
     { 
      //sets isValid equal to true, so if it never gets changed during the loop, it will exit next time around 
      isValid = true; 
      for(int i = 0; i < totalRounds; i++) 
      { 
       //if the randomly generated value is one of the previous ones, make isValid false 
       if(index == previousValues[i]) 
       { 
        isValid = false; 
       } 
      } 

      //if isValid is false, create a new random number between 1 and 6 
      if(!isValid) 
      { 
       index = (int)(Math.random() * 5 + 1); 
      } 
     } 

     //once a value that's never been used before is created, save it in previousValues 
     //so that it is not used again 
     previousValues[totalRounds] = index; 
     totalRounds++; 

     //return phrase at the index of the random number 
     String[] phrase = {"NULL", "Go Team", "Hello World" , "Java Rocks", "Bread Bowl" , "Soup"}; 
     return phrase[index]; 
    } 

    public static void main(String[] args) { 
     HangManGame game = new HangManGame(); 
     game.play(); 
    } 
} 

注意的主要方法執行的唯一任務是創建HangManGame類的一個實例,並調用play()方法上object..that就是它!

另請注意,selectPhrase()方法未聲明爲靜態方法,因爲它不是必需的。

2)對於您所有的類,您需要訪問並需要能夠調用方法的對象應該在方法之外聲明爲「類範圍的實例變量」。例如,請注意HangManFrame是如何在HangManGame類中聲明的,但在方法之外。然後該幀被創建並分配在構造函數中的`HangManGame

public class HangManGame { 
    HangManFrame frame; 

    public HangManGame(){ 
     frame = new HangManFrame(); 
    } 
... 
} 

這樣做可以讓你調用框架上的方法,從內任何HangManGame方法。

我已經稍微重構了你的HangManFrameHangManGraphics類,遵循類似的策略,包括在下面。

最後,你應該開始對HangManGame類中創建的其他方法,也許像nextMove(),或updateBoard()也好,由驅動,從play()方法調用。

HangManFrame class:

包hangman;

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class HangManFrame extends JFrame implements ActionListener { 
    JPanel lettersPanel; 

    public HangManFrame() { 
     lettersPanel = new JPanel(new GridLayout(5, 8, 5, 5)); 

     JButton jbtA = new JButton("A"); 
     JButton jbtB = new JButton("B"); 
     JButton jbtC = new JButton("C"); 
     JButton jbtD = new JButton("D"); 
     JButton jbtE = new JButton("E"); 
     JButton jbtF = new JButton("F"); 
     JButton jbtG = new JButton("G"); 
     JButton jbtH = new JButton("H"); 
     JButton jbtI = new JButton("I"); 
     JButton jbtJ = new JButton("J"); 
     JButton jbtK = new JButton("K"); 
     JButton jbtL = new JButton("L"); 
     JButton jbtM = new JButton("M"); 
     JButton jbtN = new JButton("N"); 
     JButton jbtO = new JButton("O"); 
     JButton jbtP = new JButton("P"); 
     JButton jbtQ = new JButton("Q"); 
     JButton jbtR = new JButton("R"); 
     JButton jbtS = new JButton("S"); 
     JButton jbtT = new JButton("T"); 
     JButton jbtU = new JButton("U"); 
     JButton jbtV = new JButton("V"); 
     JButton jbtW = new JButton("W"); 
     JButton jbtX = new JButton("X"); 
     JButton jbtY = new JButton("Y"); 
     JButton jbtZ = new JButton("Z"); 

     lettersPanel.add(jbtA); 
     lettersPanel.add(jbtB); 
     lettersPanel.add(jbtC); 
     lettersPanel.add(jbtD); 
     lettersPanel.add(jbtE); 
     lettersPanel.add(jbtF); 
     lettersPanel.add(jbtG); 
     lettersPanel.add(jbtH); 
     lettersPanel.add(jbtI); 
     lettersPanel.add(jbtJ); 
     lettersPanel.add(jbtK); 
     lettersPanel.add(jbtL); 
     lettersPanel.add(jbtM); 
     lettersPanel.add(jbtN); 
     lettersPanel.add(jbtO); 
     lettersPanel.add(jbtP); 
     lettersPanel.add(jbtQ); 
     lettersPanel.add(jbtR); 
     lettersPanel.add(jbtS); 
     lettersPanel.add(jbtT); 
     lettersPanel.add(jbtU); 
     lettersPanel.add(jbtV); 
     lettersPanel.add(jbtW); 
     lettersPanel.add(jbtX); 
     lettersPanel.add(jbtY); 
     lettersPanel.add(jbtZ); 

     JPanel mainPanel = new JPanel(new BorderLayout()); 
     setLayout(new GridLayout(2, 2, 5, 5)); 

     mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 
     mainPanel.add(lettersPanel, BorderLayout.EAST); 
     mainPanel.setBackground(Color.BLACK); 

     add(mainPanel, BorderLayout.CENTER); 
     add(new HangmanGraphics()); 
     setTitle("Hangman"); 
     setSize(1020, 800); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setResizable(false); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     System.out.println("Received actionPerformed event: " + e.getActionCommand()); 
    } 
} 

HangManGraphics類:

包劊子手;

import javax.swing。; import java.awt。;

類HangmanGraphics擴展JPanel {

JPanel graphicsPanel = new JPanel(); 

int incorrectGuesses =0; 

@Override 
public void paintComponent(Graphics g){ 

    super.paintComponent(g); 
    Graphics2D g2D = (Graphics2D) g; 
    BasicStroke aStroke = new BasicStroke(15.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); 
    g2D.setStroke(aStroke); 
    g2D.setColor(Color.BLUE); 

    switch (incorrectGuesses) { 
     case 10: 
      g2D.drawLine(200, 200, 150, 250); // Left leg 
     case 9: 
      g2D.drawLine(200, 200, 250, 250); // Right leg 
     case 8: 
      g2D.drawLine(150, 150, 200, 175); // Left arm 
     case 7: 
      g2D.drawLine(250, 150, 200, 175); // Right arm 
     case 6: 
      g2D.drawLine(200, 200, 200, 150); // Torso 
     case 5: 
      g2D.drawOval(175, 100, 50, 50); // Head 
     case 4: 
      g2D.drawLine(200, 100, 200, 50);// Noose 
     case 3: 
      g2D.drawLine(200, 50, 100, 50);//Plank 
     case 2: 
      g2D.drawLine(100, 300, 100, 50); //Post 
     case 1: 
      g2D.drawLine(20, 300, 300, 300); //Stage 
    } 

    repaint(); 

    graphicsPanel.add(new HangmanGraphics()); 

} 

}