修改以下代码,使得对于table的若干对键值输入由文件一键读入:package Practise; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.HashMap; class Listener implements ActionListener{ Window window; Listener(Window window){ this.window = window; } @Override public void actionPerformed(ActionEvent e) { if(window.table.containsKey(window.text1.getText())) { window.text2.setText(window.table.get(window.text1.getText())); } else window.text2.setText("Can not translate!"); } } class Window extends JFrame{ JTextField text1 = new JTextField(10); JTextField text2 = new JTextField(10); JButton button1 = new JButton("Translate"); HashMap<String,String> table; Listener listener = new Listener(this); Window(HashMap<String,String> table){ this.table = table; setBounds(160,100,400,300); add(text1); add(button1); button1.addActionListener(listener); add(text2); setDefaultCloseOperation(3); setLayout(new FlowLayout()); setVisible(true); } } public class practise5 { public static void main(String[] args){ HashMap<String,String> table = new HashMap<String, String>(); table.put("one","1"); table.put("two","2"); table.put("three","3"); Window window = new Window(table); } }
时间: 2024-03-26 15:41:47 浏览: 22
以下是修改后的代码:
```java
package Practise;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Scanner;
class Listener implements ActionListener {
Window window;
Listener(Window window) {
this.window = window;
}
@Override
public void actionPerformed(ActionEvent e) {
if (window.table.containsKey(window.text1.getText())) {
window.text2.setText(window.table.get(window.text1.getText()));
} else
window.text2.setText("Can not translate!");
}
}
class Window extends JFrame {
JTextField text1 = new JTextField(10);
JTextField text2 = new JTextField(10);
JButton button1 = new JButton("Translate");
HashMap<String, String> table;
Listener listener = new Listener(this);
Window(HashMap<String, String> table) {
this.table = table;
setBounds(160, 100, 400, 300);
add(text1);
add(button1);
button1.addActionListener(listener);
add(text2);
setDefaultCloseOperation(3);
setLayout(new FlowLayout());
setVisible(true);
}
public void readFrom(String fileName) throws FileNotFoundException {
Scanner scanner = new Scanner(new File(fileName));
while (scanner.hasNextLine()) {
String[] pair = scanner.nextLine().split(",");
if (pair.length == 2) {
table.put(pair[0], pair[1]);
}
}
scanner.close();
}
}
public class Practise5 {
public static void main(String[] args) {
HashMap<String, String> table = new HashMap<String, String>();
try {
Window window = new Window(table);
window.readFrom("input.txt");
} catch (FileNotFoundException e) {
System.out.println("File not found!");
}
}
}
```
修改后的代码中,我们添加了一个 `readFrom()` 方法,该方法接受一个文件名作为输入,从文件中读取键值对并将其存储在 `HashMap` 中。
在 `main()` 方法中,我们首先创建了一个空的 `HashMap`,然后创建了一个 `Window` 对象,并调用 `readFrom()` 方法从文件中读取键值对并存储到 `HashMap` 中。最终,我们将 `HashMap` 传递给 `Window` 对象,并显示窗口。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)