这是下面的代码,由于在 JLabel 上应用了 setfont() 函数,我收到错误。 setFont()
的语法似乎是正确的。
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Font {
public static void main(String Args[])
{
JFrame frame=new JFrame();
frame.setBounds(100, 100, 450, 300);
JLabel string1=new JLabel("Some Text");
string1.setBounds(100,100,450,300);
JTextField txt=new JTextField("add");
string1.setFont (new Font("Arial", Font.Bold, 12));
frame.setVisible(true);
frame.add(string1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
错误是:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method setFont(java.awt.Font) in the type JComponent is not applicable for the arguments (Font)
Bold cannot be resolved or is not a field
at Font.main(Font.java:13)
最佳答案
程序正在调用名为 Font 的类。 更改类的名称或使用新的 java.awt.Font 而不是上面程序中的 Font,例如:
更改类名
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Font1 {
public static void main(String Args[])
{
JFrame frame=new JFrame();
frame.setBounds(100, 100, 450, 300);
JLabel string1=new JLabel("Some Text");
string1.setBounds(100,100,450,300);
JTextField txt=new JTextField("add");
string1.setFont (new Font("Arial", Font.BOLD, 22));
frame.setVisible(true);
frame.add(string1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
或者
不更改类名
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Font {
public static void main(String Args[])
{
JFrame frame=new JFrame();
frame.setBounds(100, 100, 450, 300);
JLabel string1=new JLabel("Some Text");
string1.setBounds(100,100,450,300);
JTextField txt=new JTextField("add");
string1.setFont (new java.awt.Font("Arial", java.awt.Font.BOLD, 22));
frame.setVisible(true);
frame.add(string1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
关于java - 方法 setFont() 不适用于参数 [JLabel] (eclipse),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33545649/