非靜態方法的getContentPane()不能從靜態上下文錯誤中的Java Swing非靜態方法的getContentPane()不能從靜態上下文錯誤中的Java Swing引用
import javax.swing.*;
import java.awt.*;
import java.awt.Component;
import java.awt.event.*;
public class Studentlogin extends JFrame{
public static void main(String[] args) {
Container c = getContentPane();
setTitle(" Staff Signin ");
setSize(400 , 300);
setLayout(new FlowLayout());
setVisible(true);
setLayout(null);
JLabel tun = new JLabel("UserName");
tun.setBounds(10,10,140,25);
c.add(sun);
JTextField tuname = new JTextField(10);
tuname.setToolTipText("Enter your StaffId ");
tuname.setBounds(145,10,200,25);
c.add(tuname);
JLabel tpw = new JLabel("PassWord");
tpw.setBounds(10,50,140,25);
c.add(tpw);
JPasswordField tpword = new JPasswordField(10);
tpword.setEchoChar('*');
tpword.setBounds(145,50,200,25);
c.add(tpword);
}
}
被引用在編譯時這我得到這種類型的錯誤的任何人都可以找我有什麼不對這個代碼,因爲我可以在actionlistrener段
Studentlogin.java:9: error: non-static method getContentPane() cannot be referen
ced from a static context
Container c = getContentPane();
^
Studentlogin.java:11: error: non-static method setTitle(String) cannot be refere
nced from a static context
setTitle(" Staff Signin ");
^
Studentlogin.java:12: error: non-static method setSize(int,int) cannot be refere
nced from a static context
setSize(400 , 300);
^
該錯誤消息幾乎是一個答案。請一遍又一遍地閱讀。 – 2014-09-19 12:30:47
你爲什麼試圖從主要方法.. jFrame必須我主線程的子線程.. – Lijo 2014-09-19 12:32:24
這是正確的,你不能從靜態方法調用任何非靜態方法。 – justrohu 2014-09-19 12:32:45