写一个登陆窗口的程序,当用户名和密码分别为mr和softmr时正确。可是我这个程序总说监听事件那个函数有问题。
请高人指教下 。package register;import java.awt.Event.*;
import java.awt.*;
import javax.swing.*;public class MyRegisterFrame extends JFrame
{
public void CreatRegisterFrame(String title)
{
JFrame jf = new JFrame(title); //定义容器和各部件;
Container c = jf.getContentPane();
JLabel jl1 = new JLabel("用户名");
JLabel jl2 = new JLabel("密码");
JTextField jt = new JTextField("");
JPasswordField jp = new JPasswordField("");
JButton jb1 = new JButton("登陆");
JButton jb2 = new JButton("重置");
jf.setSize(600, 400);
jf.setVisible(true);
c.setBackground(Color.blue);
c.add(jl1); //添加各部件到容器;
c.add(jl2);
c.add(jt);
c.add(jp);
c.add(jb1);
c.add(jb2);
jl1.setBounds(150, 100, 10, 5);
jl2.setBounds(150, 105, 10, 5);
jt.setBounds(160, 100, 20, 5);
jp.setBounds(160, 105, 20, 5);
jb1.setBounds(180, 115, 10, 5);
jb2.setBounds(195, 115, 10, 5);
jp.setEchoChar('*');
jb1.addActionListener(new ActionListener() //为“登陆”按钮添加监听事件;
{
public void actionPerformde(ActionEvent e)
{
if (jt.getText() == "mr" && jp.getText() == "mrsoft")
{
new MyDialog(jf, "登陆成功").setVisible(true);
}
else
{
new MyDialog(jf, "用户名或密码错误").setVisible(true);
}
}
});
jb2.addActionListener(new ActionListener() //为“按钮”添加监听事件;
{
public void actionPerformde(ActionEvent e)
{
jt.setText("");
jp.setText("");
}
});
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new MyRegisterFrame().CreatRegisterFrame("用户登陆窗口");
}
}class MyDialog extends JDialog //自定义对话框类;
{
public MyDialog(JFrame jf, String str)
{
super(jf, "提示信息", true);
Container c = getContentPane();
c.add(new JLabel(str));
c.setSize(100, 100);
}
}ActionListener cannot be resolved to a type MyRegisterFrame.java RegisterFrame/src/register line 55 Java Problem
ActionListener cannot be resolved to a type MyRegisterFrame.java RegisterFrame/src/register line 39 Java Problem报错说
请高人指教下 。package register;import java.awt.Event.*;
import java.awt.*;
import javax.swing.*;public class MyRegisterFrame extends JFrame
{
public void CreatRegisterFrame(String title)
{
JFrame jf = new JFrame(title); //定义容器和各部件;
Container c = jf.getContentPane();
JLabel jl1 = new JLabel("用户名");
JLabel jl2 = new JLabel("密码");
JTextField jt = new JTextField("");
JPasswordField jp = new JPasswordField("");
JButton jb1 = new JButton("登陆");
JButton jb2 = new JButton("重置");
jf.setSize(600, 400);
jf.setVisible(true);
c.setBackground(Color.blue);
c.add(jl1); //添加各部件到容器;
c.add(jl2);
c.add(jt);
c.add(jp);
c.add(jb1);
c.add(jb2);
jl1.setBounds(150, 100, 10, 5);
jl2.setBounds(150, 105, 10, 5);
jt.setBounds(160, 100, 20, 5);
jp.setBounds(160, 105, 20, 5);
jb1.setBounds(180, 115, 10, 5);
jb2.setBounds(195, 115, 10, 5);
jp.setEchoChar('*');
jb1.addActionListener(new ActionListener() //为“登陆”按钮添加监听事件;
{
public void actionPerformde(ActionEvent e)
{
if (jt.getText() == "mr" && jp.getText() == "mrsoft")
{
new MyDialog(jf, "登陆成功").setVisible(true);
}
else
{
new MyDialog(jf, "用户名或密码错误").setVisible(true);
}
}
});
jb2.addActionListener(new ActionListener() //为“按钮”添加监听事件;
{
public void actionPerformde(ActionEvent e)
{
jt.setText("");
jp.setText("");
}
});
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new MyRegisterFrame().CreatRegisterFrame("用户登陆窗口");
}
}class MyDialog extends JDialog //自定义对话框类;
{
public MyDialog(JFrame jf, String str)
{
super(jf, "提示信息", true);
Container c = getContentPane();
c.add(new JLabel(str));
c.setSize(100, 100);
}
}ActionListener cannot be resolved to a type MyRegisterFrame.java RegisterFrame/src/register line 55 Java Problem
ActionListener cannot be resolved to a type MyRegisterFrame.java RegisterFrame/src/register line 39 Java Problem报错说
2.
JFrame jf = new JFrame(title);改为final JFrame jf = new JFrame(title);
JTextField jt = new JTextField("");改为final JTextField jt = new JTextField("");
JPasswordField jp = new JPasswordField("");改为final JPasswordField jp = new JPasswordField("");以上两类会造成该类有编译错误的
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;public class MyRegisterFrame extends JFrame {
public void CreatRegisterFrame(String title) {
final JFrame jf = new JFrame(title); // 定义容器和各部件;
Container c = jf.getContentPane();
JLabel jl1 = new JLabel("用户名");
JLabel jl2 = new JLabel("密码");
final JTextField jt = new JTextField("");
final JPasswordField jp = new JPasswordField("");
JButton jb1 = new JButton("登陆");
JButton jb2 = new JButton("重置"); jf.setSize(600, 400);
jf.setVisible(true);
c.setBackground(Color.blue); c.add(jl1); // 添加各部件到容器;
c.add(jl2);
c.add(jt);
c.add(jp);
c.add(jb1);
c.add(jb2); jl1.setBounds(150, 100, 10, 5);
jl2.setBounds(150, 105, 10, 5);
jt.setBounds(160, 100, 20, 5);
jp.setBounds(160, 105, 20, 5);
jb1.setBounds(180, 115, 10, 5);
jb2.setBounds(195, 115, 10, 5);
jp.setEchoChar('*'); jb1.addActionListener(new ActionListener() // 为“登陆”按钮添加监听事件;
{
public void actionPerformed(ActionEvent e) {
if (jt.getText() == "mr" && jp.getText() == "mrsoft") {
new MyDialog(jf, "登陆成功").setVisible(true);
} else {
new MyDialog(jf, "用户名或密码错误").setVisible(true);
}
} }); jb2.addActionListener(new ActionListener() // 为“按钮”添加监听事件;
{
public void actionPerformed(ActionEvent e) {
jt.setText("");
jp.setText("");
} }); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} public static void main(String[] args) {
new MyRegisterFrame().CreatRegisterFrame("用户登陆窗口");
}
}class MyDialog extends JDialog // 自定义对话框类;
{
public MyDialog(JFrame jf, String str) {
super(jf, "提示信息", true);
Container c = getContentPane();
c.add(new JLabel(str));
c.setSize(100, 100);
}
}