Question

Exercise 5: StyleOption program a) Modify the StyleOption program, in your lecture slides, to allow the user to specify the

answer this question please, use java language

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

//Answer for a.

//---------- StyleOption.java ----------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class StyleOption extends JFrame implements ActionListener
{
   //checkboxes for bold and italic
   JCheckBox bold;
   JCheckBox italic;
   //label to display text with given style
   JLabel text;
   //combo box of font names and font sizes
   JComboBox fontNames;
   JComboBox fontSizes;
   String[] names = {"Times New Roman","SansSerif","Rockwell","Monospaced","Calibri"};
   String[] sizes = {"10","12","14","16","18","20","24","26","28","30"};
   StyleOption()
   {
       setLayout(null);
       setSize(500,300);
       setTitle("Style Options");
       //set background color
       getContentPane().setBackground(new Color(47,222,216));
      
       //get width and height
       int width = getWidth();
       int height = getHeight();
      
       //initilize all components
       text = new JLabel("Say it with style!");
      
       bold = new JCheckBox("Bold");
       italic = new JCheckBox("Italic");
      
       fontNames = new JComboBox(names);
       fontSizes = new JComboBox(sizes);
      
       JPanel panel = new JPanel(null);

       int halfOfHeight = height/2;
      
       //set locations for each component
       text.setBounds(width/2-100,10,width-30,halfOfHeight-50);
      
       bold.setBounds(10,halfOfHeight,50,30);
       italic.setBounds(70,halfOfHeight,60,30);
       fontNames.setBounds(130,halfOfHeight,130,30);
       fontSizes.setBounds(280,halfOfHeight,50,30);
      
       //for bold and italic set background colors to look alike in the question given.
       bold.setBackground(new Color(47,222,216));
       italic.setBackground(new Color(47,222,216));
      
       //add components
       add(bold);
       add(italic);
       add(fontNames);
       add(fontSizes);
      
       add(text);
      
       //add action listeners
       fontNames.addActionListener(this);
       fontSizes.addActionListener(this);
       bold.addActionListener(this);
       italic.addActionListener(this);
   }
   //when event triggered
   public void actionPerformed(ActionEvent ae)
   {
       //if triggered event is font names are font sizes
       if(ae.getSource() == fontNames || ae.getSource() == fontSizes)
       {
           //update the label style
           updateLabel();
       }
       //if bold
       else if(ae.getSource() == bold)
       {
           //then deselect italic checkbox
           italic.setSelected(false);
           //update the label style
           updateLabel();
       }
       //if italic
       else if(ae.getSource() == italic)
       {
           //then deselect bold
           //update the label style
           bold.setSelected(false);
           updateLabel();
       }
   }
   //function to update the label
   public void updateLabel()
   {
       //get font style and size
       String font = fontNames.getSelectedItem().toString();
       int size = Integer.parseInt(fontSizes.getSelectedItem().toString());
       Font f;
       //if bold is selected create font with Font.BOLD
       if(bold.isSelected())
       {
           f = new Font(font,Font.BOLD, size);
       }
       //if ITALIC is selected create font with Font.ITALIC
       else if(italic.isSelected())
       {
           f = new Font(font,Font.ITALIC, size);
       }
       //if not create font with Font.PLAIN
       else
       {
           f = new Font(font,Font.PLAIN, size);
       }
       //ASSIGN STYLE TO label text.
       text.setFont(f);
   }
   public static void main(String[] args)
   {
       //create object for the class
       StyleOption gui = new StyleOption();
       //set closing operation
       gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       //set visible true.
       gui.setVisible(true);
      
   }
}

//OUTPUT

Style Options х Say it with style! Bold Italic Times New Rom... 10-Style Options х Say it with style! Bold Italic Rockwell - 30 -Style Options х Say it with style! ✓ Bold Italic Rockwell - 301

//------------ ANSWER FOR B ----------

//---------- StyleOptionB.java ----------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
class StyleOptionB extends JFrame implements ActionListener,ChangeListener
{
   //checkboxes for bold and italic
   JCheckBox bold;
   JCheckBox italic;
   //label to display text with given style
   JLabel text;
   //combo box of font names
   JComboBox fontNames;
   JLabel sizeLabel;
   JSlider fontSize;
   String[] names = {"Times New Roman","SansSerif","Rockwell","Monospaced","Calibri"};
  
