java hangman swing,如何在Java Swing for Hangman游戏中获取从监听器更新的文本字段?...

I am trying to write a code from Hangman program. I am not sure if this idea is correct. I was thinking of having dashes (_ _ _ _ _) in textfield which must dynamically change when user presses buttons. for example, if the user presses button "A" in the below code, the the dashes should change to ( A _ _ _ _ ). That is the user has guessed the letter A correct.

I am still confused as how to implement this. Attaching eventListeners would be my next part.

But for now, I have to get the basic GUI working, for which I need to an idea as how to implement entire thing.

what is the better way to get this working?

here is my code as of now.

import javax.swing.*;

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.GridBagConstraints;

import java.awt.GridLayout;

import java.awt.event.*;

import java.awt.image.BufferedImage;

public class HangmanGUI {

public HangmanGUI() {

JFrame myframe= new JFrame();

myframe.getContentPane().setLayout(new BorderLayout());

JPanel myPanel = new JPanel();

myPanel.setLayout(new GridLayout(2,15));

myframe.setSize(600,600);

for (char alphabet = 'A';alphabet<='Z';alphabet++){

myPanel.add(new JButton(alphabet+""));

}

myframe.getContentPane().add(myPanel, BorderLayout.SOUTH);

myframe.setTitle("Hangman Game");

myframe.setVisible(true);

myframe.setLocationRelativeTo(null);

myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args){

new HangmanGUI();

}

}

解决方案

Yes use JFromattedTextFeild with a MaskFormatter class. MaskFormatter has a setPlaceholderCharacter('_') function to help you with. You may also need to use the InputVerifier to verify user input's validity. JFormattedTextFeild's documentation has example to show how to use an InputVerifier with it.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值