如何将 class 中的文本字段文本显示到另一个 class 标签中
How to display a textfield's text from a class into another class' label
我需要帮助,了解如何从 class 获取文本字段的文本并将其显示到来自另一个 class 的标签中。
我只展示了 2 classes,这样更容易 see/read 我想问的问题。
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 中获取 name 并将其显示到 这个来自 [=54 的 label =]。
文本字段中的名称基于文本文件和 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();
您可以尝试属性 更改侦听器。如果您的 Java 对象 class 实现了 属性 更改侦听器,如果 GUI 上的任何值更改,它将触发 属性 更改侦听器并自动更新值。但是您需要编写一个刷新方法来重置该标签的值。
在GameView.java中创建一个public方法,returns一个字符串:
private String name;
public String getName() {
return name;
}
在 GameView 中,当文本字段被填充时设置新变量 "name"。
你可以做这个
String name = nameTxt.getText();
进入这个:
name = nameTxt.getText(); )
在FishingPairFrame.java,调用:
GameView gameView = new GameView();
String name = gameView.getName();
如果您在 FishingPairFrame 中没有 GameView class 的实例,或者创建方法 static
。
这种技术称为封装(您可以使用 getter 和 setter 访问私有变量。仔细阅读它,它非常有用。
我需要帮助,了解如何从 class 获取文本字段的文本并将其显示到来自另一个 class 的标签中。
我只展示了 2 classes,这样更容易 see/read 我想问的问题。
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 中获取 name 并将其显示到 这个来自 [=54 的 label =]。
文本字段中的名称基于文本文件和 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();
您可以尝试属性 更改侦听器。如果您的 Java 对象 class 实现了 属性 更改侦听器,如果 GUI 上的任何值更改,它将触发 属性 更改侦听器并自动更新值。但是您需要编写一个刷新方法来重置该标签的值。
在GameView.java中创建一个public方法,returns一个字符串:
private String name; public String getName() { return name; }
在 GameView 中,当文本字段被填充时设置新变量 "name"。
你可以做这个
String name = nameTxt.getText();
进入这个:
name = nameTxt.getText(); )
在FishingPairFrame.java,调用:
GameView gameView = new GameView(); String name = gameView.getName();
如果您在 FishingPairFrame 中没有 GameView class 的实例,或者创建方法 static
。
这种技术称为封装(您可以使用 getter 和 setter 访问私有变量。仔细阅读它,它非常有用。