2016-05-13 58 views
0

我需要帮助,以了解如何从类中获取文本字段的文本并将其显示到来自另一个类的标签中。如何将文本字段的文本从类中显示到另一个类的标签中

我只是展示了2个类,它更容易看/看我想问什么。

GameView.java

public void logIn() 
    { 
     loginFrame = new JFrame("FIshing Pair Game"); 
     loginFrame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     loginFrame.setResizable(true); 

     gridBag = new GridBagLayout(); 

     loginPanel = new JPanel(); 
     loginPanel.setBackground(Color.LIGHT_GRAY); 
     loginPanel.setPreferredSize(new Dimension(400,400)); 
     loginPanel.setLayout(gridBag); 

     loginTitleLbl = new JLabel("Login to Play!"); 
     loginTitleLbl.setFont(new Font("Britannic Bold",Font.PLAIN,22)); 

     nameLbl = new JLabel("Login name:"); 
     nameLbl.setFont(new Font("Agency FB",Font.BOLD,20)); 

     passLbl = new JLabel("Password:"); 
     passLbl.setFont(new Font("Agency FB",Font.BOLD,20)); 

     nameTxt = new JTextField(); 
     nameTxt.setPreferredSize(new Dimension(200,30)); 

     passTxt = new JPasswordField(); 
     passTxt.setPreferredSize(new Dimension(200,30)); 
     passTxt.setEchoChar('*'); 

     private class LoginBtnListener implements ActionListener 
{ 
    public void actionPerformed(ActionEvent ae) 
    { 
     ArrayList<Player>players = new ArrayList<>(); 
     Scanner playerScan = null; 

     try{ 
      playerScan = new Scanner(new File("players.dat")); 
      while(playerScan.hasNextLine()) 
      { 
       String playerData = playerScan.nextLine(); 
       String[] dataSplit = playerData.split("\\|"); 
       Player existingPlayer = new Player(dataSplit[0],dataSplit[1],dataSplit[2],Integer.parseInt(dataSplit[3])); 
       players.add(existingPlayer); 
      } 
     }catch(FileNotFoundException ex){ 
      System.out.println("Error! File - players.dat not found!"); 
     } 
     playerScan.close(); 

     boolean exist = false; 

     String name = nameTxt.getText(); 
     String passWord = passTxt.getText(); 

     for(int i = 0; i < players.size(); i++) 
     { 
      if(players.get(i).getLoginName().equalsIgnoreCase(name)) 
      { 
       exist = true; 
      } 
     } 

     if(exist == true) 
     { 
      nameTxt.setText(""); 
      passTxt.setText(""); 
      JOptionPane.showMessageDialog(null,"Welcome back " + name + "!"); 
      javax.swing.UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("Verdana", Font.ITALIC, 20))); 
      ShuffleCardsFrame fishingFrame = new ShuffleCardsFrame(); 
      fishingFrame.run(); 
      loginFrame.setVisible(false); 
     } 

     if(exist == false) 
     { 
      JOptionPane.showMessageDialog(null,"Error! Player " + name + " doesn't exist! Please try again."); 
      javax.swing.UIManager.put("JOptionPane.messageFont", new FontUIResource(new Font("Baskerville Old Face", Font.BOLD, 20))); 
     } 
    } 

}

我需要从nametxt文本字段和显示成从FishingPairFrame.java这个标签得到名。

文本字段中的名称基于文本文件和ArrayList。

FishingPairFrame.java

playerLbl = new JLabel(""); 

playerLbl.setFont(new Font("Agency FB",Font.BOLD,20)); 

修订CODES

FishingPairFrame

playerName = gameView.getPlayerName(); 
playerLbl = new JLabel(playerName+" Matching Pair: 0"); 
playerLbl.setFont(new Font("Agency FB",Font.BOLD,20)); 

GameView.java

public String getPlayerName() 
{ 
    return playerName; 
} 

public void setPlayerName(String playerName) 
{ 
    this.playerName = playerName; 
} 
//in private loginbtnlistener class 
playerName = nameTxt.getText(); 

回答

1
  1. 创建GameView.java一个公共方法返回一个字符串:

    private String name; 
    
    public String getName() { 
        return name; 
    } 
    
  2. 在GameView设置新的变量“名称”文本字段被填充时。

你可以只让这个

String name = nameTxt.getText(); 

到这一点:

name = nameTxt.getText();) 
在FishingPairFrame.java
  • ,请致电:

    GameView gameView = new GameView(); 
    String name = gameView.getName(); 
    
  • 或者如果您在FishingPairFrame中没有GameView类的实例,请使用方法static

    这种技术被称为封装(您可以使用getter和setter访问私有变量。阅读它,这是非常有用的。

    +0

    我试过了,它显示null而不是玩家的名字。我会粘贴我更新的代码 – hello

    +0

    它显示像“null匹配对:0”在GUI屏幕 – hello

    +0

    @hello我认为RandyFreak指向2是指在LoginBtnListener.actionPerformed方法中进行更改,而不是在GameView.login中进行更改。 – RubioRic

    0

    您可以尝试属性更改侦听器。如果您的Java对象类实现了属性更改侦听器,如果GUI上的任何值发生更改,它将触发属性更改侦听器并自动更新值。但是您需要编写刷新方法来重置该标签的值。

    相关问题