我将如何禁用文本框? [英] How would i disable a text box?
问题描述
所以我正在做一个项目,我试图从程序中禁用框架和字段,以便只有窗口在前面是这个窗口处于活动状态这是我的代码:
SO i am working on a project and i was trying to disable the frame and the field from the program so that only the window is front is this one being active here is my code :
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class password
{
private static String password = "pass";
public static void main(String[]args) {
JFrame frame = new JFrame("Password");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,100);
JLabel label = new JLabel("Enter password");
JPanel panel = new JPanel();
frame.add(panel);
JPasswordField pass = new JPasswordField(10);
pass.setEchoChar('*');
pass.addActionListener(new AL());
panel.add(label, BorderLayout.WEST);
panel.add(pass, BorderLayout.WEST);
}
static class AL implements ActionListener
{
public void actionPerformed(ActionEvent e) {
JPasswordField input = (JPasswordField) e.getSource();
char [] passy = input.getPassword();
String p = new String(passy);
if (p.equals(password)){
JOptionPane.showMessageDialog(null, "Correct");
System.out.print("Welcome to Adam's Quirky program.");
}
else
JOptionPane.showMessageDialog(null, "Incorrect");
}
}
}
我目前用来编程的程序是 Eclipse.
the program i am currently using to program is Eclipse.
推荐答案
我想看看模态对话框.
要么使用 JOptionPane
,要么使用你自己的.
Either use a JOptionPane
or roll your own.
如果这些不适合您的需求,请尝试查看 JLayer (Java 7) 或 JXLayer(Java 6 及以下)
If these don't suit your needs, try taking a look at JLayer (Java 7) or JXLayer (Java 6 and below)
看看 LockableUI(这是有点过时,但基本思路是一样的)
Have a look at LockableUI (It's a little out of date, but the basic idea is the same)
如果这不吸引人,您可以使用阻挡玻璃板"
If that doesn't appeal, you could use a "blocking glass pane"
看看
举个例子
这篇关于我将如何禁用文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!