java - 需要帮助添加使用 system.exit(0) 的 JButton

标签 java swing actionlistener

来自非典型编码员,我需要一点帮助。我想在 f1 部分添加第二个 JButton,我可以对其进行编码以运行 system.exit(0) 以在确认密码后终止程序。除了使用单独的 ActionListener 编写第二个 JButton 之外,我几乎已经弄清楚了所需的一切。我只是想在 Windows 登录后将其编写为辅助授权程序,以便我可以从本地 SQL 数据库中提取密码。任何帮助将不胜感激。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class Login extends JFrame implements ActionListener {
    JLabel l1, l2, l3;
    JTextField tf1;
    JButton btn1;
    JPasswordField p1;

    Login() {
        setTitle("Company Name");
        setVisible(true);
        setSize(525, 300);
        setLayout(null);

        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        l1 = new JLabel("Enter Credentials:");
        l1.setForeground(Color.blue);
        l1.setFont(new Font("Serif", Font.BOLD, 20));

        l2 = new JLabel("Enter User Number:");
        l3 = new JLabel("Enter Password:");
        tf1 = new JTextField();
        p1 = new JPasswordField();
        btn1 = new JButton("Submit");

        l1.setBounds(100, 30, 400, 30);
        l2.setBounds(80, 70, 200, 30);
        l3.setBounds(80, 110, 200, 30);
        tf1.setBounds(300, 70, 200, 30);
        p1.setBounds(300, 110, 200, 30);
        btn1.setBounds(150, 160, 100, 30);

        add(l1);
        add(l2);
        add(tf1);
        add(l3);
        add(p1);
        add(btn1);
        btn1.addActionListener(this);

    }

    public void actionPerformed(ActionEvent e) {
        showData();
    }

    public void showData() {
        JFrame f1 = new JFrame();
        JLabel l, l0;

        String str1 = tf1.getText();
        char[] p = p1.getPassword();
        String str2 = new String(p);
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/emp?user=root&password=password");
            PreparedStatement ps = con.prepareStatement("select name from emp where id=? and password=?");
            ps.setString(1, str1);
            ps.setString(2, str2);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                f1.setVisible(true);
                f1.setSize(525, 300);
                f1.setLayout(null);

                f1.setLocationRelativeTo(null);
                l = new JLabel();
                l0 = new JLabel("you are succefully logged in..");
                l0.setForeground(Color.blue);
                l0.setFont(new Font("Serif", Font.BOLD, 30));
                l.setBounds(60, 50, 400, 30);
                l0.setBounds(60, 100, 400, 40);

                f1.add(l);
                f1.add(l0);
                l.setText("Welcome " + rs.getString(1));
                l.setForeground(Color.red);
                l.setFont(new Font("Serif", Font.BOLD, 30));

            } else {
                JOptionPane.showMessageDialog(null, "Incorrect email-Id or password..Try Again with correct detail");
            }
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }

    public static void main(String arr[]) {
        new Login();
    }
} 

最佳答案

我不相信你真的想这样做(尽管我可能是错的)。如果这是一个登录窗口,并且您使用 System.exit(...) 终止 JVM,那么整个程序将结束,包括调用此登录窗口的代码。您百分百确定这就是您想要做的吗?

相反,我认为您希望处置此窗口(它可能应该是 JDialog 而不是 JFrame)。 ActionListener 中的类似代码可以工作:

public void actionPerformed(ActionEvent evt) {
  // get the button that was pressed
  AbstractButton src = (AbstractButton) evt.getSource();

  // get the top-level Window that holds the button
  Window window = SwingUtilities.getWindowAncestor(src);

  // dispose of this Window
  window.dispose();
}

请注意,如果窗口是 JFrame,并且它的 defaultCloseOperation 设置为 JFrame.EXIT_ON_CLOSE,那么这也会终止 JVM。如果 JButton 由 JDailog 持有,则处置 JDialog 不应杀死 JVM,而只是关闭 JDialog 并释放其资源。

<小时/>

编辑
我同意其他人的观点,即您所写的问题有点不清楚,并且您的代码由于其格式而难以阅读。

其他建议:

  • 避免空布局,因为它们会导致管理、调试和改进 GUI 变得非常困难。
  • 而是嵌套组件,每个组件都使用自己的布局管理器。
  • 如果您使用布局管理器,请在添加组件后在顶级 GUI 上调用 pack()
  • 在添加组件之后之前(通常在调用 pack() 之前),不要在顶级 GUI 上调用 setVisible(true) )。
  • 阅读Swing Tutorials了解有关如何在 Swing 中编码的详细信息。

关于java - 需要帮助添加使用 system.exit(0) 的 JButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25127484/

相关文章:

java - 如何避免在 Swing 中耦合 View 和 Controller

java - 输入按钮抛出空指针异常

java - JButton 事件监听器故障

java - Intellij 2019重新编译整个类问题

java - 当我向 Jpanel 添加按钮时 KeyListener 不起作用

java - 我的 Java Swing JPanel 居中功能不起作用。为什么?

java - Jpanel点击创建事件

java - 如何在没有 ArrayIndexOutOfBoundsException 的情况下通过 executeBatch 获取生成的 key ?

java - 在用户创建所述数字后,如何创建要填充的空变量?

java - 使用 Apache POI 评估 Excel 公式