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