您可以通过使用for循环来构造按钮.每个键盘行的一个循环是合理的方法.
String row1 = "1234567890";
String row2 = "qwertyuiop";
// and so forth
String[] rows = { row1, row2, .. };
for (int i = 0; i < rows.length; i++) {
    char[] keys = rows[i].toCharArray();
    for (int j = 0; i < keys.length; j++) {
        JButton button = new JButton(Character.toString(keys[j]));
        // add button
    }
}
// add special buttons like space bar
这可以通过更多的OOP方法更优雅地完成,但是这个基本的循环系统将起作用.
这个简单的例子可以帮助您:

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);
        }
    }
}
| 归档时间: | 
 | 
| 查看次数: | 13249 次 | 
| 最近记录: |