다른 방법으로 변수 문자를 사용하려고합니다. 변수 외부에 정적 변수로 변수를 선언했지만 인쇄 할 때 여전히 표시되지 않습니다. 대신 null을 표시합니다. 전체 코드는 다음과 같습니다. 고급 도움말에 감사드립니다. 당신은 아무것도 이전 letter
의 값을 인쇄하는여러 방법으로 변수를 사용하려면 어떻게해야합니까?
package Game;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
class GameStructure {
private String []wordList = {"javatar","java","activity","alaska","appearance","article",
"automobile","basket","birthday","canada","central","character","chicken","chosen",
"cutting","daily","darkness","diagram","disappear","driving","effort","establish","exact",
"establishment","fifteen","football","foreign","frequently","frighten","function","gradually",
"hurried","identity","importance","impossible","invented","italian","journey","lincoln",
"london","massage","minerals","outer","paint","particles","personal","physical","progress",
"quarter","recognise","replace","rhythm","situation","slightly","steady","stepped",
"strike","successful","sudden","terrible","traffic","unusual","volume","yesterday"};
private JTextField tf;
private JLabel jl2;
static String letter;
public void window() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_A);
menuBar.add(menu);
JMenuItem menuItem = new JMenuItem("Developer", KeyEvent.VK_T);
menu.add(menuItem);
JMenuItem menuItem2 = new JMenuItem("Instructions", KeyEvent.VK_T);
menu.add(menuItem2);
JMenuItem menuItem3 = new JMenuItem("Restart", KeyEvent.VK_T);
menu.add(menuItem3);
JMenuItem menuItem4 = new JMenuItem("Exit", KeyEvent.VK_T);
menu.add(menuItem4);
ImageIcon ic = new ImageIcon("hangman2.png");
JFrame gameFrame = new JFrame();
JPanel bottomRight = new JPanel();
JPanel bottomLeft = new JPanel();
JPanel top = new JPanel();
JPanel bottom = new JPanel();
JPanel imgPane = new JPanel();
JPanel panel1 = new JPanel();
bottom.setLayout(new BoxLayout(bottom, BoxLayout.X_AXIS));
imgPane.setLayout(new BorderLayout());
panel1.setLayout(new BorderLayout());
panel1.setOpaque(false);//!!
top.setBorder(BorderFactory.createTitledBorder(""));
bottom.setBorder(BorderFactory.createTitledBorder(""));
tf = new JTextField(1);
JLabel img = new JLabel(ic, JLabel.CENTER);
JLabel jl = new JLabel("Enter a letter", JLabel.CENTER);
jl2 = new JLabel("Letters used: ", JLabel.CENTER);
JLabel jl3 = new JLabel("__ ", JLabel.CENTER);
jl.setFont(new Font("Rockwell", Font.PLAIN, 20));
tf.setFont(new Font("Rockwell", Font.PLAIN, 20));
jl2.setFont(new Font("Rockwell", Font.PLAIN, 20));
imgPane.add(img);//center
top.add(jl);//top center
top.add(tf);//top center
bottomLeft.add(jl2);//bottom left position
bottomRight.add(jl3);//bottom right position
bottom.add(bottomLeft);//bottom
bottom.add(bottomRight);//bottom
panel1.add(imgPane, BorderLayout.CENTER);//background image (center)
panel1.add(top, BorderLayout.NORTH);//text field and jlabel (top)
panel1.add(bottom, BorderLayout.SOUTH);// blank spaces and letters used (bottom)
gameFrame.setJMenuBar(menuBar);
gameFrame.setTitle("Hangman");
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.setIconImage(
new ImageIcon("hangmanIcon.png").getImage());
gameFrame.setResizable(false);
gameFrame.add(panel1);
gameFrame.setSize(800, 500);
gameFrame.setLocationRelativeTo(null);
gameFrame.setVisible(true);
int j = 0;
int []length = new int[64];
for(j = 0; j<64; j++) {
length[j] = wordList[j].length();//gets length of words in wordList
}//end for
int l = 0;
String line = "";
//create line first then put into .setText
for(int m = 0; m<length[l]; m++) {
line += "__ ";
l++;
}//end for
jl3.setText(line);
tf.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {//when enter key pressed
JTextField tf = (JTextField)e.getSource();
letter = tf.getText();
jl2.setText(jl2.getText() + letter + " ");//sets jlabel text to users entered letter
}//end actionPerformed method
});
}//end window method
public void print(){
System.out.println(letter);
}
}
public class GameMain {
public static void main(String[] args) {
GameStructure game = new GameStructure();
game.window();
game.print();
}
}
더 많은 코드가 필요합니다. 또한, 어떻게됩니까? _ 작동하지 _ 않습니다 우리가 함께 일할 수있는 일이 아닙니다. – Keppil
"작동하지 않는다"는 것은 무엇을 의미합니까? 당신은 가치를 부여하고, 또 다른 가치가 있습니까? 중단 점이있는 디버거를 사용 했습니까? –