我試圖爲我的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());
}
}