Set background color of the text area in the frame in Java

In this program, we will learn about the menu bar and we will create the menu bar on the frame. JMenuBar is the class that is mandatory for the menu bar. Swing package is also important for the menu bar. If we want to create menu items on the menu bar its possible with the help of the JMenuItem class. One or more menu items we create on one menu. In this program, we also set background color and we also write the file on the text area with the help of the file chooser. we use the Set Background method to paste the color. We use three colors red, green, yellow.

Java Program to Set background color of the text area in the frame

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

class MenuTest implements ActionListener
{
  JFrame fr;
  JMenuBar bar;
  JMenu m,n;
  JMenuItem m1,m2,m3,n1,n2,n3;
  JTextArea ta;
  JFileChooser jf;
MenuTest()

{
  fr=new JFrame();
  fr.setSize(400,400);
  fr.setLayout(null);
  
  ta=new JTextArea();
  ta.setBounds(0,0,fr.getWidth(),fr.getHeight());
  fr.add(ta);
  
  bar=new JMenuBar();
  fr.setJMenuBar(bar);
  
  m=new JMenu("Color");
  m.setMnemonic(KeyEvent.VK_R);
    n= new JMenu("File");
  bar.add(m);
  bar.add(n);
  
  m1=new JMenuItem("Red");
  m.add(m1);
     m2=new JMenuItem("Green");
   m.add(m2);
    m3=new JMenuItem("Yellow");
  m.add(m3);
  
  n1=new JMenuItem("open");
  n2=new JMenuItem("save");
  n3=new JMenuItem("print");
  n.add(n1);
  n.add(n2);
  n.add(n3);
  fr.setVisible(true);
  m1.addActionListener(this);

  m2.addActionListener(this);
  m3.addActionListener(this);
  n1.addActionListener(this);
  n2.addActionListener(this);
  n3.addActionListener(this);
  
}
public void actionPerformed(ActionEvent e)
{
  if(e.getSource()==m1)
    ta.setBackground(Color.red);
  if(e.getSource()==m2)
    ta.setBackground(Color.green);
  if(e.getSource()==m3)
    ta.setBackground(Color.yellow);
  if(e.getSource()==n1)
  {
    try{
      jf=new JFileChooser();
      jf.showOpenDialog(fr);
      File filename=jf.getSelectedFile();
      FileInputStream fin=new FileInputStream(filename);
      int size=fin.available();
      byte b[]=new byte[size];
      fin.read(b);
      String str=new String(b);
      ta.setText(str);
    }
    catch(Exception ee)
    {
    }
  }
  
    
  if(e.getSource()==n3)
  {
    try
    {
      ta.print();
    }
    catch(Exception ee)
    {}
  }
}
  public static void main(String s[])
  {
    new MenuTest();
  }
}

In the program, first, we create a frame and set the size. After that, we create a menu on the top of the frame by using JMenuBar class and set the bounds. After that, we create menu items in the menu. Finally, we set an action listener on the menu items.

The output of the program

Set background color of the text area in the frame in java

Output: Set background color of the text area in the frame in java

ALso learn,

Leave a Reply

Your email address will not be published. Required fields are marked *