Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-关于JTextField和JPasswordField的问题_Java_Swing_Jtextfield_Jpasswordfield - Fatal编程技术网

Java-关于JTextField和JPasswordField的问题

Java-关于JTextField和JPasswordField的问题,java,swing,jtextfield,jpasswordfield,Java,Swing,Jtextfield,Jpasswordfield,这是我的代码(JTextField带输入提示)。有人能帮我改进代码吗 1) 当我单击文本字段时,我不希望提示消失。我希望只有在文本字段中键入内容时提示才会消失 2) 如何使用输入提示编码JPasswordField 如何创建类似Facebook移动登录页面的JTextField和JPasswordField(roundJTextField/PasswordField并粘在一起)?我不是一个狂热的粉丝,但我认为你的第一个问题在于这种方法: public class Hints { publ

这是我的代码(
JTextField
带输入提示)。有人能帮我改进代码吗

1) 当我单击文本字段时,我不希望提示消失。我希望只有在文本字段中键入内容时提示才会消失

2) 如何使用输入提示编码
JPasswordField


如何创建类似Facebook移动登录页面的JTextField和JPasswordField(round
JTextField
/
PasswordField
并粘在一起)?

我不是一个狂热的粉丝,但我认为你的第一个问题在于这种方法:

public class Hints
{
    public static void main (String [] args)
    {
        Box mainPanel = Box.createVerticalBox ();
        mainPanel.setBackground (Color.LIGHT_GRAY);
        mainPanel.setOpaque (true);
        mainPanel.add (new HintedTextField (12, "Login"));
        mainPanel.add (Box.createVerticalStrut (1));
        mainPanel.add (new HintedPasswordField (12, "Password"));

        JPanel panel = new JPanel (new BorderLayout ());
        panel.add (mainPanel, BorderLayout.CENTER);
        panel.setBorder (BorderFactory.createEmptyBorder (8, 8, 8, 8));

        JFrame frame = new JFrame ();
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane ().setLayout (new BorderLayout ());
        frame.getContentPane ().add (panel, BorderLayout.CENTER);
        frame.pack ();
        frame.setVisible (true);
    }

    private static class RoundedRectableBorder implements Border
    {
        private final boolean top;
        private final boolean bottom;

        public RoundedRectableBorder (boolean top, boolean bottom)
        {
            this.top = top;
            this.bottom = bottom;
        }

        @Override
        public void paintBorder (Component c, Graphics g, int x, int y,
                int width, int height)
        {
            Area clipArea = new Area (new Rectangle (x, y, width, height));
            clipArea.subtract (
                new Area (
                    new Rectangle (
                        x + 5, top ? y + 5 : y, width - 10, height - (top ? 5 : 0) - (bottom ? 5 : 0))));

            g.setClip (clipArea);

            g.setColor (c.getParent ().getParent ().getBackground ());
            g.fillRect (x, y, width, height);
            g.setColor (c.getBackground ());
            g.fillRoundRect (x, top ? y : (y - 5), width - 1, height + (top ? 5 : 0) + (bottom ? 5 : 0) - 1, 10, 10);
            g.setColor (c.getParent ().getBackground ());
            g.drawRoundRect (x, top ? y : (y - 5), width - 1, height + (top ? 5 : 0) + (bottom ? 5 : 0) - 1, 10, 10);
        }

        @Override
        public Insets getBorderInsets (Component c)
        {
            return new Insets (5, 5, 5, 5);
        }

        @Override
        public boolean isBorderOpaque ()
        {
            return false;
        }
    }

    private static class HintedTextField extends JTextField
    {
        private final JTextField hintField;

        public HintedTextField (int columns, String hint)
        {
            super (columns);

            setBorder (new RoundedRectableBorder (true, false));

            hintField = new JTextField (hint);
            hintField.setBorder (BorderFactory.createEmptyBorder (5, 5, 5, 5));
        }

        @Override
        protected void paintComponent (Graphics g)
        {
            super.paintComponent (g);

            if (getText ().isEmpty ())
            {
                hintField.setBounds (getBounds ());
                hintField.setForeground (getDisabledTextColor());
                hintField.setOpaque (false);
                hintField.paint (g);
            }
        }
    }

    private static class HintedPasswordField extends JPasswordField
    {
        private final JTextField hintField;

        public HintedPasswordField (int columns, String hint)
        {
            super (columns);

            setBorder (new RoundedRectableBorder (false, true));

            hintField = new JTextField (hint);
            hintField.setBorder (BorderFactory.createEmptyBorder (5, 5, 5, 5));
        }

        @Override
        protected void paintComponent (Graphics g)
        {
            super.paintComponent (g);

            if (getPassword ().length == 0)
            {
                hintField.setBounds (getBounds ());
                hintField.setForeground (getDisabledTextColor());
                hintField.setOpaque (false);
                hintField.paint (g);
            }
        }
    }
}
要做到这一点,您需要创建一个线程来控制文本字段中包含的文本:

public void focusGained(FocusEvent e)
{
    if(this.getText().length() == 0)
    {
        super.setText("");
    }
}

我不是一个秋千迷,但我认为你的第一个问题在于这种方法:

public class Hints
{
    public static void main (String [] args)
    {
        Box mainPanel = Box.createVerticalBox ();
        mainPanel.setBackground (Color.LIGHT_GRAY);
        mainPanel.setOpaque (true);
        mainPanel.add (new HintedTextField (12, "Login"));
        mainPanel.add (Box.createVerticalStrut (1));
        mainPanel.add (new HintedPasswordField (12, "Password"));

        JPanel panel = new JPanel (new BorderLayout ());
        panel.add (mainPanel, BorderLayout.CENTER);
        panel.setBorder (BorderFactory.createEmptyBorder (8, 8, 8, 8));

        JFrame frame = new JFrame ();
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane ().setLayout (new BorderLayout ());
        frame.getContentPane ().add (panel, BorderLayout.CENTER);
        frame.pack ();
        frame.setVisible (true);
    }

