用java编写一个应用程序,要求如下: 窗口中有一个 TextField 对象和一个按钮对象,将这两个对象添加到一个面板中,然后将该面板添加到窗口的上面。 用户在 TextField 对象中输入一个英文单词,然后按 Enter 键或单击“确定”按钮,程序将创建若干个不可编辑的文本框,每个文本框随机显示英文单词中的一个字母。要求将这些文本框按一行添加到一个面板中,然后将该面板添加到窗口的中心。 用户用鼠标单击一个文本框后,通过按下键盘上的“→”和“←”键交换相邻文本框中的字母,使得这些文本框中字母的排列顺序和英文单词中字母的顺序相同。
时间: 2023-06-13 07:05:07 浏览: 301
以下是一个简单的Java应用程序,满足您的要求:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class WordPuzzle extends JFrame implements ActionListener, KeyListener {
private JTextField textField;
private JButton button;
private JPanel topPanel, centerPanel;
private JLabel[] letterLabels;
private int[] letterIndices;
private int selectedLabelIndex = -1;
public WordPuzzle() {
setTitle("Word Puzzle");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
// 创建顶部面板
topPanel = new JPanel();
textField = new JTextField(20);
textField.addKeyListener(this);
button = new JButton("确定");
button.addActionListener(this);
topPanel.add(textField);
topPanel.add(button);
add(topPanel, BorderLayout.NORTH);
// 创建中心面板
centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(1, 0));
add(centerPanel, BorderLayout.CENTER);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
// 获取输入单词
String word = textField.getText().trim().toLowerCase();
int wordLength = word.length();
// 清空中心面板
centerPanel.removeAll();
// 创建字母标签数组和索引数组
letterLabels = new JLabel[wordLength];
letterIndices = new int[wordLength];
for (int i = 0; i < wordLength; i++) {
letterLabels[i] = new JLabel("" + word.charAt(i), JLabel.CENTER);
letterIndices[i] = i;
centerPanel.add(letterLabels[i]);
}
// 随机打乱字母标签
shuffleLabels();
// 更新界面
centerPanel.revalidate();
centerPanel.repaint();
}
}
public void keyPressed(KeyEvent e) {
if (selectedLabelIndex >= 0 && e.getKeyCode() == KeyEvent.VK_LEFT) {
// 向左移动选中的标签
if (selectedLabelIndex > 0) {
int tempIndex = letterIndices[selectedLabelIndex];
letterIndices[selectedLabelIndex] = letterIndices[selectedLabelIndex - 1];
letterIndices[selectedLabelIndex - 1] = tempIndex;
updateLabelPositions();
selectedLabelIndex--;
}
} else if (selectedLabelIndex >= 0 && e.getKeyCode() == KeyEvent.VK_RIGHT) {
// 向右移动选中的标签
if (selectedLabelIndex < letterLabels.length - 1) {
int tempIndex = letterIndices[selectedLabelIndex];
letterIndices[selectedLabelIndex] = letterIndices[selectedLabelIndex + 1];
letterIndices[selectedLabelIndex + 1] = tempIndex;
updateLabelPositions();
selectedLabelIndex++;
}
}
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
private void shuffleLabels() {
int numLabels = letterLabels.length;
for (int i = numLabels - 1; i >= 0; i--) {
int j = (int) (Math.random() * (i + 1));
JLabel tempLabel = letterLabels[i];
letterLabels[i] = letterLabels[j];
letterLabels[j] = tempLabel;
}
updateLabelPositions();
}
private void updateLabelPositions() {
for (int i = 0; i < letterLabels.length; i++) {
int index = letterIndices[i];
letterLabels[i].setText("" + textField.getText().charAt(index));
}
}
public static void main(String[] args) {
new WordPuzzle();
}
}
这个程序创建了一个窗口,在窗口的顶部有一个文本框和一个按钮,用户输入一个英文单词后按下按钮或者回车键,程序会在窗口的中心区域随机打乱单词的字母并显示出来。用户可以通过鼠标单击一个字母并按下键盘上的左右箭头键来交换相邻字母的位置,直到还原单词的顺序。
相关推荐















