如何将课程中的文本字段文本显示到另一个班级中。标签

时间:2016-05-13 03:48:52

标签: java user-interface jlabel jtextfield

我需要有关如何从类中获取文本字段文本并将其显示为来自其他类的标签的帮助。

我只是展示了两个班级,更容易看到/读到我想要问的内容。

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 TextField 获取名称,从FishingPairFrame.java 将其显示为标签。

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

FishingPairFrame.java

playerLbl = new JLabel("");

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

更新的代码

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

2 个答案:

答案 0 :(得分:1)

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

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

  4. 你可以这样做

        String name = nameTxt.getText(); 
    

    进入这个:

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

      GameView gameView = new GameView();
      String name = gameView.getName();
      
    2. 如果您在FishingPairFrame中没有GameView类的实例,请创建方法static

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

答案 1 :(得分:0)

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