1 Reply Latest reply: Jun 1, 2014 3:01 PM by rp0428 RSS

    Display a Phrase in Every Font Size from 6 thru 20

    e752a7a0-bab5-4ee4-9ea4-bf7c4a2cadab

      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.