   StyleOptionB()
   {
       setLayout(null);
       setSize(1200,200);
       setTitle("Style Options");
       //set background color
       getContentPane().setBackground(new Color(47,222,216));
      
       //get width and height
       int width = getWidth();
       int height = getHeight();
      
       //initilize all components
       text = new JLabel("Say it with style!");
      
       bold = new JCheckBox("Bold");
       italic = new JCheckBox("Italic");
      
       JLabel fontLabel = new JLabel("Font Name: ");
       fontNames = new JComboBox(names);
      
       sizeLabel = new JLabel("Font size: ");
       //create a slider with font size 10 to 40 and put initial value to 10.
       fontSize = new JSlider(10,40,10);
      
       JPanel panel = new JPanel(null);

       int halfOfHeight = height/2;
       int halfWidth = width/2;
       //set locations for each component
       text.setBounds(50,10,halfWidth,100);
      
       bold.setBounds(500,40,50,30);
       italic.setBounds(560,40,60,30);
      
       fontLabel.setBounds(630,40,70,30);
      
       fontNames.setBounds(710,40,130,30);
       sizeLabel.setBounds(850,40,80,30);
       fontSize.setBounds(940,40,150,50);
      
       //set PaintTicks to true
       fontSize.setPaintTicks(true);
       fontSize.setPaintLabels(true);
       //set tick spacing for major and minor
       fontSize.setMajorTickSpacing(5);
       fontSize.setMinorTickSpacing(1);
      
       //for bold and italic set background colors to look alike in the question given.
       bold.setBackground(new Color(47,222,216));
       italic.setBackground(new Color(47,222,216));
      
       //add components
       add(bold);
       add(italic);
       add(fontLabel);
       add(fontNames);
       add(sizeLabel);
       add(fontSize);
      
       add(text);
      
       //add action listeners
       fontNames.addActionListener(this);
      
       bold.addActionListener(this);
       italic.addActionListener(this);
       //add change event
       fontSize.addChangeListener(this);
   }
   //when event triggered
   public void actionPerformed(ActionEvent ae)
   {
       //if triggered event is font names
       if(ae.getSource() == fontNames)
       {
           //update the label style
           updateLabel();
       }
       //if bold
       else if(ae.getSource() == bold)
       {
           //then deselect italic checkbox
           italic.setSelected(false);
           //update the label style
           updateLabel();
       }
       //if italic
       else if(ae.getSource() == italic)
       {
           //then deselect bold
           //update the label style
           bold.setSelected(false);
           updateLabel();
       }
   }
   //when change event triggered
   public void stateChanged(ChangeEvent ce)
   {
       //if triggered by fontSize slider
       if(ce.getSource() == fontSize)
       {
           sizeLabel.setText("Font size: "+(int)fontSize.getValue());
           //update label
           updateLabel();
       }
   }
   //function to update the label
   public void updateLabel()
   {
       //get font style and size
       String font = fontNames.getSelectedItem().toString();
       //get font size from slider and explictly convert it to integer.
       int size = (int)fontSize.getValue();
       Font f;
       //if bold is selected create font with Font.BOLD
       if(bold.isSelected())
       {
           f = new Font(font,Font.BOLD, size);
       }
       //if ITALIC is selected create font with Font.ITALIC
       else if(italic.isSelected())
       {
           f = new Font(font,Font.ITALIC, size);
       }
       //if not create font with Font.PLAIN
       else
       {
           f = new Font(font,Font.PLAIN, size);
       }
       //ASSIGN STYLE TO label text.
       text.setFont(f);
   }
   public static void main(String[] args)
   {
       //create object for the class
       StyleOptionB gui = new StyleOptionB();
       //set closing operation
       gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       //set visible true.
       gui.setVisible(true);
      
   }
}
//------------- OUTPUT ---------------

Style Options - O х Say it with style! Bold Italic Font Name: Times New Rom... → Font size: 40 IIIIIIIIIIIIIIIIIIIIIIIIIIIIIStyle Options х Say it with style! Bold Italic Font Name: Times New Rom... → Font size: 40 LILILILI LILILILLILILI LILILILII 1Style Options х Say it with style! ✓ Bold Italic Font Name: Times New Rom... → Font size: 40 LILILILI LILILILLILILI LILILILIIStyle Options х ✓ Bold Bold Italic Italic Font Hame: Monospaces Font Name: Monospaced Say it with style! - | Font size: 40 LIStyle Options х Say it with style! ✓ Bold Italic Font Name: Monospaced Font size: 23 10 15 20 25 30 35 40

