如何将Java Swing应用程序实现到触摸屏 [英] How to implement a Java Swing application to Touch Screen

查看:124
本文介绍了如何将Java Swing应用程序实现到触摸屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经建立了销售点系统,现在我们需要将其实施到触摸屏?我们是否需要依次更改任何代码才能使其正常工作。

We have built a Point of Sale system and now we require to implement it to Touch screens? Do we need to change any code in turn to allow this to work.

我们正在使用键盘输入值 - 比方说数量 - 当我专注于JTextField时,是否有java方式弹出键盘(如android) ?

And we are using the Keyboard to enter values - let's say quantity - Is there a java way of popping up a key board (like android) when I focus on a JTextField?

推荐答案

这是一个关于如何实现弹出式键盘的简单示例:

Here is a simple example on how to implement a pop-up keyboard:

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;


@SuppressWarnings("serial")
public class MainFrame extends JFrame
{
    private JTextField txt;
    private PopUpKeyboard keyboard;

    public MainFrame()
    {
        super("pop-up keyboard");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        txt = new JTextField(20);
        keyboard = new PopUpKeyboard(txt);

        txt.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                Point p = txt.getLocationOnScreen();
                p.y += 30;
                keyboard.setLocation(p);
                keyboard.setVisible(true);
            }
        });
        setLayout(new FlowLayout());
        add(txt);

        pack();
        setLocationByPlatform(true);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new MainFrame().setVisible(true);
            }
        });
    }

    private class PopUpKeyboard extends JDialog implements ActionListener
    {
        private JTextField txt;

        public PopUpKeyboard(JTextField txt)
        {
            this.txt = txt;
            setLayout(new GridLayout(3, 3));
            for(int i = 1; i <= 9; i++) createButton(Integer.toString(i));
            pack();
        }

        private void createButton(String label)
        {
            JButton btn = new JButton(label);
            btn.addActionListener(this);
            btn.setFocusPainted(false);
            btn.setPreferredSize(new Dimension(100, 100));
            Font font = btn.getFont();
            float size = font.getSize() + 15.0f;
            btn.setFont(font.deriveFont(size));
            add(btn);
        }

        @Override
        public void actionPerformed(ActionEvent e)
        {
            String actionCommand = e.getActionCommand();
            txt.setText(txt.getText() + actionCommand);
        }
    }
}

这篇关于如何将Java Swing应用程序实现到触摸屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