标签名称无法解析

时间:2017-03-27 01:43:20

标签: java swing jlabel windowbuilder

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

2 个答案:

答案 0 :(得分:0)

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

相反,您应该将loginLabel声明为一个字段,位于该类的顶部:

private JPasswordField txtPassword;
private JLabel loginLabel;

这对于类中的所有方法以及类中定义的任何非静态类(例如前面提到的ActionListener)都是可见的。

然后只需在初始化()方法中更改创建标签的行:

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

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

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

答案 1 :(得分:0)

为避免出现“ LabelX无法解决”或“ ButtonY无法解决”之类的问题,我学会了做到这一点:

  1. 在Eclipse上,转到“窗口”模块...
  2. 选择项目“首选项” ...
  3. 选择“ WindowBuilder” ...
  4. 选择“ Swing”或“ SWT” ...
  5. 在“代码生成”中,在变量生成中选择“字段” ...
  6. 在“事件处理程序”中,选择“父类中的实现侦听器接口” ...

Preferences Eclipse IDE

它可以在Java 1.7或1.8上运行。再见ActionListner类和GUI组件Swing或SWT中的错误。当然,我正在使用WindowBuilder插件来构建GUI应用程序。

相关问题