用Java产生一个软键盘
时间: 2024-03-01 17:54:26 浏览: 75
产生一个软键盘的方法有很多,以下是一个简单的Java代码示例,可以产生一个基本的软键盘:
```
import javax.swing.*;
import java.awt.*;
public class SoftKeyboard extends JFrame {
private JTextField textField;
public SoftKeyboard() {
super("Soft Keyboard");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
textField = new JTextField();
add(textField, BorderLayout.NORTH);
JPanel keyboardPanel = new JPanel(new GridLayout(4, 3));
for (int i = 1; i <= 9; i++) {
JButton button = new JButton(String.valueOf(i));
button.addActionListener(e -> {
textField.setText(textField.getText() + button.getText());
});
keyboardPanel.add(button);
}
JButton button0 = new JButton("0");
button0.addActionListener(e -> {
textField.setText(textField.getText() + button0.getText());
});
keyboardPanel.add(button0);
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(e -> {
textField.setText("");
});
keyboardPanel.add(clearButton);
add(keyboardPanel, BorderLayout.CENTER);
pack();
setVisible(true);
}
public static void main(String[] args) {
new SoftKeyboard();
}
}
```
这个软键盘只包含数字和清除按钮,但你可以根据自己的需要添加更多的按钮和功能。
阅读全文