So I learned from YouTube videos and online tutorials and they gave me a task to do a simple program. I need to pick 1 button from 4 buttons (radio buttons) and each time I pick a button he's changing a text in a text box.
The program is working great ! I'm just asking you guys to check if I did something that is considered wrong in programming or if there's a better way to do this task.
So that's my main program.
import javax.swing.JFrame;
public class Main {
public static void main(String args[])
{
gui go = new gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(317,220);
go.setVisible(true);
}
}
and that's the class i'm using (gui class)
package learning;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class gui extends JFrame implements ActionListener {
private JTextField tf = new JTextField( 25);
private JRadioButton regularRadio = new JRadioButton("Regular",true);
private JRadioButton boldRadio = new JRadioButton("Bold",false);
private JRadioButton italicRadio = new JRadioButton("Italic",false);
private JRadioButton boldNitalicRadio = new JRadioButton("Bold And Italic",false);;
private ButtonGroup group = new ButtonGroup();
public gui(){
super("Changing text program");
setLayout (new FlowLayout());
add(tf);
add (regularRadio);
add (boldRadio );
add (italicRadio);
add (boldNitalicRadio);
group.add(boldRadio);
group.add(boldNitalicRadio);
group.add(italicRadio);
group.add(regularRadio);
regularRadio.addActionListener(this);
boldRadio.addActionListener(this);
italicRadio.addActionListener(this);
boldNitalicRadio.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if (e.getActionCommand() == "Regular"){
tf.setText("You picked regular radio button");
tf.setFont(new Font ("David" , Font.PLAIN , 14));
}
else if (e.getActionCommand() == "Bold") {
tf.setText("You picked bold radio button");
tf.setFont(new Font ("David" , Font.BOLD , 14));
}
else if (e.getActionCommand() == "Italic") {
tf.setText("You picked italic radio button");
tf.setFont(new Font ("David" , Font.ITALIC , 14));
}
else{
tf.setText("You picked Bold and Italic radio button");
tf.setFont(new Font ("David" , Font.ITALIC + Font.BOLD , 14));
}
}
}