【问题标题】:Changing a font size in Java, using a textbox使用文本框在 Java 中更改字体大小
【发布时间】:2014-02-21 03:31:04
【问题描述】:

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

这个比较棘手,因为我从文本框中获取值并将其应用于 fontSize 变量,然后将其应用于我那里的 currentFont...

帮助?! :(

我的代码的 sn-p 如下...

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();
}

【问题讨论】:

    标签: java swing text fonts textbox


    【解决方案1】:

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

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

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

    更新示例

    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();
                        }
                    }
                });
            }
    
        }
    
    }
    

    【讨论】:

    • 从字面上看,我想做的就是从文本框中获取一个数字,将其放入变量 fontSize 中,并将新的 fontSize 应用于 currentFont,所以是的,我关注你关于使用浮点数- 但这样做时字体大小不会更新。
    • 好吧,我不知道你在做什么,因为它对我来说很好用
    • 我知道你现在在示例中添加了什么做错了,干杯!
    猜你喜欢
    • 2011-12-08
    • 1970-01-01
    • 2011-08-31
    • 2016-12-16
    • 2011-01-08
    • 2021-08-25
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    相关资源
    最近更新 更多