Advertisement
Guest User

Untitled

a guest
Feb 26th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.72 KB | None | 0 0
  1.  
  2. import java.awt.Color;
  3. import java.awt.event.*;
  4. import java.io.BufferedReader;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.util.HashMap;
  9. import java.util.StringTokenizer;
  10. import javax.swing.*;
  11.  
  12. public class Słownik12 extends JFrame implements ActionListener {
  13.  
  14.     String i;
  15.     JButton przycisk;
  16.     JLabel etykieta1, etykieta2;
  17.     JTextField tekstPA, tekstAP;
  18.     HashMap<String, String> value = new HashMap<String, String>();
  19.  
  20.     public Słownik12() throws FileNotFoundException, IOException {
  21.         setSize(400, 400); //rozmiar ramki  
  22.         setTitle("SŁOWNIK ANG-POL & POL-ANG"); //tytuł
  23.         setLayout(null);
  24.         przycisk = new JButton("Tłumacz");
  25.         przycisk.setBounds(150, 160, 100, 60);//polozenie przycisku
  26.         add(przycisk);//dodanie przycisku
  27.         przycisk.addActionListener(this); //słuchaczem jest cała ramka
  28.  
  29.         etykieta1 = new JLabel("ang");
  30.         etykieta1.setBounds(100, 100, 70, 60);
  31.         etykieta1.setForeground(Color.BLUE);
  32.         //add(etykieta1);
  33.  
  34.         tekstAP = new JTextField();
  35.         tekstAP.setBounds(150, 100, 100, 40);
  36.         tekstAP.setEnabled(false);
  37.         add(tekstAP);
  38.  
  39.  
  40.         etykieta2 = new JLabel("pol");
  41.         etykieta2.setBounds(100, 50, 70, 60);
  42.         etykieta2.setForeground(Color.BLUE);
  43.         //add(etykieta2);
  44.  
  45.         tekstPA = new JTextField();
  46.         tekstPA.setBounds(150, 50, 100, 40);
  47.  
  48.         add(tekstPA);
  49.  
  50.        
  51.         FileReader fr = new FileReader(System.getProperty("user.home") + "\\Desktop\\slownik.txt");
  52.         BufferedReader br = new BufferedReader(fr);
  53.         String s;
  54.  
  55.         while ((s = br.readLine()) != null) //odczytanie kolejnych linijek pliku           
  56.         {
  57.             StringTokenizer st = new StringTokenizer(s, ",");
  58.             while(st.hasMoreTokens())
  59.             {
  60.                
  61.                 String key = st.nextToken();
  62.                 String val = st.nextToken();
  63.                 value.put(key,val) ;
  64.                 value.put(val,key);
  65.             }
  66.         }
  67.         System.out.println(value);
  68.         fr.close();
  69.  
  70.     }
  71.  
  72.     public static void main(String[] args) throws FileNotFoundException, IOException {
  73.  
  74.         Słownik12 slownik = new Słownik12();
  75.         slownik.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //zamyka okienko krzyzykiem
  76.         slownik.setVisible(true); // wyswietli ramke
  77.  
  78.     }
  79.  
  80.     @Override
  81.     public void actionPerformed(ActionEvent e) //obiekt zdarzenia, który jest wysyłany przez przycisk do słuchacza
  82.     {
  83.         if (e.getSource() == przycisk) {
  84.             tekstAP.setText(value.get(tekstPA.getText()));
  85.            
  86.  
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement