簡體   English   中英

如何將文本字段的文本從一個類顯示到另一個類的標簽中

[英]How to display a textfield's text from a class into another class' label

我需要有關如何從一個類中獲取文本字段的文本並將其顯示到來自另一個類的標簽中的幫助。

我只顯示了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 TextField中獲取名稱,從FishingPairFrame.java 將其顯示到此標簽中

文本字段中的名稱基於文本文件和ArrayList。

FishingPairFrame.java

playerLbl = new JLabel("");

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

更新代碼

釣魚對架

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. 在GameView.java中創建一個公共方法,該方法返回一個字符串:

     private String name; public String getName() { return name; } 
  2. 當填充文本字段時,在GameView中設置新變量“名稱”。

你可以做這個

    String name = nameTxt.getText(); 

到這個:

    name = nameTxt.getText(); )
  1. 在FishingPairFrame.java中,調用:

     GameView gameView = new GameView(); String name = gameView.getName(); 

如果您在FishingPairFrame中沒有GameView類的實例,則使該方法為static

這種技術稱為封裝(在其中,您可以使用getter和setter來訪問私有變量。仔細閱讀它,它非常有用。

您可以嘗試屬性更改偵聽器。 如果您的Java對象類實現了屬性更改偵聽器,則如果GUI上有任何值更改,它將觸發屬性更改偵聽器並自動更新值。 但是您需要編寫一種刷新方法來重置該標簽的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM