2015-11-05 126 views
0

這是下面的代碼,並且由於在JLabel上應用了setfont()函數而出現錯誤。 setFont()的語法似乎是正確的。方法setFont()不適用於參數[JLabel](eclipse)

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) 

回答

3

該計劃呼籲與名稱字體類的。 要麼改變你的類的名稱,或在上面的程序中使用新的java.awt.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); 
} 
} 

OR

無類名稱的變化

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

Thanks ...更改班級名稱解決了我的問題:) –

+0

您的歡迎:) –

0

進口java.awt.Font。在你的程序中,你將創建Font.java類的實例。編譯器抱怨,你應該提供的java.awt.Font的實例,而不是你的字體類

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


    } 
} 
相關問題