swing在表单的文本字段中执行操作

swing在表单的文本字段中执行操作

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

问题描述

我想开发一个带有一些文本字段的表单。

例如:


的想法是,每个文本字段内都有一个文本内容:

第一个写你的名字的文本字段,文本插入你的名字必须被删除......第二个文本字段(SecondName)必须发生同样的情况。

效果必须是这个:



我认为我只需要在文本字段上需要一个Action,当用户在文本字段上按下鼠标时就会醒来,这可能吗?

谢谢

解决方案



例如





当字段具有焦点时,提示将被隐藏,但您可以控制此显示,直到用户输入内容或获得焦点时突出显示。

  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){
PromptSupportTest();

$ b $ public PromptSupportTest(){
EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
尝试{
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图片= 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);
//添加动作侦听器到brose按钮来显示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);
}

}

}

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


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

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

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 - swing在表单的文本字段中执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:20