这个错误如何改?
package com.cose;import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
public class Main {
static TextField user ,password;
static Frame mainFrame=null;
public static void main(String[] args)
{
new LoginFrame("学生成绩管理系统");
user=new TextField(20);
password = new TextField(20);
Button login=new Button("登陆");
login.setSize(20,10);
login.addActionListener(new Monitor());
mainFrame.add(new Label("用户名:"));
mainFrame.add(user);
mainFrame.add(new Label("密码:"));
mainFrame.add(password);
mainFrame.add(login);
mainFrame.pack();
mainFrame.setVisible(true);
}
private class Monitor implements ActionListener
{
public void actionPerformed ( ActionEvent e)
{
String stduser="yang";
String stdpsw="123456";
if(user.getText().trim()=="")
{
JOptionPane.showMessageDialog(null,"用户名不准为空");
return;
}
if(password.getText().trim()=="")
{
JOptionPane.showMessageDialog(null,"密码不准为空");
return;
}
if(stduser.compareTo(user.getText())==0&&stdpsw.compareTo(password.getText())==0)
{
Frame f=new Frame("课程设定");
f.setVisible(true);
mainFrame.dispose();
}
}
}
}
错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
No enclosing instance of type Main is accessible. Must qualify the allocation with an enclosing instance of type Main (e.g. x.new A() where x is an instance of Main).
at com.cose.Main.main(Main.java:22)
--------------------编程问答-------------------- 看了下楼主的代码,不知是不是眼拙,我找不到类LoginFrame,是不是在包com.cose里呢?
我估计问题出在了这行代码上,建议楼主将其注释掉,测试一下吧!:
new LoginFrame("学生成绩管理系统");--------------------编程问答-------------------- mainFrame = new LoginFrame("学生成绩管理系统");
要让引用找对象.... --------------------编程问答-------------------- 唉, 不要让手底下的引用们独守空房 --------------------编程问答-------------------- package com.cose;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JOptionPane;
public class LoginFrame extends Frame {
LoginFrame(String s)
{
super(s);
setLayout(new FlowLayout());
setBounds(300,50,800,600);
this.setBackground( new Color(132,134,126));
this.addWindowListener(new WindowAdapter(){
public void windowClosing( WindowEvent e){
setVisible(false);
System.exit(0);
}
});
}
}
补充:Java , Java相关