在循环中使用字体大小

时间:2015-11-19 21:44:08

标签: java swing fonts

我正在尝试编写一个程序,显示每个字体大小为6到20的短语。我创建了一个for循环来增加每次显示短语时的字体大小和y轴。但是,当我运行该程序时,它只显示字体大小为20的短语...我错过了什么?

import javax.swing.*;
import java.awt.*;
public class JFontSizes extends JFrame
{
   String phrase = new String("This is a phrase in every font size from 6 through 20!");
   int font = 6;
   int y = 100;

   public JFontSizes()
     {
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }

    public void paint(Graphics gr)
     {
      while(font < 21)
        {
        super.paint(gr);
        Font timesPlain = new Font("Times New Roman",Font.PLAIN, font);
        gr.setFont(timesPlain);
        gr.drawString(phrase, 60, y);
        font++;
        y = y + 1;
        }      
      }

   public static void main(String[]args)
     {
        JFontSizes frame = new JFontSizes();
        frame.setSize(550,550);
        frame.setVisible(true);
     }
}

1 个答案:

答案 0 :(得分:4)

请勿在while循环中调用超强画法,因为它会删除之前绘制的字符串。在循环之前调用它。

其他问题:

  • 永远不要在JFrame中进行绘制。
  • 而是在JPanel的paintComponent方法中绘制。
  • 与上述相同,请在之前调用super.paintComponent(g) 。[/ li>
  • 然后在JFrame中显示JPanel。
  • 使font和y成为方法的局部变量。您希望每次执行重绘时重置它们。

如,

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.*;

@SuppressWarnings("serial")
public class FontSizes extends JPanel {
    private static final String PHRASE = new String("This is a phrase "
            + "in every font size from 6 through 20!");
    private static final int PREF_W = 500;
    private static final int PREF_H = 400;
    private static final int GAP = 8;
    private static final int STARTING_FONT_PTS = 6;
    private static final int MAX_FONT_PTS = 21;
    private static final Font STARTING_FONT = new Font("Times New Roman",
            Font.PLAIN, STARTING_FONT_PTS);

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        // let's make fonts appear smoother
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        int fontPoints = STARTING_FONT_PTS;
        int y = GAP;
        Font font = STARTING_FONT;
        while (fontPoints < MAX_FONT_PTS) {
            g.setFont(font);
            // let's space lines based on font height
            y += g.getFontMetrics().getAscent() + GAP;
            g.drawString(PHRASE, GAP, y);
            fontPoints++;
            font = font.deriveFont((float) fontPoints);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("FontSizes");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new FontSizes());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}