Skip to Main Content

New to Java

Display a Phrase in Every Font Size from 6 thru 20

2604068Jun 1 2014 — edited Jun 1 2014

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.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 29 2014
Added on Jun 1 2014
1 comment
1,132 views