Forum Stats

  • 3,874,444 Users
  • 2,266,734 Discussions
  • 7,911,851 Comments

Discussions

Display a Phrase in Every Font Size from 6 thru 20

2604068
2604068 Member Posts: 7
edited Jun 1, 2014 4:01PM in New To Java

This is what I have thus far.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class JFontSize extends JFrame implements ActionListener

{

     JButton button = new JButton("Press Me");

     String aPhrase = new String("This is a phrase.");

     Font aFont = new Font("Monospaced", Font.BOLD, 20);

     int x = 50, y = 75;

     final int GAP = 25;

     final int WIDTH= 800;

     final int HEIGHT = 600;

   

     public JFontSize()

     {

          Container con = getContentPane();

          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          con.setLayout(new FlowLayout());

          con.add(button);

          button.addActionListener(this);

          setSize(WIDTH, HEIGHT);
    }

  

     public void actionPerformed(ActionEvent e)

     {

          Graphics text = getGraphics();

          text.drawString(aPhrase, x += GAP, y += GAP);

     public void paint(Graphics g)

     {

          super.paint(g);

          g.drawString(aPhrase, x += GAP, y += GAP);

     }

     public static void main(String[] args)

     {

          JFontSize jfSize = new JFontSize();

          jfSize.setVisible(true);

     }

}

The result looks something like this:

               Button

     This is a phrase.

        This is a phrase.

            This is a phrase.

                This is a phrase.

                    etc.

Answers

This discussion has been closed.