//PLEASE LIKE THE ANSWER AND COMMENT IF YOU HAVE DOUBTS.

Add a comment
Know the answer?
Add Answer to:
answer this question please, use java language Exercise 5: StyleOption program a) Modify the "StyleOption" program,...
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
  • Java Using NetBean Write a program that can dynamically change the font of a message. The font name or font size can be chosen from combo boxes. The font name can be Dialog, DialogInput, Monospaced, S...

    Java Using NetBean Write a program that can dynamically change the font of a message. The font name or font size can be chosen from combo boxes. The font name can be Dialog, DialogInput, Monospaced, Serif, or SansSerif. The font style can be selected as PLAIN, BOLD and/or ITALIC. Font myFont = new Font("Serif", Font.PLAIN, 12); message.setFont(myFont);

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

    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...

  • please write code in java language and do not add any break, continue or goto statements...

    please write code in java language and do not add any break, continue or goto statements Write a program with a graphical interface that allows the user to convert an amount of money between U.S. dollars (USD), euros (EUR), and British pounds (GBP). The user interface should have the following elements: a text box to enter the amount to be converted, two combo boxes to allow the user to select the currencies, a button to make the conversion, and a...

  • In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program...

    In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program (name it SimpleSort) to implement and test the three sort algorithms (Bubble, Insertion, Selection) discussed in the lecture slides. Define method BubbleSort() to implement Bubble sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define method InsertionSort() to implement insertion sort of an array of integers. Modify the algorithm implementation to count...

  • VII JAVA ASSIGNMENT. Write a program with a graphical interface that allows the user to convert...

    VII JAVA ASSIGNMENT. Write a program with a graphical interface that allows the user to convert an amount of money between U.S. dollars (USD), euros (EUR), and British pounds (GBP). The user interface should have the following elements: a text box to enter the amount to be converted, two combo boxes to allow the user to select the currencies, a button to make the conversion, and a label to show the result. Display a warning if the user does not...

  • Use Java language to write this program Programming Exercise 3.20 required you to design a PID...

    Use Java language to write this program Programming Exercise 3.20 required you to design a PID manager that allocated a unique process identifier to each process. Exercise 4.20 required you to modify your solution to Exercise 3.20 by writing a program that created a number of threads that requested and released process identifiers. Now modify your solution to Exercise 4.20 by ensuring that the data structure used to represent the availability of process identifiers is safe from race conditions. Use...

  • Please make a JAVA program for the following using switch structures Write a program that simulates...

    Please make a JAVA program for the following using switch structures Write a program that simulates a simple Calculator program. The program will display menu choices to the user to Add, Subtract, Multiply and Divide. The program will prompt the user to make a selection from the choices and get their choice into the program. The program will use a nested if….else (selection control structure) to determine the user’s menu choice. Prompt the user to enter two numbers, perform the...

  • ** Use Java programming language Program the following algorithms in JAVA: A. Classical matrix multiplication B....

    ** Use Java programming language Program the following algorithms in JAVA: A. Classical matrix multiplication B. Divide-and-conquer matrix multiplication In order to obtain more accurate results, the algorithms should be tested with the same matrices of different sizes many times. The total time spent is then divided by the number of times the algorithm is performed to obtain the time taken to solve the given instance. For example, you randomly generate 1000 sets of input data for size_of_n=16. For each...

  • in JAVA please thanks. (need answer in a few hours !!) Exercise #1: Design and implement...

    in JAVA please thanks. (need answer in a few hours !!) Exercise #1: Design and implement a program (name it LinearBinarySearch) to implement and test the linear and binary search algorithm discussed in the lecture slides. Define method LinearSearch() to implement linear search of an array of integers. Modify the algorithm implementation to count number of comparisons it takes to find a target value (if exist) in the array. Define method BinarySearch() to implement binary search of an array of...

  • Java language: C Write a game program that allows the user to collect 4 tokens hidden...

    Java language: C Write a game program that allows the user to collect 4 tokens hidden in an array (cells) of size 10. The program asks the user to select a cell. Determine if there is a token in that cell or not, and give the user appropriate feedback. This program should keep letting user to select until the all tokens are collected. The program should display the total number of selections. Use rand_position Example input/output: There are four tokens....

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