为 JFrame 实现清除按钮

发布于 2025-01-06 16:42:19 字数 218 浏览 0 评论 0原文

我正在为由四个 JPanel 组成的 JFrame 实现一个清除按钮。每个 JPanel 都有多个文本字段、单选按钮和复选框。

当表单加载时,应禁用“清除”按钮。仅当用户在任何面板中的任何字段中输入某些值时才应启用它。

我尝试向面板添加 KeyListener。但它没有正确获取事件。我是否必须为所有 UI 组件注册 KeyListener?还有什么好的方法吗?

提前致谢!

I'm implementing a clear button for a JFrame which consists of four JPanels. Each JPanel has several text fields, radio buttons and checkboxes.

When the forms loads "clear" buttons should be disabled. It should be only enabled when the user entered some value to any of those fields in any panel.

I tried by adding a KeyListener to the panels. But It does not get the events properly. Do I have to register KeyListener for all the UI components? Any other good method?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蘸点软妹酱 2025-01-13 16:42:19

要进行更改,您可以将 ItemListener 添加到 JCheckBoxJRadioButtons,对于 JTextField,您可以添加 CaretListener

这个小程序可能会帮助你:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.CaretListener;
import javax.swing.event.CaretEvent;

public class StateChangedEventClass extends JFrame
{
    private JPanel contentPane, panel1, panel2;
    private JButton clearButton;
    private ItemListener itemChangeAction;
    private CaretListener caretAction;

    public StateChangedEventClass()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(2, 2));
        clearButton = new JButton("CLEAR");
        clearButton.setEnabled(false);

        itemChangeAction = new ItemListener()
        {
            public void itemStateChanged(ItemEvent ce)
            {
                clearButton.setEnabled(true);
            }
        };

        caretAction = new CaretListener()
        {
            public void caretUpdate(CaretEvent ce)
            {
                clearButton.setEnabled(true);
            }
        };

        panel1 = new JPanel();
        panel1.setLayout(new GridLayout(2  , 2));
        JLabel userLabel = new JLabel("USERNAME : ", JLabel.CENTER);
        JTextField userField = new JTextField(10);
        userField.addCaretListener(caretAction);
        JLabel passLabel = new JLabel("PASSWORD : " + JLabel.CENTER);
        JTextField passField = new JTextField(10);
        passField.addCaretListener(caretAction);
        panel1.add(userLabel);
        panel1.add(userField);
        panel1.add(passLabel);
        panel1.add(passField);
        contentPane.add(panel1);

        panel2 = new JPanel();
        panel2.setLayout(new GridLayout(2, 1));
        JRadioButton maleButton = new JRadioButton("MALE", false);
        maleButton.addItemListener(itemChangeAction);
        JRadioButton femaleButton = new JRadioButton("FEMALE", false);
        femaleButton.addItemListener(itemChangeAction);
        ButtonGroup bg = new ButtonGroup();
        bg.add(maleButton);
        bg.add(femaleButton);
        panel2.add(maleButton);
        panel2.add(femaleButton);
        contentPane.add(panel2);

        add(contentPane, BorderLayout.CENTER);
        add(clearButton, BorderLayout.PAGE_END);

        pack();
        setVisible(true);
    }

    public void caretUpdate(CaretEvent ce)
    {
        clearButton.setEnabled(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new StateChangedEventClass();
            }
        });
    }
}

For making changes you can add ItemListener to your JCheckBox and JRadioButtons and for JTextField you can add CaretListener.

This small program might help you :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.CaretListener;
import javax.swing.event.CaretEvent;

public class StateChangedEventClass extends JFrame
{
    private JPanel contentPane, panel1, panel2;
    private JButton clearButton;
    private ItemListener itemChangeAction;
    private CaretListener caretAction;

    public StateChangedEventClass()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(2, 2));
        clearButton = new JButton("CLEAR");
        clearButton.setEnabled(false);

        itemChangeAction = new ItemListener()
        {
            public void itemStateChanged(ItemEvent ce)
            {
                clearButton.setEnabled(true);
            }
        };

        caretAction = new CaretListener()
        {
            public void caretUpdate(CaretEvent ce)
            {
                clearButton.setEnabled(true);
            }
        };

        panel1 = new JPanel();
        panel1.setLayout(new GridLayout(2  , 2));
        JLabel userLabel = new JLabel("USERNAME : ", JLabel.CENTER);
        JTextField userField = new JTextField(10);
        userField.addCaretListener(caretAction);
        JLabel passLabel = new JLabel("PASSWORD : " + JLabel.CENTER);
        JTextField passField = new JTextField(10);
        passField.addCaretListener(caretAction);
        panel1.add(userLabel);
        panel1.add(userField);
        panel1.add(passLabel);
        panel1.add(passField);
        contentPane.add(panel1);

        panel2 = new JPanel();
        panel2.setLayout(new GridLayout(2, 1));
        JRadioButton maleButton = new JRadioButton("MALE", false);
        maleButton.addItemListener(itemChangeAction);
        JRadioButton femaleButton = new JRadioButton("FEMALE", false);
        femaleButton.addItemListener(itemChangeAction);
        ButtonGroup bg = new ButtonGroup();
        bg.add(maleButton);
        bg.add(femaleButton);
        panel2.add(maleButton);
        panel2.add(femaleButton);
        contentPane.add(panel2);

        add(contentPane, BorderLayout.CENTER);
        add(clearButton, BorderLayout.PAGE_END);

        pack();
        setVisible(true);
    }

    public void caretUpdate(CaretEvent ce)
    {
        clearButton.setEnabled(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new StateChangedEventClass();
            }
        });
    }
}
向日葵 2025-01-13 16:42:19

KeyListener 未指定用于 监听 Swing GUI 中的 Keyboards 事件,因为 Swing 就在那里按键绑定

KeyListener isn't designated for Listening a Keyboards events in the Swing GUI, for Swing is there KeyBindings

回忆那么伤 2025-01-13 16:42:19

您必须将 ActionListener 添加到 JTextFields,然后检查文本字段中的文本值,例如:

jButton.setEnabled(!jTextField.getText().equals("")); 

You must adding ActionListeners to your JTextFields, after this check text value in text fields, for example:

jButton.setEnabled(!jTextField.getText().equals("")); 
愁杀 2025-01-13 16:42:19

只有当用户向其中任何一个输入某些值时才应启用它
任何面板中的这些字段。

因此,您应该为所有这些字段实现 KeyListener 接口。在检测到用户的打字操作后,您应该启用该按钮。

jButton.setEnabled(true);

我必须为所有 UI 组件注册 KeyListener 吗?

当然不。编写一个扩展 JTextField 并实现 KeyListener 的类。从此类派生您的对象(所有文本字段)。如果您实现了按键时应该执行的操作,那么所有这些对象都会遵守您的规则。

It should be only enabled when the user entered some value to any of
those fields in any panel.

So you should implement KeyListener interface for all these fields. Upon detecting typing action of the user you should enable the button.

jButton.setEnabled(true);

Do I have to register KeyListener for all the UI components?

Of course no. Write a class which extends JTextField and implements KeyListener. Derive your objects (all text fields) from this class. If you implement what you should do upon key strokes, all these objects will obey your rule.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文