0
I created a code in java with 2 Jradiobuttons and need it to appear on the console which radio is selected, and when change radio, appear on the console as well. I’ve tried using the if(rad1.isSelected()){"Argumentos"}
and the same thing on 2°radio.
When the code starts 1°radio already starts selected, however when it changes to 2°radio the console does not change. As I do?
Code:
/*Biblioteca*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.text.*;
class Layout {
public static void main(String args[]) {
EventQueue.invokeLater(() -> {
/*cria o layout e a janela */
JFrame frame = new JFrame("Prototipo10");
JPanel panel = new JPanel(new GridBagLayout());
/*Cria os Radio Butons*/
GridBagConstraints gbc9 = new GridBagConstraints();
gbc9.anchor = GridBagConstraints.WEST;
gbc9.gridx = 1;
gbc9.gridy = 7;
JRadioButton radMasc = new JRadioButton("Radio1");
radMasc.setForeground(Color.BLUE);
GridBagConstraints gbc10 = new GridBagConstraints();
gbc10.anchor = GridBagConstraints.WEST;
gbc10.gridx = 1;
gbc10.gridy = 8;
JRadioButton radFem = new JRadioButton("Radio2");
radFem.setForeground(Color.BLUE);
ButtonGroup grubut = new ButtonGroup();
grubut.add(radMasc);
grubut.add(radFem);
panel.add(radMasc, gbc9);
panel.add(radFem, gbc10);
/*Configurações da janela*/
frame.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setSize(250, 250);
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
You did not add Listener?
– user28595
I don’t know how to use the Istener
– Shintaro
Got it + - how it works read some codes on the net but I was in doubt: Actionlistener has to be implemented in Buttongroup or direct on the radio? if yes and more what other it works?
– Shintaro
No, you need to add to all the radiobuttons.
– user28595
already understood how implements it, it works on all window menu type objects, Jtextfield, jButton among others?
– Shintaro