4

我的对话框中有一个按钮。在该对话框中按回车键后,分配给该按钮的操作将被一一调用。

public void buttonAction1() { }
....
public void buttonAction2() { }

在我的对话框中,我还有一个文本字段,用于监听焦点丢失事件。每当最初焦点在该文本字段内时,我单击选项卡或使用鼠标单击按钮,调用焦点丢失事件并打开一个弹出窗口。此弹出窗口有助于在文本字段中设置值。此值将由按下按钮时调用的操作使用。

public void focusLostAction() { }

现在的问题是,当焦点在文本字段内并且我按下回车键时,焦点丢失事件不会被调用。所以弹出窗口没有打开,并且没有在该文本字段内设置正确的值。但是由于输入键事件,该特定按钮上的操作被调用,并且该操作无法在文本字段中找到正确的值。

在输入键事件调用的操作方法中,我尝试使用以下方法手动设置焦点:

public void buttonAction1() {
    button.requestFocusInWindow();
}

public void buttonAction2() {
    // do the remaining task
} 

我也尝试使用 button.requestFocus;

我期待在该按钮上手动设置焦点会从文本字段调用松散焦点,并且可能会调用焦点丢失事件(因为这是在单独的 Swing 工作线程中实现的)。但它不起作用。请让我知道,如果您之前遇到过这个问题以及相同的解决方案。谢谢。

4

2 回答 2

4

Here second JTextField is acting the way you want it to. Try your hands on this code example :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class TextFieldExample
{
    private JTextField tfield1;
    private JTextField tfield2;
    private JLabel label;
    private JButton button;
    private FocusListener tfieldListener = new FocusListener()
    {
        @Override
        public void focusGained(FocusEvent fe)
        {
        }

        @Override
        public void focusLost(FocusEvent fe)
        {
            System.out.println("I am LOST");
            String num1 = tfield1.getText().trim();
            String num2 = tfield2.getText().trim();
            if (num1 == null || num1.equals(""))
                num1 = "0";
            if (num2 == null || num2.equals(""))
                num2 = "0";         
            label.setText(Integer.toString(Integer.parseInt(num1) + Integer.parseInt(num2)));
        }
    };

    private void displayGUI()
    {
        JFrame frame = new JFrame("Text Field Focus Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));

        tfield1 = new JTextField(10);
        tfield2 = new JTextField(10);
        tfield2.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                button.requestFocusInWindow();
            }
        });

        tfield1.addFocusListener(tfieldListener);
        tfield2.addFocusListener(tfieldListener);

        ((AbstractDocument)tfield1.getDocument()).setDocumentFilter(new MyDocumentFilter());
        ((AbstractDocument)tfield2.getDocument()).setDocumentFilter(new MyDocumentFilter());

        label = new JLabel("SUM IS");

        button = new JButton("USELESS");

        contentPane.add(tfield1);
        contentPane.add(tfield2);
        contentPane.add(label);
        contentPane.add(button);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    class MyDocumentFilter extends DocumentFilter
    {
        @Override
        public void insertString(FilterBypass fb, int offset
                                                , String text
                                                , AttributeSet aset)
        {
            try
            {
                super.insertString(fb, offset, text.replaceAll("\\D++", ""), aset);
            }
            catch(BadLocationException ble)
            {
                ble.printStackTrace();
            }
        }

        @Override
        public void replace(FilterBypass fb, int offset, int len
                                           , String text
                                           , AttributeSet aset)
        {
            try
            {
                super.replace(fb, offset, len, text.replaceAll("\\D++", ""), aset);
            }
            catch(BadLocationException ble)
            {
                ble.printStackTrace();
            }
        }       
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new TextFieldExample().displayGUI();
            }
        });
    }
}
于 2012-07-17T08:07:26.693 回答
0

gain and lost focus from frame in java Swing Application

public class MainFrame extends Frame {
Frame awe;
public MainFrame(Database database) {
    awe = this;
    this.addWindowFocusListener(new WindowFocusListener() {
        @Override
        public void windowGainedFocus(WindowEvent we) {
            System.out.println("Gain Focus");
        }

        @Override
        public void windowLostFocus(WindowEvent we) {
            System.out.println("Lost Focus");
        }
    });

}

于 2012-12-26T09:50:38.173 回答