使用文本框更改Java中的字体大小

时间:2014-02-21 03:31:05

标签: java swing text fonts textbox

好吧,所以我需要一点帮助,我使用一组变量来声明我的字体等,我通过使用deriveFont方法使粗体和斜体函数工作 - 创建一个新字体并应用新的风格等。

这个更棘手,因为我从文本框中取出值并将其应用于fontSize变量然后将其应用于currentFont我有...

HELP? :(

我的代码片段如下......

public class Practice01 extends javax.swing.JFrame {

private int fontStyle;
private int fontSize = 12;


private void changeSize(){

    Font currentFont = edtText.getFont(); //getting the current font
    fontSize = Integer.parseInt(txtSize.getText()); //getting the number input from the text box and putting it to the fontSize variable
    txtSize.setText(Integer.toString(fontSize)); //setting the txtSize entry to the fontSize variable?
    currentFont.deriveFont(fontSize); //deriving a new font
    edtText.setFont(currentFont.deriveFont(fontSize)); //setting the new font and size to the text box.


}

这是我的字体变量位......

Font serifFont;
Font monoFont;
Font sansserifFont;

public Practice01() {
    serifFont = new Font ("Serif", fontStyle, fontSize);
    monoFont = new Font ("Monospaced", fontStyle, fontSize);
    sansserifFont = new Font ("SansSerif", fontStyle, fontSize);
    initComponents();
}

1 个答案:

答案 0 :(得分:3)

注意,Font#derive(int)更改字体样式,而不是它的大小,您可以尝试使用Font#deriveFont(float)来更改字体的大小......

此外,deriveFont会根据您提供的值创建Font的新实例,因此您需要维护对它的引用,例如......

Font font = currentFont.deriveFont((float)fontSize); //deriving a new font
edtText.setFont(font);

使用示例更新

enter image description here

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class FontTest {

    public static void main(String[] args) {
        new FontTest();
    }

    public FontTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JTextField field;

        public TestPane() {
            setLayout(new GridBagLayout());
            field = new JTextField(5);
            add(field);
            field.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String text = field.getText();
                    try {
                        float size = Float.parseFloat(text);
                        Font font = field.getFont();
                        font = font.deriveFont(size);
                        field.setFont(font);
                        revalidate();
                    } catch (Exception exp) {
                        exp.printStackTrace();
                    }
                }
            });
        }

    }

}