    private static class RoundedRectableBorder implements Border
    {
        private final boolean top;
        private final boolean bottom;

        public RoundedRectableBorder (boolean top, boolean bottom)
        {
            this.top = top;
            this.bottom = bottom;
        }

        @Override
        public void paintBorder (Component c, Graphics g, int x, int y,
                int width, int height)
        {
            Area clipArea = new Area (new Rectangle (x, y, width, height));
            clipArea.subtract (
                new Area (
                    new Rectangle (
                        x + 5, top ? y + 5 : y, width - 10, height - (top ? 5 : 0) - (bottom ? 5 : 0))));

            g.setClip (clipArea);

            g.setColor (c.getParent ().getParent ().getBackground ());
            g.fillRect (x, y, width, height);
            g.setColor (c.getBackground ());
            g.fillRoundRect (x, top ? y : (y - 5), width - 1, height + (top ? 5 : 0) + (bottom ? 5 : 0) - 1, 10, 10);
            g.setColor (c.getParent ().getBackground ());
            g.drawRoundRect (x, top ? y : (y - 5), width - 1, height + (top ? 5 : 0) + (bottom ? 5 : 0) - 1, 10, 10);
        }

        @Override
        public Insets getBorderInsets (Component c)
        {
            return new Insets (5, 5, 5, 5);
        }

        @Override
        public boolean isBorderOpaque ()
        {
            return false;
        }
    }

    private static class HintedTextField extends JTextField
    {
        private final JTextField hintField;

        public HintedTextField (int columns, String hint)
        {
            super (columns);

            setBorder (new RoundedRectableBorder (true, false));

            hintField = new JTextField (hint);
            hintField.setBorder (BorderFactory.createEmptyBorder (5, 5, 5, 5));
        }

        @Override
        protected void paintComponent (Graphics g)
        {
            super.paintComponent (g);

            if (getText ().isEmpty ())
            {
                hintField.setBounds (getBounds ());
                hintField.setForeground (getDisabledTextColor());
                hintField.setOpaque (false);
                hintField.paint (g);
            }
        }
    }

    private static class HintedPasswordField extends JPasswordField
    {
        private final JTextField hintField;

        public HintedPasswordField (int columns, String hint)
        {
            super (columns);

            setBorder (new RoundedRectableBorder (false, true));

            hintField = new JTextField (hint);
            hintField.setBorder (BorderFactory.createEmptyBorder (5, 5, 5, 5));
        }

        @Override
        protected void paintComponent (Graphics g)
        {
            super.paintComponent (g);

            if (getPassword ().length == 0)
            {
                hintField.setBounds (getBounds ());
                hintField.setForeground (getDisabledTextColor());
                hintField.setOpaque (false);
                hintField.paint (g);
            }
        }
    }
}
要做到这一点,您需要创建一个线程来控制文本字段中包含的文本:

public void focusGained(FocusEvent e)
{
    if(this.getText().length() == 0)
    {
        super.setText("");
    }
}

为了在键入文本时消除提示,您应该实现Keylistener。在它的按键实现中,您可以编写

new Thread(){
    public void run(){
        while(true){
          String s = this.getText();
          if(s.length == 0 || s.equals(hint))
              super.setText(hint);
          else //significa che è cambiato il testo
              if(s.contains(hint))
                 super.setText(s.replace(hint, ""));
        }
    }
}.start();

还要注释掉focusgound()实现。

为了在键入文本时消除提示,您应该实现Keylistener。在它的按键实现中,您可以编写

new Thread(){
    public void run(){
        while(true){
          String s = this.getText();
          if(s.length == 0 || s.equals(hint))
              super.setText(hint);
          else //significa che è cambiato il testo
              if(s.contains(hint))
                 super.setText(s.replace(hint, ""));
        }
    }
}.start();
还要注释掉focusgound()实现

当我单击文本字段时,我不希望提示消失。我希望只有在文本字段中键入内容时提示才会消失

提供此功能

我以前没有在JPasswordField上尝试过,但它也应该在那里工作

当我单击文本字段时,我不希望提示消失。我希望只有在文本字段中键入内容时提示才会消失

提供此功能


我以前没有用JPasswordField尝试过,但它也应该在那里工作。

直接绘制提示,而不是为JTextField设置文本。请参见本例中的
paintComponent()
方法:

@Override
public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    if(hideOnFocus) repaint();
}

与其为JTextField设置文本,不如直接绘制提示。请参见本例中的
paintComponent()
方法:

@Override
public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    if(hideOnFocus) repaint();
}

我认为一些测试代码会有帮助。像“Hello world”这样的例子怎么样?这段代码还不能运行。请至少添加一个简单的
main()。。。。因此,代码是HintTextField textfield=new HintTextField();setHint(“这是一个提示”);我认为一些测试代码会有帮助。像“Hello world”这样的例子怎么样?这段代码还不能运行。请至少添加一个简单的
main()。。。。因此,代码是HintTextField textfield=new HintTextField();setHint(“这是一个提示”);谢谢你帮助我。像facebook手机登录页面那样的“圆棒”JTextField和JPasswordField怎么样?@JjCheong添加了圆棒支持。谢谢你的帮助。像facebook手机登录页面一样的“圆棒”JTextField和JPasswordField怎么样?@JjCheong增加了圆棒支持。