Question

What is the solution for chapter 15 3e for java programming 8th edition Create a JFrame...

What is the solution for chapter 15 3e for java programming 8th edition

Create a JFrame that holds fjive buttons with the names of five different fonts. Include a sexth button that the user can click to make a font larger or smaller. Display a demonstration JLabel usig the font and size that th user selects.

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

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.Random;

import java.util.HashSet;

public class JFontFrame extends JFrame implements ActionListener

{

private final String demoText = "The font you selected looks like this.";

private JLabel demonstration = new JLabel("Please select choose a font", JLabel.CENTER);

private JButton[] fontButtons = new JButton[4];

GridLayout mainLayout = new GridLayout(2, 0);

Container buttonArea = new Container();

public JFontFrame()

{

super("JFontFrame");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(350, 300);

setResizable(false);

setLocationRelativeTo(null);

setLayout(mainLayout);

buttonArea.setLayout(new FlowLayout());

addWindowComponents();

add(demonstration);

add(buttonArea);

}

private void addWindowComponents()

{

String[] fonts = chooseFonts();

for(int i = 0; i < fontButtons.length; i++)

{

fontButtons[i] = new JButton(fonts[i]);

buttonArea.add(fontButtons[i]);

fontButtons[i].addActionListener(this);

}

}

private String[] chooseFonts()

{

Random random = new Random();

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

Font[] fonts = ge.getAllFonts();

HashSet<Integer> set = new HashSet<Integer>(); // Thanks to google

int i = 0;

String[] fontList = new String[4];

while(i < 4)

{

int rand = random.nextInt(fonts.length);

if(!(set.contains(rand)))

{

set.add(rand);

fontList[i] = fonts[rand].getFontName();

}

i++;

}

return fontList;

}

public void actionPerformed(ActionEvent e)

{

JButton source = (JButton)e.getSource();

String choice = source.getText();

changeDemonstration(choice);

}

private void changeDemonstration(String f)

{

try

{

demonstration.setText(demoText);

Font newFont = new Font(f, Font.PLAIN, 12);

demonstration.setFont(newFont);

}

catch(Exception e)

{

JOptionPane.showMessageDialog(null, "Error setting font!\n" + e);

System.exit(1);

}

}

public static void main(String[] args)

{

JFontFrame fontWindow = new JFontFrame();

fontWindow.setVisible(true);

}

}

Add a comment
Know the answer?
Add Answer to:
What is the solution for chapter 15 3e for java programming 8th edition Create a JFrame...
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
  • Using Java, please create the program for the following prompt. MUST CREATE BUTTONS IN A JFRAME,...

    Using Java, please create the program for the following prompt. MUST CREATE BUTTONS IN A JFRAME, as specified by the prompt! DO NOT use user input of 1, 2, 3 etc. User input must come from clicking the buttons. Please be sure to test your program. Thank you! Write a program that displays three buttons with the names or images of three candidates for public of office. Imagine that a person votes by clicking the button that shows the candidate...

  • Java (8th Edition) Chapter 6, Problem 12PGP Looking for a solution to this question. Edit: I...

    Java (8th Edition) Chapter 6, Problem 12PGP Looking for a solution to this question. Edit: I was missing the question.. Create a GUI or JavaFX application that displays a button with the text "Button 1". Underneath the button add a label with the text "label 1". Repeat with and additional "Button 2" and "Label 2". Add an image icon of your choice to the first button and the first label.

  • By using Java Programming Langauge for Aodroid Studio wite the code for this project: The Tech...

    By using Java Programming Langauge for Aodroid Studio wite the code for this project: The Tech Gadgets app shows the top five technology gifts on your wish list. Algorithms: 1. The opening screen displays names of five technology gadgets of your own choosing. 2. If the user selects any of the gadgets, a second screen opens that has an image and a button. If the user clicks the button, a webpage opens that displays more information about the tech gadget....

  • JAVA CODING Must be compilable, thanks The purpose is to provide an exercise in event-driven programming...

    JAVA CODING Must be compilable, thanks The purpose is to provide an exercise in event-driven programming and image handling using JavaFX. Create an application that responds to the user clicking command buttons. Initially, it will display one of two graphic images and, based upon the button clicked by the user, will switch the image displayed. If the user clicks the mouse button to display the same image as is currently displayed, the image is not changed, but a message at...

  • qbasic is the programming language Chapter 8 Do Programming Exercise 8-3 om page 414. Create a...

    qbasic is the programming language Chapter 8 Do Programming Exercise 8-3 om page 414. Create a string array to hold the months of the year. Call it anything you want to just make sure it's a string array is at the end of the name, e.g. months). Create a DATA statement that holds the month names: DATA "jan","Feb", "Mar" etc. Next you'll need a FOR loop to READ the DATA into the array, Call a SUB to get the rainfall...

  • Chapter 7 Exercise 18, Introduction to Java Programming, Tenth Edition Y. Daniel Liang. Please write your...

    Chapter 7 Exercise 18, Introduction to Java Programming, Tenth Edition Y. Daniel Liang. Please write your own code. 7.18 (Bubble sort) Write a sort method that uses the bubble-sort algorithm. The bubblesort algorithm makes several passes through the array. On each pass, successive neighboring pairs are compared. If a pair is not in order, its values are swapped; otherwise, the values remain unchanged. The technique is called a bubble sort or sinking sort because the smaller values gradually “bubble” their...

  • Project 2 Description Create a Visual C# project that when an employee's biweekly sales amount is...

    Project 2 Description Create a Visual C# project that when an employee's biweekly sales amount is entered and the Calculate button is pressed, the gross pay, deductions, and net pay will be displayed. Each employee will receive a base pay of $1200 plus a sales commission of 8% of sales. Once the net pay has been calculated, display the budget amount for each category listed below based on the percentages given.         base pay = $1200; use a named...

  • Java programming The purpose of this problem is to practice using a generic Urn class. NOTE:...

    Java programming The purpose of this problem is to practice using a generic Urn class. NOTE: Refer to the code for the ArrayStack class from Chapter 12. Use that code as a starting point to create the Urn class, modifying it to remove the methods in the ArrayStack class (push, pop, etc) then add the methods described below. Also change the variable names to reflect the new class. For example the array name should NOT be stack, instead it should...

  • This homework problem has me pulling my hair out. We are working with text files in Java using Ne...

    This homework problem has me pulling my hair out. We are working with text files in Java using NetBeans GUI application. We're suppose to make a movie list and be able to pull up movies already in the text file (which I can do) and also be able to add and delete movies to and from the file (which is what I'm having trouble with). I've attached the specifications and the movie list if you need it. Any help would...

  • Objective: GUI Layout manager Download one of the sample GUI layout program. Use any GUI layout...

    Objective: GUI Layout manager Download one of the sample GUI layout program. Use any GUI layout to add buttons to start each sort and display the System.nanoTime in common TextArea panel. The question is a bit confusing so i will try to simplify it. Using the GUI ( I made a unclick able one so you have to make it clickable), allow a user to sort the text file based on what they click on. example: if i click merge...

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