java - 如何在多个方法中使用一个变量?

标签 java methods static jtextfield static-variables

我正在尝试在另一种方法中使用变量字母。我已将该变量声明为方法外部的静态变量,但在打印时它仍然不显示。相反,它显示为空。整个代码如下。感谢您的提前帮助。

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();
   }
}

最佳答案

您正在打印 letter 的值,然后才有机会影响它(在执行某种操作之前它不会被分配值),因此它显示为

关于java - 如何在多个方法中使用一个变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20749062/

相关文章:

java - JPA获取select中的上一条和下一条记录

java - Flex 中此声明的 Java 等效项是什么

java - Java 是 "pass-by-reference"还是 "pass-by-value"?

c# - 为什么静态类不能有非静态方法和变量?

caching - 使用ehcache优于静态HashMap的优势

spring - 如果该类不包含状态,使用非静态类是否合理?

java - CheckBox 不响应选中状态 (Android Studio)

java - 我可以将 GAE 的 dev_appserver 设置为在我更改 .class 文件时自动重新加载上下文吗?

Ruby,运行所有方法

go - 在结构定义之外使用方法的原因是什么?