如何使用按钮

时间:2015-07-06 21:58:30

标签: java swing user-interface

我对我的Java GUI程序有疑问,所以就是这样。

我今天已经在这个项目上工作了几个小时,我有整个"登录"窗口设置,但有一件事似乎并不适合我。我在"登录"下方有一个标签。表单,如果我输入密码或用户名错误的字符串值不正确,它就不会更新。这是代码:

package main;

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;
import javax.swing.JFormattedTextField;
import javax.swing.SwingConstants;

import java.awt.Window.Type;

import javax.swing.JPasswordField;


public class Login {

private JFrame frmConsoleappReborn;
private JTextField textField_1;
private JPasswordField passwordField;

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

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

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frmConsoleappReborn = new JFrame();
    frmConsoleappReborn.setType(Type.UTILITY);
    frmConsoleappReborn.getContentPane().setBackground(new Color(0, 0, 0));
    frmConsoleappReborn.setTitle("ConsoleApp Reborn");
    frmConsoleappReborn.setBackground(new Color(0, 0, 255));
    frmConsoleappReborn.getContentPane().setForeground(Color.GREEN);
    frmConsoleappReborn.setBounds(100, 100, 565, 369);
    frmConsoleappReborn.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmConsoleappReborn.getContentPane().setLayout(null);



    JLabel lblWelcomeToMy = new JLabel("Welcome to my amazing program!");
    lblWelcomeToMy.setForeground(new Color(255, 0, 0));
    lblWelcomeToMy.setBounds(174, 11, 242, 14);
    frmConsoleappReborn.getContentPane().add(lblWelcomeToMy);

    JLabel lblUserName = new JLabel("User name");
    lblUserName.setForeground(new Color(255, 0, 0));
    lblUserName.setBackground(new Color(255, 255, 255));
    lblUserName.setBounds(158, 70, 67, 25);
    frmConsoleappReborn.getContentPane().add(lblUserName);

    textField_1 = new JTextField();
    textField_1.setForeground(new Color(0, 0, 0));
    textField_1.setBackground(new Color(255, 0, 0));
    textField_1.setColumns(10);
    textField_1.setBounds(235, 72, 118, 20);
    frmConsoleappReborn.getContentPane().add(textField_1);

    JLabel lblPassword = new JLabel("Password");
    lblPassword.setForeground(new Color(255, 0, 0));
    lblPassword.setBounds(158, 146, 76, 14);
    frmConsoleappReborn.getContentPane().add(lblPassword);

    passwordField = new JPasswordField();
    passwordField.setBackground(Color.RED);
    passwordField.setBounds(235, 143, 118, 20);
    frmConsoleappReborn.getContentPane().add(passwordField);

    JLabel loginStatus = new JLabel("Your login status will show up here.");
    loginStatus.setForeground(Color.WHITE);
    loginStatus.setBounds(184, 195, 219, 14);
    frmConsoleappReborn.getContentPane().add(loginStatus);

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

        @SuppressWarnings("deprecation")
        public void actionPerformed(ActionEvent arg0) {
        if(textField_1.getText().equals("Admin"))
        {
            if(passwordField.getText().equals("Admin"))
            {
            loginStatus.setText("Login successful!");
            } else {
                loginStatus.setText("Login failed. Please check password.");
            }
        } else {
            loginStatus.setText("Login failed. Please check username");
        }
        }
    });

}
}

一切顺利,

Groax

0 个答案:

没有答案