How would i disable a text box?

蹲街弑〆低调 提交于 2019-12-24 16:41:40

问题


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");
       }
    }
 }

the program i am currently using to program is Eclipse.


回答1:


I'd take a look at a modal dialog.

Either use a JOptionPane or roll your own.

If these don't suit your needs, try taking a look at JLayer (Java 7) or JXLayer (Java 6 and below)

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"

Take a look at

  • glassPane is not blocking input
  • Is there a problem with this blocking GlassPane?

as some examples



来源:https://stackoverflow.com/questions/12169662/how-would-i-disable-a-text-box

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!