使用swing如何调暗未聚焦的文本字段?

时间:2016-01-11 21:34:41

标签: java swing user-interface

允许用户仅从少数选项中选择一个选项。每个选项都由一个文本字段表示(用户需要在那里放置一些文本)所以我虽然应该很好地调整一个没有重点的字段让用户理解他可以选择一个选项。

enter image description here

任何人都可以通过示例/链接告诉我一个相似的东西吗?

1 个答案:

答案 0 :(得分:4)

因此,基本的想法是简单地使用setEnabled来更改您不想修改的字段的启用状态。困难的部分是知道何时禁用/启用字段

幸运的是,JTextField' Document可以在更新时生成事件。有关详细信息,请参阅Listening for Changes on a Document

所以,有了这个,我们可以做这样的事情......

public class ManagedDocumentHandler implements DocumentListener {

    private JTextField toBe;
    private JTextField orNotToBe;

    public ManagedDocumentHandler(JTextField toBe, JTextField orNotToBe) {
        this.toBe = toBe;
        this.orNotToBe = orNotToBe;
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        updateState();
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        updateState();
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        updateState();
    }

    protected void updateState() {
        toBe.setEnabled(true);
        orNotToBe.setEnabled(toBe.getText().trim().length() == 0);
    }

}

知道我们有两个字段,一个将在内容更改时启用,另一个将(可能)在另一个更改时被禁用(除非字段为空,然后它们都被启用)

YouShellNotPass

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class MakeItSo {

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

    public MakeItSo() {
        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() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1;
            gbc.insets = new Insets(4, 4, 4, 4);

            add(new JLabel("Choose this:"), gbc);
            gbc.gridx++;
            add(new JLabel("Or this:"), gbc);

            JTextField thisOne = new JTextField(10);
            JTextField orThisOne = new JTextField(10);

            thisOne.getDocument().addDocumentListener(new ManagedDocumentHandler(thisOne, orThisOne));
            orThisOne.getDocument().addDocumentListener(new ManagedDocumentHandler(orThisOne, thisOne));

            gbc.gridx = 0;
            gbc.gridy++;

            add(thisOne, gbc);
            gbc.gridx++;
            add(orThisOne, gbc);
        }

        public class ManagedDocumentHandler implements DocumentListener {

            private JTextField toBe;
            private JTextField orNotToBe;

            public ManagedDocumentHandler(JTextField toBe, JTextField orNotToBe) {
                this.toBe = toBe;
                this.orNotToBe = orNotToBe;
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                updateState();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                updateState();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                updateState();
            }

            protected void updateState() {
                toBe.setEnabled(true);
                orNotToBe.setEnabled(toBe.getText().trim().length() == 0);
            }

        }

    }

}