我在创建几个JTextFields和JLabel时,很难在我的JPanel中创建一个hang子手游戏。我试图在JLabel中将用户输入显示为“使用的字母”。我评论过下面不显示的区域。提前致谢。难以在JPanel内部显示几个JTextFields和JLabels
/*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
你能帮我解决一下我的代码吗?一段时间以来我一直都很困难。 – Anon