【发布时间】: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