gpt4 book ai didi

java - 如何将一个类中的文本字段文本显示到另一个类的标签中

转载 作者:行者123 更新时间:2023-12-01 09:56:08 24 4
gpt4 key购买 nike

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

我只展示了 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));

更新代码

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. 在 GameView.java 中创建一个返回字符串的公共(public)方法:

    private String name;

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

你可以做这个

    String name = nameTxt.getText(); 

进入此:

    name = nameTxt.getText(); )
  • 在 FishingPairFrame.java 中,调用:

    GameView gameView = new GameView();
    String name = gameView.getName();
  • 或者如果您在 FishingPairFrame 中没有 GameView 类的实例,则将该方法设为静态

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

    关于java - 如何将一个类中的文本字段文本显示到另一个类的标签中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37200907/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com