Вызов метода в JFrame Label - PullRequest
       69

Вызов метода в JFrame Label

0 голосов
/ 07 ноября 2019

У меня есть проект в классе Computer Science, и я пытаюсь использовать JFrame, но у меня возникают проблемы. Первая цель, которую я пытаюсь выполнить, состоит в том, чтобы пользователь ввел «имя персонажа» в первый класс JFrame.java и смог перенести имя этого персонажа в другой класс JFrame.java.

WelcomeScreen.java - это класс, который фиксирует имя персонажа пользователя. CharSum.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);
    }
}
//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());
}
...