使用文本框更改Java中的字体大小
问题描述:
好吧,所以我需要一些帮助手,我使用一组varibles来声明我的字体等,我已经通过使用deriveFont使粗体和斜体函数工作方法 - 创建一个新的字体和应用新的风格等使用文本框更改Java中的字体大小
这一个更棘手,因为即时通讯从文本框中获取值并将其应用到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();
}
答
当心,Font#derive(int)
改变字体样式,而不是它的大小,你可以尝试使用Font#deriveFont(float)
这是用来更改字体的大小...
此外,deriveFont
创建一个的新实例根据您提供的,所以你需要保持对它的引用,例如值...
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,所以是的,我跟着你使用了浮动 - 但是字体大小在更新时不会更新这一点。 – anthonycook2014
嗯,我不知道你在做什么,因为它对我来说工作正常 – MadProgrammer
我看到你在示例中添加了什么,现在做了错误,欢呼! – anthonycook2014