2

I want to create a onscreen keyboard such that when a text field gets focused the keyboard appears on the screen and when the focus is lost or if one clicks outside the screen the keyboard should disappear.

This is not the problem, the problem is that i'm not sure what I should use to create such a keyboard. I cannot use a jFrame because if I click outside the keyboard window then the keyboard goes to the background and is not closed. I also can't use jDialog because it do not allow us to click outside the window.

また、キーボードで入力した内容をリアルタイムでテキストボックスに表示できるようにしたいと思います(画面上のキーボードのキーをクリックすると、テキストボックスに表示されるはずです)。私はこのプログラムを netbeans で実行しているので、覚えておいていただけると助かります。

4

1 に答える 1

2

I am not sure I understand the requirement here, but see if this example gives you some ideas.

The basic thrust of it is to ensure there is 'white space' (OK RED/ORANGE in this example, but let's not quibble over shades of gray) around the components that can become focusable. Add a mouse listener to it, and on event, request the focus (or in your case, hide the keyboard).

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class ComponentFocus {

    ComponentFocus() {
        final JPanel gui = new JPanel(new GridLayout(0,1,15,15));
        gui.setFocusable(true);
        gui.addMouseListener(new MouseAdapter(){
            @Override 
            public void mouseClicked(MouseEvent me) {
                System.out.println(me);
                gui.requestFocus(true);
            }
        });
        gui.setBackground(Color.RED);
        gui.addFocusListener(new FocusAdapter(){
            @Override
            public void focusGained(FocusEvent fe) {
                gui.setBackground(Color.ORANGE);
            }

            @Override
            public void focusLost(FocusEvent fe) {
                gui.setBackground(Color.RED);
            }
        });

        JButton button1 = new JButton("button1");
        gui.add(button1);
        JButton button2 = new JButton("button2");
        gui.add(button2);

        JOptionPane.showMessageDialog(null, gui);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ComponentFocus();
            }
        });
    }
}
于 2012-06-14T17:15:43.103 に答える