标签名称无法解析

label name can not be resolved

我创建了一个标签 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);
    }
}

您指的是您之前添加到 btnSubmit 的匿名 ActionListener class 中的局部变量 loginLabel。这将不起作用,因为 ActionListener class(或任何其他 class)中的代码看不到方法的局部变量。

相反,您应该将 loginLabel 声明为一个字段,位于 class:

的顶部
private JPasswordField txtPassword;
private JLabel loginLabel;

这对 class 内的所有方法以及 class 内定义的任何非静态 classes(例如前面提到的 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 隐式地将已定义但从未更改的本地字段标记为最终字段。如果您的目标是 java 7 或更早版本,您需要明确地使该变量成为最终变量。

为了避免像"LabelX can not be resolved"或"ButtonY can not be resolved"这样的问题,我学会了这样做:

  1. 在 Eclipse 上,转到 "Window" 模块...
  2. 选择项目"Preferences"...
  3. 选择"WindowBuilder"...
  4. 选择"Swing"或"SWT"...
  5. 在"Code Generation"中,在变量生成中选择"Field"...
  6. 在 "Event handlers" 中,选择 "Implement listener interface in parent class"...

它适用于 Java 1.7 或 1.8。告别 ActionListner Class 和 GUI 组件 Swing 或 SWT 中的错误。当然,我正在使用 WindowBuilder 插件来构建 GUI 应用程序。