Question

1. Create a Java application that will change the appearance of a string as shown in the picture.

2. The program will include a panel, label, checkboxes, radio buttons, and list.

3. To change color, set up 4 radio buttons in a button group. The colors are Black, Red, Green, and Blue to be selected. The default color is Black.

4. To change the font size, set up a list. The sizes are 18, 26, 38, and 56 to be selected. The default font is Verdana and 18 point.

5. To change the font style, set up 2 check boxes. The font styles are None, Bold or/and Italic to be selected. The default font style is None or Plain.

6. The sample string will be printed in the location (50, 300).

Thanks! :)

Change Appearance - O X O Black Red Blue Blue Green ✓ Bold Italic SAMPLE

0 0
Add a comment Improve this question Transcribed image text
Answer #1

So here in this program as you have mentioned we have used the radio button for the color and check box for the font style and the list for font size.

Step-1

Program code:

import java.awt.*;
import java.awt.event.*;

public class Ex2 extends Frame implements ItemListener
{
   Checkbox c1,c2,c3,c4,bold,italic; //intialize variable
   List l1=new List(4); //list for font size
   Panel p=new Panel();
   Label lab=new Label("SAMPLE"); // label of string
   CheckboxGroup color=new CheckboxGroup();
   Ex2()
   {
       setLayout(null);
       setSize(500,500); //size of frame
  
l1.setBounds(310,60, 75,75); //size and location of list
l1.add("18");
l1.add("26");
l1.add("38");
l1.add("56");
       l1.addItemListener (this);
       add(l1);
       c1=new Checkbox("Black",true,color); //set checkbox for black color
       c1.setBounds(10,30,100,20);
       c2=new Checkbox("Red",false,color);//set checkbox for red color
       c2.setBounds(110,30,100,20);
       c3=new Checkbox("Blue",false,color);//set checkbox for blue color
       c3.setBounds(210,30,100,20);
       c4=new Checkbox("Green",false,color);//set checkbox for green color
       c4.setBounds(310,30,100,20);
      
       bold=new Checkbox("Bold",false); //set checkbox for bold font style
       bold.setBounds(20,90,100,20);
       italic=new Checkbox("Italic",false); //set checkbox for italic font style
       italic.setBounds(120,90,100,20);
      
       //adding all checkbox to frame
       add(bold);
       add(italic);
       add(c1);
       add(c2);
       add(c3);
       add(c4);
      
       //set layout and add label for panel
       p.setLayout(null);
       p.add(lab);
       lab.setBounds(0,0,300,50); //set location for label
       p.setBounds(10,140,400,400);
  
       add(p);
       c1.addItemListener(this);
       c2.addItemListener(this);
       c3.addItemListener(this);
       c4.addItemListener(this);
       setVisible(true);
   }

   //set color,style and size for font
   public void itemStateChanged(ItemEvent e)
   {
       Graphics g=getGraphics();
       int var1=0;
       if(c1.getState()==true)
       {
           lab.setForeground(Color.black);
       }
       if(c2.getState()==true)
       {
           lab.setForeground(Color.red);
       }
       if(c3.getState()==true)
       {
           lab.setForeground(Color.blue);
       }
       if(c4.getState()==true)
       {
           lab.setForeground(Color.green);
       }

       if(l1.getSelectedItem()=="18")
       {
           var1=18;
       }
       if(l1.getSelectedItem()=="26")
       {
           var1=26;
       }
       if(l1.getSelectedItem()=="38")
       {
           var1=38;
       }
       if(l1.getSelectedItem()=="56")
       {
           var1=56;
       }
       if(bold.getState()==true)
       {
           lab.setFont(new Font("Verdana", Font.BOLD, var1));  
       }
       if(italic.getState()==true)
       {
           lab.setFont(new Font("Verdana", Font.ITALIC, var1));  
       }
       if(bold.getState()==false && italic.getState()==false)
       {
           lab.setFont(new Font("Verdana", Font.PLAIN, var1));  
       }
   }
   public static void main(String[] args) {
       new Ex2();
   }
}

Step-2

Output:

— o x Black Red Blue C Green Bold Italic SAMPLE

Step-3

Conclusion

So, here in the program i have mention the comments for each and every step


Add a comment
Know the answer?
Add Answer to:
1. Create a Java application that will change the appearance of a string as shown in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT