這是下面的代碼,並且由於在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)
Thanks ...更改班級名稱解決了我的問題:) –
您的歡迎:) –