java gui application

Hello guys, today I am going to show how a simple program using Java Swing. In the program we have some text and 2 radio buttons. We will use them to make the text smaller or larger based on user click.The font of the program is going to change also.

So, have do we do that?

We extend the JFrame class to get its attributes and to create the graphical user interface. We also implements the ActionListener interface to interact with the user.

Oracle official JFrame tutorial  http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html

Variables needed :

private JLabel label;
private JRadioButton button;
private JRadioButton button2;
private JPanel p1;
private ButtonGroup group;

The constructor :

Here we initialize our variables and add them to the frame.

setSize() ->JFrame method to set the window size.

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) -> to tell the user to close the program when he presses the X button in the window frame.

setVisible() -> we set this to true to actually show the window.

You can also give the program a title by using setTitle() method.

public change_font_and_size()
  {
    label=new JLabel("some text");
    add(label,BorderLayout.EAST);
   
    p1=new JPanel();
    button=new JRadioButton("small");
    button.addActionListener(this);
    button2=new JRadioButton("large");
    button2.addActionListener(this);
    button.setSelected(true);
    group=new ButtonGroup();
    group.add(button);
    group.add(button2);
    p1.add(button);
    p1.add(button2);
    add(p1);
    setSize(300,400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    
    //close constructor
  }

 

The logic :

Here we tell the user what to expect when he clicks the radio buttons.

actionPerformed() is the method we implement form our interface implementation which i mentioned earlier (ActionListener). It takes the user click as a parameter.And we decide what happens when the user clicks button or button2. Basically it will change the size to a smaller or a larger one. The font also changes. After clicking on radio buttons we repaint the label so that the program changes take effect.

public void actionPerformed(ActionEvent e) {
    int size=0;
    if(e.getSource() == button)
    {
      size=14;
    }
    else if(e.getSource() == button2)
    {
      size=30;
    }
    Font f=new Font("Courier", Font.ITALIC,size);
    label.setFont(f);
    label.repaint();
    
  }

 

The full program :

package bittlife;

import java.awt.BorderLayout;
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.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class change_font_and_size extends JFrame implements ActionListener {
  private JLabel label;
  private JRadioButton button;
  private JRadioButton button2;
  private JPanel p1;
  private ButtonGroup group;
  public change_font_and_size()
  {
    label=new JLabel("some text");
    add(label,BorderLayout.EAST);
    
    
  
    p1=new JPanel();
    button=new JRadioButton("small");
    button.addActionListener(this);
    button2=new JRadioButton("large");
    button2.addActionListener(this);
    button.setSelected(true);
    group=new ButtonGroup();
    group.add(button);
    group.add(button2);
    p1.add(button);
    p1.add(button2);
    add(p1);
    setSize(300,400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    
    //close constructor
  }
  
  public static void main(String[] args) {
    new change_font_and_size();
  }
  public void actionPerformed(ActionEvent e) {
    int size=0;
    if(e.getSource() == button)
    {
      size=14;
    }
    else if(e.getSource() == button2)
    {
      size=30;
    }
    Font f=new Font("Courier", Font.ITALIC,size);
    label.setFont(f);
    label.repaint();
    
  }
}

Don’t forget, we have to call the constructor in the main method!

Hope you enjoyed this and stay tuned for more.

9 COMMENTS

  1. I’m really enjoying the design and layout of your blog. It’s a very easy on the eyes which makes it much more enjoyable for me to come here and visit more often. Did you hire out a designer to create your theme? Excellent work!

  2. I¦ve been exploring for a bit for any high-quality articles or blog posts in this kind of area . Exploring in Yahoo I finally stumbled upon this site. Reading this information So i am satisfied to convey that I’ve an incredibly just right uncanny feeling I found out exactly what I needed. I such a lot definitely will make sure to do not omit this web site and give it a glance regularly.

LEAVE A REPLY

Please enter your comment!
Please enter your name here