如何在触摸屏上实现 Java Swing 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11623777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement a Java Swing application to Touch Screen
提问by Chan
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.
我们已经建立了一个销售点系统,现在我们需要将它实施到触摸屏上?我们是否需要依次更改任何代码才能使其正常工作。
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?
我们正在使用键盘输入值 - 让我们说数量 - 当我专注于 JTextField 时,是否有一种 java 方式弹出键盘(如 android)?
采纳答案by Eng.Fouad
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);
}
}
}
回答by Charlie
If you don't need multi-touch, the normal mouse drivers for use with most touch screen controllers will just have the touch-screen emulate a normal mouse where a finger touching the screen is emulated as a mouse click.
如果您不需要多点触控,与大多数触摸屏控制器一起使用的普通鼠标驱动程序只会让触摸屏模拟普通鼠标,其中手指触摸屏幕被模拟为鼠标点击。
As for a virtual keyboard, there are crummy ones built into Windows and MacOSX but it would probably be best to build one into the application if you can.
至于虚拟键盘,Windows 和 MacOSX 中内置了一些简陋的键盘,但如果可以,最好在应用程序中内置一个。
If you need multi touch or have issues with specific touch screen controllers, there are a few options.
如果您需要多点触控或对特定的触摸屏控制器有问题,有几个选项。
Your best bet in swing, at least on windows, seems to be this project: http://www.michaelmcguffin.com/code/JWinPointer/
至少在 Windows 上,你最好的挥杆选择似乎是这个项目:http: //www.michaelmcguffin.com/code/JWinPointer/
JavaFX appears to have touch support, Intel has a tutorial: https://software.intel.com/en-us/articles/using-javafx-to-implement-multi-touch-with-java-on-windows-8-desktop. You might be able to get this working with swing somehow as there are methods to host Swing in JavaFX and JavaFX in Swing, you might look for other answers to accomplish interop between both.
JavaFX 似乎有触摸支持,英特尔有一个教程:https: //software.intel.com/en-us/articles/using-javafx-to-implement-multi-touch-with-java-on-windows-8-桌面。您可能能够以某种方式使用 Swing 进行此操作,因为有一些方法可以在 JavaFX 中托管 Swing 和在 Swing 中托管 JavaFX,您可能会寻找其他答案来完成两者之间的互操作。
There was project MT4J, but it seems to be defunct. It doesn't seem to work with Swing or JavaFX.
有项目 MT4J,但它似乎已不复存在。它似乎不适用于 Swing 或 JavaFX。
回答by MadProgrammer
You should be able to provide your own virtual keyboard through the use of something like a JWindow
and the KeyboardFocusManager
您应该能够通过使用类似 aJWindow
和KeyboardFocusManager
回答by Robin
We implemented a custom Look-and-feel for our Swing application with touch support to make everything just look bigger (all buttons, checkboxes, ..., even JTree
instances) so that it is easy to modify them using a finger.
我们为我们的具有触摸支持的 Swing 应用程序实现了一个自定义的外观和感觉,使所有东西看起来都更大(所有按钮、复选框、...,甚至JTree
实例),以便使用手指轻松修改它们。
Such a solution might save you the work to convert all your UI's to a touch-friendly UI.
这样的解决方案可能会节省您将所有 UI 转换为触摸友好 UI 的工作。
回答by Max JSwing
You should change your user experience and interaction design, thus change the code. Check out Windows UX guidelines for touch - https://msdn.microsoft.com/en-us/library/windows/desktop/dn742468.aspx
你应该改变你的用户体验和交互设计,从而改变代码。查看 Windows UX 触控指南 - https://msdn.microsoft.com/en-us/library/windows/desktop/dn742468.aspx