gpt4 book ai didi

Java 新 GUI 问题

转载 作者:行者123 更新时间:2023-12-01 17:18:21 24 4
gpt4 key购买 nike

我发布了一个问题here不久前。我对此有不同的问题。我已经更改了图形处理方面的代码,但它仍然无法正常工作。

然而,它完美地绘制了底部框架(带有按钮等的框架)。在此之上,刽子手游戏本身应该是抽签的地方。它不是。它根本没有画任何东西。我不知道该怎么办。我快疯了,因为这是我今晚到期的最后一个项目!有人能帮帮我吗?

这是我的代码:(我省略了尽可能多的代码,但如果您需要整个程序来测试自己,grab it here

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

import java.util.Random;

public class Hangman extends JFrame {

private Hangman.Hangman_Panel canvas = new Hangman.Hangman_Panel();

// How many times you can guess the wrong letter till you lose
static final int DEAD = 7;

private int errors; // amount of errors
private String message; // Message displaying either Error or Victory
private String information; // Secondary Message
private String RealWord; // The Real word
private StringBuffer GuessWord;// The Guessed word
private Button StartBtn; // The Restart Button
private Button GoBtn; // The Go Button
private TextField LetterBox; // The LetterBox

//Contructor
public Hangman() {

this.add(canvas, BorderLayout.CENTER); // Add canvas to center
// Create a "Textbox" for the letter guessing
LetterBox = new TextField();

JPanel p = new JPanel();
p.setLayout(new FlowLayout());

p.add(StartBtn = new Button("Restart"));
p.add(new Label("Guess a letter"));
p.add(LetterBox);
p.add(GoBtn = new Button("Go"));

add(p, BorderLayout.SOUTH);

}

public static void main(String[] args) {

JFrame frame = new Hangman();

frame.setSize(300, 400);
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

}

class Hangman_Panel extends JPanel implements ActionListener {

@Override
public void paint(Graphics g) {
super.paintComponent(g);

int BaseY = 250;

//THE HANGING STAND
if (errors > 0) { //1 Error
g.drawLine(90, BaseY, 200, BaseY); //The ground
g.drawLine(125, BaseY, 125, BaseY-100); //The bar going up
g.drawLine(125, BaseY-100, 175, BaseY-100); //The sidebar
g.drawLine(175, BaseY-100, 175, BaseY-75); //The Rope
}

//THE PERSON
if (errors > 1) {
g.drawOval(170, BaseY-75, 10, 12); // The Head
}

if (errors > 2) {
g.drawLine(175, BaseY-62, 175, BaseY-45); // The Body
}

if (errors > 3) {
g.drawLine(165, BaseY-65, 175, BaseY-55); // Left Arm
}

if (errors > 4) {
g.drawLine(185, BaseY-65, 175, BaseY-55); // Right Arm
}

if (errors > 5) {
g.drawLine(170, BaseY-30, 175, BaseY-45); //Left Leg
}

if (errors > 6) { //7 Errors
g.drawLine(175, BaseY-45, 180, BaseY-30); // Right Left
}

//Show Messages/Errors
g.drawString(message, 40, BaseY+25);
g.drawString(information, 25, BaseY+45);
g.drawString(new String (GuessWord), 140, BaseY-120);

g.drawString(new String("WELCOME TO HANGMAN!"), 75, 40);
}

public void init() {

//Make buttons event listeners
StartBtn.addActionListener(this);
GoBtn.addActionListener(this);

//Startup the Game
initGame();

}

}

这也是它正在做什么的图像:

Not Drawing

如果我没记错的话,我用来将刽子手游戏部分本身添加到表单中的是以下代码行:

private Hangman.Hangman_Panel canvas = new Hangman.Hangman_Panel();

//Constructor
public Hangman() {

this.add(canvas, BorderLayout.CENTER); // Add canvas to center

最佳答案

快速尝试您的代码会出现明显的异常,您忘记在绘制方法上检查字符串是否为空

g.drawString(message, 40, BaseY+25);
g.drawString(information, 25, BaseY+45);
g.drawString(new String (GuessWord), 140, BaseY-120);

我知道你的作业很快就要到期了,但我强烈建议你learn how to read exception stack trace , use IDE, and debug your code 。将其视为对您 future 编程职业的投资。

关于Java 新 GUI 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20482850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com