我创建了一个标签loginLabel来显示有关错误身份验证的消息。我已将此逻辑保留在actionPerformed方法中,但由于loginLabel can not be resolved而出现错误。如何清除此错误?我已经使用window-builder创建了GUI。

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.*;

public class FirstView {

    private JFrame frmFirstProject;
    private JTextField txtUsername;
    private JPasswordField txtPassword;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    FirstView window = new FirstView();
                    window.frmFirstProject.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public FirstView() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frmFirstProject = new JFrame();
        frmFirstProject.getContentPane().setBackground(new Color(51, 204, 204));
        frmFirstProject.setTitle("first project");
        frmFirstProject.setBounds(100, 100, 714, 491);
        frmFirstProject.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmFirstProject.getContentPane().setLayout(null);

        JPanel panel = new JPanel();
        panel.setBorder(new LineBorder(new Color(0, 0, 0), 3));
        panel.setBounds(63, 47, 580, 354);
        frmFirstProject.getContentPane().add(panel);
        panel.setLayout(null);

        JLabel lblUsername = new JLabel("Username:");
        lblUsername.setBounds(24, 25, 69, 22);
        panel.add(lblUsername);

        JLabel lblPassword = new JLabel("Password:");
        lblPassword.setBounds(24, 66, 69, 22);
        panel.add(lblPassword);

        txtUsername = new JTextField();
        txtUsername.setBounds(97, 26, 86, 20);
        panel.add(txtUsername);
        txtUsername.setColumns(10);

        txtPassword = new JPasswordField();
        txtPassword.setBounds(97, 67, 86, 20);
        panel.add(txtPassword);

        JButton btnsubmit = new JButton("submit");
        frmFirstProject.getRootPane().setDefaultButton(btnsubmit); //enter key
        btnsubmit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    int val = 0;
                    Connection sqlCon = DB_con.getSQLConnection();
                    PreparedStatement ps = sqlCon.prepareStatement("select 1 from tbl_user_info where username = ? and password = ?");
                    ps.setString(1, txtUsername.getText().toUpperCase());
                    ps.setString(2, String.valueOf(txtPassword.getPassword()).toUpperCase());
                    ResultSet rs = ps.executeQuery();
                    if (rs.next()) {
                        val = rs.getInt(1);
                    }
                    if(val != 1)
                    {
                        loginLabel.setVisible(true);
                    }
                    System.out.println("the value is: "+val);
                    sqlCon.close();
                    System.exit(0);
                } catch (Exception e) {
                    System.out.println(e.toString());
                }

            }
        });
        btnsubmit.setBounds(94, 125, 89, 23);
        panel.add(btnsubmit);

        JPanel panel_display = new JPanel();
        panel_display.setBounds(36, 181, 355, 50);
        panel.add(panel_display);
        panel_display.setLayout(null);

        JLabel loginLabel = new JLabel("Login Authentication Mismatched. Please try again.");
        loginLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
        loginLabel.setBounds(10, 11, 319, 28);
        panel_display.add(loginLabel);
        loginLabel.setVisible(false);

        JLabel lbl_header = new JLabel("LIBRARY MANAGEMENT SYSTEM");
        lbl_header.setFont(new Font("Tahoma", Font.BOLD, 14));
        lbl_header.setBounds(211, 11, 288, 25);
        frmFirstProject.getContentPane().add(lbl_header);
    }
}

最佳答案

您正在引用先前添加到loginLabel的匿名ActionListener类中的局部变量btnSubmit。这将不起作用,因为ActionListener类(或任何其他类)中的代码无法查看方法本地的变量。

相反,您应该在类顶部声明loginLabel为字段:

private JPasswordField txtPassword;
private JLabel loginLabel;


这对于该类内部的所有方法以及您的类内部定义的任何非静态类(例如上述的ActionListener)都是可见的。

然后只需将以下代码更改为在您的initialize()方法内创建标签的行:

JLabel loginLabel = new JLabel("Login Authentication Mismatched. Please try again.");




loginLabel = new JLabel("Login Authentication Mismatched. Please try again.");


在Java 8中,在ActionListener之前定义loginLabel就足够了,因为Java 8隐式标记了已定义但从未更改为final的本地字段。如果您以Java 7或更早版本为目标,则需要明确将该变量定为final。

09-26 05:52