根据下面的代码,如何在运行主代码时打开绘图小程序?我认为扩展会做到这一点,但没有任何结果。我不能让程序一次执行一个身体部位。我没有足够的时间,但至少我希望它能在主要执行时立即显示刽子手。
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);
}
}
答案 0 :(得分:1)
两件事。
首先看一下Creating a GUI With JFC/Swing。
我建议您将用户界面转移到JFrame
,直到您了解如何构建用户界面的基础知识,因为小程序带来了自己的问题,如果您还没有某些背景知识可能会让您感到困惑用户界面如何运作。
事实上。从JPanel
开始,当您准备好后,将其添加到JFrame
的实例中。如果有效,您可以尝试将面板添加到JApplet