【问题标题】:JButton not adding JLabel with an ActionListenerJButton 不使用 ActionListener 添加 JLabel
【发布时间】:2015-07-07 02:13:28
【问题描述】:

我已经开始对我已经工作了几个月的项目进行扩展,我觉得有必要将其从控制台中取出并放入 GUI 窗口中。到目前为止一切都很好!除了一件事,当我尝试测试登录按钮时(这只是为了限制谁使用它,并看看我是否可以这样做。)动作侦听器没有像我希望的那样响应。代码如下:

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JLayeredPane;


public class Main {

private JFrame frame;
private JTextField textField;
private JTextField textField_1;

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

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

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.getContentPane().setForeground(Color.GREEN);
    frame.setBounds(100, 100, 612, 389);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JLabel lblUserName = new JLabel("User name");
    lblUserName.setBackground(Color.YELLOW);
    lblUserName.setBounds(158, 70, 67, 25);
    frame.getContentPane().add(lblUserName);

    JLabel lblPassword = new JLabel("Password");
    lblPassword.setBounds(158, 146, 53, 14);
    frame.getContentPane().add(lblPassword);

    textField = new JTextField();
    textField.setBounds(235, 143, 118, 20);
    frame.getContentPane().add(textField);
    textField.setColumns(10);

    textField_1 = new JTextField();
    textField_1.setColumns(10);
    textField_1.setBounds(235, 72, 118, 20);
    frame.getContentPane().add(textField_1);

    JButton btnLogin = new JButton("Login");
    btnLogin.setBounds(151, 228, 256, 61);
    frame.getContentPane().add(btnLogin);
    btnLogin.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            if(textField.equals("Admin"))
            {
                if(textField.equals("Admin"))
                {
                    JLabel lblLoginSuccessPlease = new JLabel("LOGIN SUCCESS! Please wait while the other functions are loaded");
                    lblLoginSuccessPlease.setBounds(124, 204, 344, 14);
                    frame.getContentPane().add(lblLoginSuccessPlease);

                }
            }

        }
    });      

    JLabel lblWelcomeToMy = new JLabel("Welcome to my amazing program!");
    lblWelcomeToMy.setBounds(174, 11, 242, 14);
    frame.getContentPane().add(lblWelcomeToMy);
    }
}

我尝试使用 && 来测试用户名和密码框,但它也不起作用。另外,如果有人可以指导我如何使密码框隐藏字符,那将非常有帮助。

【问题讨论】:

  • 我把这个作为一个编程问题放在我的脑海中......所以我发布到编程堆栈交换。
  • 这不是“编程”,而是“程序员”。实施问题不在此处讨论。
  • 啊,那我很抱歉。也许,不,我会在堆栈溢出被迁移时得到答案。

标签: java user-interface


【解决方案1】:
    if(textField.equals("Admin"))
        {
            if(textField.equals("Admin"))
            {
                JLabel lblLoginSuccessPlease = new JLabel("LOGIN SUCCESS! Please wait while the other functions are loaded");
                lblLoginSuccessPlease.setBounds(124, 204, 344, 14);
                frame.getContentPane().add(lblLoginSuccessPlease);

            }
        }

首先,不需要检查 textField 是否等于“Admin”两次。

但主要问题是文本字段(在 UI 中接受输入的白色矩形)永远等于字符串,即字符序列。

您要测试的是在文本字段中输入的文本是否等于“Admin”:

if (textField.getText().equals("Admin"))

您的代码还有其他问题:

  • 使用绝对坐标而不是使用layout manager
  • 动态地将元素添加到框架中,而不是从一开始就添加它们并在需要时简单地使它们可见。需要添加,但随后需要重新验证 GUI。

【讨论】:

  • 谢谢!另外,我想我一定忘记了要检查的 textField_1,因为我确实知道如何从该框中获取文本然后检查。这应该让我更容易。此外,使用布局管理器,它限制了我让我的按钮像我喜欢的那样在 2 或 3 列列表中对齐,每个列表之间都有一点空间。我会尝试重新制作一段时间(与经理合二为一),但我会完成它,因为我有几个朋友渴望测试它。 :) 再次感谢。
【解决方案2】:
  1. frame 在Main 类中具有私有访问权限

    private JFrame frame;
    

    将其更改为公开或删除修饰符

  2. 要获取 JTextfield 的文本,请使用textfield.getText()

      //if (textField.equals("Admin")) {wrong
            if (textField.getText().equals("Admin")) {//right way
    
  3. 您必须再次检查密码而不是用户名

      if (textField_1.getText().equals("Admin")) {
              f (textField.getText().equals("Admin")) {//this is the password field
    
  4. 要屏蔽密码,您必须使用JPasswordField.so create like

    private JPasswordField textField;
    

    并像初始化一样

    textField = new JPasswordField();
    

    然后检查

    if (textField_1.getText().equals("Admin")) {
                if (String.valueOf(textField.getPassword()).equals("Admin")) {
                    JLabel lblLoginSuccessPlease = new JLabel("LOGIN SUCCESS! Please wait while the other functions are loaded");
                    lblLoginSuccessPlease.setBounds(124, 204, 344, 14);
                    frame.getContentPane().add(lblLoginSuccessPlease);
    
                }
            }
    

【讨论】:

  • 谢谢,我正在使用我的窗口生成器,并在我发布它之后就注意到了,并且因为没有注意而对自己生气
猜你喜欢
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
  • 2019-05-14
  • 2013-10-04
  • 1970-01-01
  • 1970-01-01
  • 2015-07-11
  • 2013-10-06
相关资源
最近更新 更多