方法 setFont() 不适用于参数 [JLabel] (eclipse)

Method setFont() not applicable for arguments [JLabel] (eclipse)

这是下面的代码,由于在 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)

导入 java.awt.Font。在您的程序中,您正在创建 Font.java class 的实例。编译器抱怨说,你应该提供 java.awt.Font 的实例而不是你的字体 class

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, 12));
        frame.setVisible(true);
        frame.add(string1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


   }
 }

程序正在使用名称 Font 调用您的 class。 要么更改 class 的名称,要么在上面的程序中使用新的 java.awt.Font 而不是字体,例如:

正在更改 class 名称

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

没有Class更名

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