Java 在JFrame标签中调用方法
我在计算机科学课上有一个项目,我正在尝试使用JFrame,但遇到了一些问题。Java 在JFrame标签中调用方法,java,swing,user-interface,jframe,Java,Swing,User Interface,Jframe,我在计算机科学课上有一个项目,我正在尝试使用JFrame,但遇到了一些问题。 我试图实现的第一个目标是让用户在第一个JFrame.java类中输入一个“字符名”,并能够将该字符名引入另一个JFrame.java类 java是捕获用户字符名的类。 java是我想要显示字符名的类 在WelcomeScreen.java中,我从Swing控件和 //This is the WelcomeScreen.java text field I edited so that what the user inpu
我试图实现的第一个目标是让用户在第一个JFrame.java类中输入一个“字符名”,并能够将该字符名引入另一个JFrame.java类 java是捕获用户字符名的类。 java是我想要显示字符名的类 在WelcomeScreen.java中,我从Swing控件和
//This is the WelcomeScreen.java text field I edited so that what the user inputs turns into a string.
class WelcomeScreen extends JFrame {
private void characterNameActionPerformed(java.awt.event.ActionEvent evt) {
//String charName is declared in the beginning
charName = characterName.getText().trim();
dndRacesAndClasses newUser = new dndRacesAndClasses(charName);
characterName.add(charName, this);
// TODO add your handling code here:
}
//This is the textbox the user types in their character name
private void characterNameActionPerformed(java.awt.event.ActionEvent evt) {
charName = characterName.getText().trim();
dndRacesAndClasses newUser = new dndRacesAndClasses(charName);
newUser.charName(charName);
// TODO add your handling code here:
}
//I added this get/set methods because this works in object-oriented coding to bring something over to a different class.
public String getCharName()
{
characterName.setText(charName);
return charName;
}
public void setCharName(String charName){
characterName.setText(charName);
}
}
“进入第一个JFrame.java类”为了避免混淆,您应该使用文件的实际名称。如何创建WelcomeScreen和CharSum的实例?如何显示它们?”//这是WelcomeScreen.java“与其用文字来描述,不如做
class WelcomeScreen{…}
。同样的CharSum
。我按照之前的评论进行了编辑。如果您修复了缩进并修复了任何其他问题,这将有所帮助。例如,CharSum
有一些应该在方法中的代码。有关创建好的代码示例的更多提示,请参阅。getter正在设置值,它应该只返回变量的值
//This is CharSum.java (I used a label in this class because it seemed like the only thing that would be capable of printing information that the user cannot edit on this screen)
class CharSum extends JFrame {
charSum = new javax.swing.JLabel(); //charSum is the label name
WelcomeScreen newUser = new WelcomeScreen();
//using the set method in WelcomeScreen.java to set character name
newUser.setCharName(charSum.toString());
//setting text for label to character name
charSum.setText(newUser.getCharName());
}