我是夏晨龙小迷妹

package review;

public interface ITour {
String Suggest(String city);
}
package review;
import java.util.*;
import java.io.*;
public class CityTour implements ITour {
    HashMap<String,String> tourMap=new HashMap<>();
    private String fileName;
    public CityTour(String fileName){
        this.fileName=fileName;
        try(Scanner sc=new Scanner(new FileReader(this.fileName))){
            while(sc.hasNextLine()){
                String line=sc.nextLine();
                String []pair=line.split(":");
                tourMap.put(pair[0], pair[1]);
            }
        }catch(Exception e){
            System.out.println("NO SUCH FILE FOUND");
        }
    }
    public String Suggest(String city){
        return tourMap.getOrDefault(city, "CITY NOT FOUND");
    }
}
package review;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.WindowEvent;

import javax.swing.*;
public class TestTour extends JFrame {
    ITour tourSuggest=new CityTour("D:\\java import\\BlankJavaProject\\cities.txt.txt");
    public static void main(String[] args){
        new TestTour();
    }
    JTextField text1=new JTextField(20);
    JTextField text2=new JTextField(20);
    public TestTour(){
        JFrame frame=new JFrame("Urban attraction inquire");
        Dimension d=new Dimension();
        d.setSize(400,200);
        frame.setSize(d);
        frame.setBackground(Color.red);
        Point p=new Point(100,100);
        
        JPanel jpanel=new JPanel();
        JLabel cityLabel=new JLabel("City");
        JLabel tourLabel=new JLabel("Tour Attractions");
        JButton button=new JButton("OK");
        jpanel.setLayout(new BoxLayout(jpanel,BoxLayout.Y_AXIS));
        
        JPanel panel1=new JPanel(new FlowLayout(FlowLayout.RIGHT));
        panel1.add(cityLabel);
        panel1.add(text1);
        panel1.setAlignmentX(Component.CENTER_ALIGNMENT);
        jpanel.add(panel1);
        
        JPanel panel2=new JPanel(new FlowLayout(FlowLayout.RIGHT));
        panel2.add(tourLabel);
        panel2.add(text2);
        panel2.setAlignmentX(Component.CENTER_ALIGNMENT);
        jpanel.add(panel2);
        
        JPanel panel3=new JPanel(new FlowLayout(FlowLayout.RIGHT));
        panel3.add(button);
        panel3.setAlignmentX(Component.CENTER_ALIGNMENT);
        jpanel.add(panel3);
        
        frame.add(jpanel);
        frame.setVisible(true);
        frame.setLocation(p);
        
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                String city=String.valueOf(text1.getText());
                text2.setText(tourSuggest.Suggest(city));
            }
        });
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值