Java - 在表单的文本字段中摆动监听动作 [英] Java - swing listen an action in a text field of a form

查看:26
本文介绍了Java - 在表单的文本字段中摆动监听动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个带有一些文本字段的表单.
示例:

<块引用>

名称第二个名字

这个想法是每个文本字段都有一个类似的文本:

<块引用>

插入你的名字插入您的第二个名字

当您单击第一个文本字段以输入您的姓名时,必须删除文本插入您的姓名"...第二个文本字段 (SecondName) 也必须如此.
效果必须是这样的:

我认为我只需要在文本字段上执行一个操作,当用户在文本字段上按下鼠标时必须唤醒该操作,这可能吗?
谢谢

解决方案

查看 SwingLabs SwingX 库

例如

当字段获得焦点时,提示"将被隐藏,但您可以控制它,使其显示直到用户输入某些内容或在获得焦点时突出显示.

import java.awt.Dimension;导入 java.awt.EventQueue;导入 java.awt.GridBagConstraints;导入 java.awt.GridBagLayout;导入 java.awt.Insets;导入 javax.swing.JButton;导入 javax.swing.JFrame;导入 javax.swing.JPanel;导入 javax.swing.JTextField;导入 javax.swing.UIManager;导入 javax.swing.UnsupportedLookAndFeelException;导入 org.jdesktop.swingx.prompt.BuddySupport;导入 org.jdesktop.swingx.prompt.PromptSupport;公共类 PromptSupportTest {公共静态无效主(字符串 [] args){新的 PromptSupportTest();}公共提示支持测试(){EventQueue.invokeLater(new Runnable() {@覆盖公共无效运行(){尝试 {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {ex.printStackTrace();}JFrame frame = new JFrame("测试");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.add(new TestPane());框架.pack();frame.setLocationRelativeTo(null);frame.setVisible(true);}});}公共类 TestPane 扩展 JPanel {公共测试窗格(){JTextField firstName = new JTextField(10);PromptSupport.setPrompt("名字", firstName);PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIDE_PROMPT, firstName);JTextField lastName = new JTextField(10);PromptSupport.setPrompt("Last Name", lastName);PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIDE_PROMPT, lastName);JTextField 图片 = new JTextField(10);PromptSupport.setPrompt("图片", 图片);PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIDE_PROMPT, 图片);JButton browse = new JButton("...");browse.setMargin(new Insets(0, 0, 0, 0));浏览.setContentAreaFilled(false);浏览.setFocusPainted(false);浏览.setFocusable(false);浏览.setOpaque(false);//将动作监听器添加到 brose 按钮​​以显示 JFileChooser...BuddySupport.addRight(浏览,图片);setLayout(new GridBagLayout());GridBagConstraints gbc = 新的 GridBagConstraints();gbc.gridwidth = GridBagConstraints.REMAINDER;gbc.weightx = 1;添加(名字,gbc);添加(姓氏,gbc);添加(图片,gbc);gbc.anchor = GridBagConstraints.CENTER;添加(新 JButton(确定"),gbc);}@覆盖公共维度 getPreferredSize() {返回新维度(200, 200);}}}

我还添加了一个 BuddySupport 示例,它是同一 API 的一部分,它允许您与文本组件伙伴"另一个组件.这里我做了经典的文件浏览器"组合,但我一直在做这样的搜索"样式字段......

I would like to develop a form with some text field.
example:

Name
SecondName

the idea is that every text field have inside a text like:

Insert your name
Insert your second name

when you click on the first text field to write your name, the text "Insert your name" have to be deleted... the same have to happen for the second text field (SecondName).
The effect have to be this:

I think that i just need an Action on the text field that have to wake up when the user press on the mouse on the text field, it's possible?
Thank you

解决方案

Take a look at PromptSupport in SwingLabs SwingX Library

For Example

When the fields have focus, the "prompt" will be hidden, but you can control this, making it shown until the user types something or highlight when focus is gained.

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.jdesktop.swingx.prompt.BuddySupport;
import org.jdesktop.swingx.prompt.PromptSupport;

public class PromptSupportTest {

    public static void main(String[] args) {
        new PromptSupportTest();
    }

    public PromptSupportTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            JTextField firstName = new JTextField(10);
            PromptSupport.setPrompt("First Name", firstName);
            PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIDE_PROMPT, firstName);

            JTextField lastName = new JTextField(10);
            PromptSupport.setPrompt("Last Name", lastName);
            PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIDE_PROMPT, lastName);

            JTextField picture = new JTextField(10);
            PromptSupport.setPrompt("Picture", picture);
            PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIDE_PROMPT, picture);

            JButton browse = new JButton("...");
            browse.setMargin(new Insets(0, 0, 0, 0));
            browse.setContentAreaFilled(false);
            browse.setFocusPainted(false);
            browse.setFocusable(false);
            browse.setOpaque(false);
            // Add action listener to brose button to show JFileChooser...

            BuddySupport.addRight(browse, picture);

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weightx = 1;

            add(firstName, gbc);
            add(lastName, gbc);
            add(picture, gbc);

            gbc.anchor = GridBagConstraints.CENTER;
            add(new JButton("Ok"), gbc);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}

I've also added an example of BuddySupport which is part of the same API, which allows you to "buddy" another component with a text component. Here I've done the classic "file browser" combination, but I do "search" style fields like this all the time...

这篇关于Java - 在表单的文本字段中摆动监听动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