java可视化界面分页看单词的,数据有单词和释义,有大约6000个单词量,写出代码
时间: 2024-05-10 14:18:42 浏览: 132
以下是一个简单的Java可视化界面分页查看单词的示例代码:
```java
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class WordList extends JFrame implements ActionListener {
private JPanel contentPane;
private JTextField searchField;
private JTextArea resultArea;
private JButton searchButton, prevButton, nextButton;
private ArrayList<String> words;
private ArrayList<String> definitions;
private int currentPage = 1;
private int totalPages;
public WordList() {
super("Word List");
// 初始化单词和释义列表
words = new ArrayList<String>();
definitions = new ArrayList<String>();
// 在这里添加单词和释义,可以从文本文件中读取,或者从数据库中查询
words.add("apple");
definitions.add("a round fruit with red or green skin and a white inside");
words.add("banana");
definitions.add("a long curved fruit with a yellow skin");
words.add("cat");
definitions.add("a small domesticated carnivorous mammal with soft fur");
// 设置窗口的大小和位置
setSize(500, 400);
setLocationRelativeTo(null);
// 创建主面板
contentPane = new JPanel(new BorderLayout());
setContentPane(contentPane);
// 创建顶部搜索框和按钮
JPanel searchPanel = new JPanel();
searchField = new JTextField(20);
searchButton = new JButton("Search");
searchPanel.add(searchField);
searchPanel.add(searchButton);
contentPane.add(searchPanel, BorderLayout.NORTH);
// 创建中间显示结果的文本区域
resultArea = new JTextArea();
resultArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(resultArea);
contentPane.add(scrollPane, BorderLayout.CENTER);
// 创建底部分页按钮
JPanel pagePanel = new JPanel();
prevButton = new JButton("Prev");
nextButton = new JButton("Next");
pagePanel.add(prevButton);
pagePanel.add(nextButton);
contentPane.add(pagePanel, BorderLayout.SOUTH);
// 添加事件监听器
searchButton.addActionListener(this);
prevButton.addActionListener(this);
nextButton.addActionListener(this);
// 计算总页数
totalPages = (int) Math.ceil(words.size() / 10.0);
// 显示第一页的结果
showPage(1);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == searchButton) {
// 搜索按钮被点击,显示符合条件的结果
String query = searchField.getText().toLowerCase();
resultArea.setText("");
for (int i = 0; i < words.size(); i++) {
String word = words.get(i).toLowerCase();
String definition = definitions.get(i);
if (word.contains(query)) {
resultArea.append(word + ": " + definition + "\n");
}
}
currentPage = 1;
updatePageButtons();
} else if (e.getSource() == prevButton) {
// 上一页按钮被点击,显示上一页的结果
if (currentPage > 1) {
currentPage--;
showPage(currentPage);
}
updatePageButtons();
} else if (e.getSource() == nextButton) {
// 下一页按钮被点击,显示下一页的结果
if (currentPage < totalPages) {
currentPage++;
showPage(currentPage);
}
updatePageButtons();
}
}
// 显示指定页码的结果
private void showPage(int pageNum) {
int startIndex = (pageNum - 1) * 10;
int endIndex = Math.min(startIndex + 9, words.size() - 1);
resultArea.setText("");
for (int i = startIndex; i <= endIndex; i++) {
resultArea.append(words.get(i) + ": " + definitions.get(i) + "\n");
}
}
// 更新分页按钮的状态,禁用或启用上一页和下一页按钮
private void updatePageButtons() {
prevButton.setEnabled(currentPage > 1);
nextButton.setEnabled(currentPage < totalPages);
}
public static void main(String[] args) {
WordList frame = new WordList();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
```
这个程序将单词和释义列表存储在两个ArrayList中,并提供了一个搜索框和按钮,在用户输入单词并点击搜索按钮时,程序将显示所有包含该单词的结果。
底部的分页按钮允许用户切换结果列表中的页码。默认情况下,每页显示10个结果。程序使用了Java的Swing库来创建GUI组件,并使用了事件监听器来处理用户交互。
阅读全文