Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.io.*;
- class Search extends JFrame implements ActionListener
- {
- JTextField word;
- JTextArea meaning;
- JLabel wordLabel,meaningLabel;
- JButton search,back;
- FileReader fr;
- BufferedReader bra;
- public Search()
- {
- super("Dictionary");
- word=new JTextField(20);
- meaning=new JTextArea(2,20);
- meaning.setEditable(false);
- wordLabel=new JLabel("Word");
- meaningLabel=new JLabel("Meaning");
- search=new JButton("Search");
- search.setActionCommand("Search");
- search.addActionListener(this);
- back=new JButton("Back");
- back.setActionCommand("Back");
- back.addActionListener(this);
- GridBagLayout gbag=new GridBagLayout();
- GridBagConstraints gbc = new GridBagConstraints();
- setLayout(gbag);
- gbc.weightx = 0.0; // use a column weight of 1
- gbc.ipadx = 5; // pad by 5 units
- gbc.insets = new Insets(4, 4, 0, 0); //inset slightly from top left
- gbc.anchor = GridBagConstraints.CENTER;
- gbc.gridwidth = GridBagConstraints.RELATIVE;
- gbag.setConstraints(wordLabel, gbc);
- gbc.gridwidth = GridBagConstraints.REMAINDER;
- gbag.setConstraints(word, gbc);
- gbc.weighty = 0.0;
- gbc.weightx = 0.0;
- gbc.gridwidth = GridBagConstraints.RELATIVE;
- gbag.setConstraints(meaningLabel,gbc);
- gbc.gridwidth = GridBagConstraints.REMAINDER;
- gbag.setConstraints(meaning,gbc);
- gbc.weightx = 0.0;
- gbc.weighty = 0.0;
- gbc.gridwidth = GridBagConstraints.REMAINDER;
- gbag.setConstraints(search,gbc);
- gbc.gridwidth = GridBagConstraints.REMAINDER;
- gbag.setConstraints(back,gbc);
- add(wordLabel);
- add(word);
- add(meaningLabel);
- add(meaning);
- add(search);
- add(back);
- setSize(400, 400);
- setVisible(true);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public void actionPerformed(ActionEvent ae)
- {
- if((ae.getActionCommand()).equals("Back"))
- {
- setVisible(false);
- new Dictionary();
- }
- else if((ae.getActionCommand()).equals("Search"))
- {
- boolean b=search(word.getText());
- if(!(b))
- {
- JOptionPane.showMessageDialog(this,"Error reading from the file","Inane error",
- JOptionPane.ERROR_MESSAGE);
- }
- }
- }
- public boolean search(String w)
- {
- try
- {
- fr=new FileReader("Data.txt");
- bra=new BufferedReader(fr);
- }
- catch(Exception e)
- {
- return false;
- }
- boolean founded=false;
- String s;
- String s2[]=new String[2];
- try
- {
- while((s=bra.readLine())!=null)
- {
- s2=s.split(":");
- if(w.equalsIgnoreCase(s2[0]))
- {
- meaning.append(s2[1]+"\n");
- founded=true;
- }
- }
- }
- catch(IOException e)
- {
- return false;
- }
- if(!(founded))
- meaning.setText("No result found");
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement