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

标签 java eclipse arguments

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

相关文章:

java - Android Studio 在运行时找不到类

java - .jar 文件无法打开

java - Eclipse 插件 : disable/enable dynamically an action from main menubar

java - 如何在 Eclipse 中拆分字符串并将项目添加到 ListView(用于循环/迭代)?

function - 这种柯里化(Currying)函数的语法有什么区别吗?

java - Vaadin:将枚举值绑定(bind)到组合框(ConversionException)

java - Thymeleaf 中的可选参数?

Eclipse 无法正确部署 Gradle 项目

regex - 如何在Groovy的命令行参数中使用星号(*)?

c++ - 为什么带引号的字符串在 std::string 之前匹配 bool 方法签名?