-1
以下給出我的代碼當我運行主代碼時,如何讓paint applet打開?我認爲擴展會做的伎倆,但沒有出現。我無法讓程序一次執行一個身體部分。我沒有足夠的時間,但至少我希望它在執行主體後立即顯示hang子手的繪畫。JApplet無法打開控制檯程序
import java.awt.Graphics;
import java.util.Scanner;
import javax.swing.JApplet;
public class HangmanLogic extends HangmanGuy {
public static void main(String[] args) {
int count = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter a 4 or 5 letter word and the computer will play hangman against you!");
String word = in.nextLine();
char[] letter = word.toCharArray();
for (int i = 0; i < letter.length; i++) {
letter[i] = 'a';
}
for (int i = 0; i < word.length(); i++){
for (int j = 48; j < 122; j++) {
count++;
if (letter[i] == word.charAt(i)) {
break;
} else {
letter[i] = (char)((int) j + 1);
}
}
}
System.out.println("Attempt to solve: " + count);
System.out.println("Your word is: ");
for (char letters : letter) {
System.out.print(letters);
}
}
}
import java.awt.Graphics;
import javax.swing.JApplet;
import java.awt.*;
public class HangmanGuy extends JApplet
{
public void paint (Graphics Page)
{
//gallows
Page.drawLine(0,300,20,300);
Page.drawLine(10,40,10,300);
Page.drawLine(10,40,80,40);
Page.drawLine(80,40,80,55);
//torso
Page.drawOval(50,55,50,55);
Page.drawOval(50,100,50,100);
//left arm and hand
Page.drawLine(50,150,40,110);
Page.drawLine(40,110, 45,100);
Page.drawLine(40,110, 25,100);
Page.drawLine(40,110, 25,115);
//right arm and hand
Page.drawLine(100,150,120,110);
Page.drawLine(120,110, 115,95);
Page.drawLine(120,110, 125,95);
Page.drawLine(120,110, 135,115);
//left leg and foot
Page.drawLine(80,200,100,250);
Page.drawLine(100,250, 115,260);
//right leg and foot
Page.drawLine(75,200,60,250);
Page.drawLine(60,250,45,260);
}
}
您錯誤地使用了繼承。這不是它如何工作,或者你應該如何嘗試它的工作。當你知道一個新的類將滿足「是 - 一個」關係,而你不知道的時候,你只能使用繼承。另外,從瀏覽器打開小程序。 –
這是您第二次嘗試此操作,儘管之前建議您不要混用控制檯和GUI概念。 – MadProgrammer
爲什麼要麻煩提出建議,如果他只是忽略它們。 –