When you press the button and execute the button event, you can create the new JFrame by indicating the text to JLabel .
For this, each button must have its own action event which we add by means of a "action listener" , this is called ActionListener , the class ** JButton * has the addActionListener(...);
method.
When you create each button, you must assign its actions at once, for this, suppose you have the 5 buttons and that each one is called according to its vowel, something like this:
private JButton vocalA;
private JButton vocalE;
private JButton vocalI;
private JButton vocalO;
private JButton vocalU;
At the moment of instantiating them, once you add their actions:
vocalA = new JButton("A");
vocalA.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Se ejecuta una acción
}
});
However, Java offers to use Lambda expressions and they are quite simple:
vocalA = new JButton("A");
vocalA.addActionListener((ActionEvent e) -> {
// Se ejecuta una acción
});
Once each button has its own action (you must do that for each button), it's time to call the new JFrame with the JLabel and assign it the information, for this, we could create a new method in this same class and since all the buttons are going to perform the same action (only that each one with different text) you could do the following:
private void asignarVocal(String vocal){
// Se realiza la acción
}
In this way, you could save a lot of code and simply, within each event you call this method and as a parameter, you pass the text that has this button by means of JButton.getText()
. We are leaving something like this:
vocalA.addActionListener((ActionEvent e) -> {
asignarVocal(vocalA.getText());
});
Thus, you would be passing the name of the button (which contains the letter of the vowel) to the method. Now we only have to assign this to JLabel .
For this, within the method that we created ( assignVocal ), we will do something like this:
private void asignarVocal(String vocal){
tuFrame.getTuLabel().setText(vocal);
tuFrame.setVisible(true);
}
(You must change TuLabel by the name of the JLabel that is in the other JFrame that you create, and yourFrame by the name of your second JFrame ). This way you will have passed the text of the button (which is the letter of the vowel) to JLabel .
In summary, we should have something like this:
When you assign the buttons:
private void iniciarComponentes(){ // < esto es a modo de ejemplo, no sé como lo hagas en tu clase
// Declaras tus variables....
vocalA.addActionListener((ActionEvent e) -> {
asignarVocal(vocalA.getText());
});
vocalE.addActionListener((ActionEvent e) -> {
asignarVocal(vocalE.getText());
});
vocalI.addActionListener((ActionEvent e) -> {
asignarVocal(vocalI.getText());
});
vocalO.addActionListener((ActionEvent e) -> {
asignarVocal(vocalO.getText());
});
vocalU.addActionListener((ActionEvent e) -> {
asignarVocal(vocalU.getText());
});
// Inicias otros componentes...
}
private void asignarVocal(String vocal){
tuFrame.getTuLabel().setText(vocal);
tuFrame.setVisible(true);
}
And with this it would be enough, there you tell us how it went!