【发布时间】:2013-12-21 01:07:41
【问题描述】:
我在 JPanel 中为我正在制作的刽子手游戏创建多个 JTextField 和 JLabel 时遇到了困难。我试图在 JLabel 中将用户输入显示为“使用的字母”。我已经评论了下面没有显示的区域。提前致谢。
/*PACKAGE DECLARATION*/
package Game;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/************************
* GAME MECHANICS CLASS *
* **********************/
public class GameMechanics {
/* STATIC DECLARATIONS */
static JPanel jp;//panel
static JLabel jl;//label
static JTextField tf;//text field
static String input = "";
/*********************
* USER INPUT METHOD *
* *******************/
public void userInput() {
jp = new JPanel();
jp.setLayout(new BoxLayout(jp, BoxLayout.Y_AXIS));
jl = new JLabel("Enter a Letter");//prompt with label
tf = new JTextField(null);//length of text field by character
jp.add(jl);//add label to panel
jp.add(tf);//add text field to panel
tf.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JTextField tf = (JTextField)e.getSource();
input = tf.getText();//get user input
JLabel jl2 = new JLabel("Letters Used: " + input);//NOT DISPLAYING
jp.add(jl2);//NOT DISPLAYING
}//end actionPerformed method
});
}//end userInput method
/*****************
* WINDOW METHOD *
* ***************/
public void window() {
LoadImageApp i = new LoadImageApp();//calling image class
JFrame gameFrame = new JFrame();//declaration
gameFrame.add(i); //adds background image to window
i.add(jp); // adds panel containing label to background image panel
gameFrame.setTitle("Hangman");//title of frame window
gameFrame.setSize(850, 600);//sets size of frame
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//exit when 'x' button pressed
gameFrame.setIconImage(new ImageIcon("Hangman-Game-grey.png").getImage());//set the frame icon to an image loaded from a file
gameFrame.setLocationRelativeTo(null);//window centered
gameFrame.setResizable(false);//user can not resize window
gameFrame.setVisible(true);//display frame
}//end window method
}//end GameMechanics class
/*PACKAGE DECLARATION*/
package Game;
/***********************
* IMPORT DECLARATIONS *
* *********************/
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
/***************
* IMAGE CLASS *
* *************/
public class LoadImageApp extends JPanel {
private static final long serialVersionUID = 1L;
private ImageIcon image;
/***********************
* PAINT IMAGE METHOD *
* *********************/
public void paintComponent (Graphics g) {
super.paintComponent(g);
image = new ImageIcon("hangman.png");//image name & type
image.paintIcon(this, g, 0, 9);
}//end paintComponent method
}//end LoadImageApp class
/*PACKAGE DECLARATION*/
package Game;
/*******************
* GAME MAIN CLASS *
* *****************/
public class GameMain {
/***************
* MAIN METHOD *
* *************/
public static void main (String []args) {
GameMechanics game = new GameMechanics();//declaration
game.userInput();//userInput call
game.window();
}//end main method
}//end GameMain class
【问题讨论】:
标签: java swing jpanel jlabel jtextfield