我在創建幾個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