如何将Java Swing应用程序实现到触摸屏

如何将Java Swing应用程序实现到触摸屏,第1张

如何将Java Swing应用程序实现到触摸屏

这是有关如何实现d出式键盘的简单示例

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);        }    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5506940.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存