简体   繁体   中英

GUI classes (JTextField-JTextArea)

I'm new at the GUI and java, I had a problem with the actionPerformed method when I tried to write the code for this question:

  1. Using the frame layout shown below, write a program that searches for a movie whose title is entered in the text field. When the user presses the SEARCH button or presses the ENTER key, the information of the movie (Title, Year, and Genre) is displayed in the text area. If the movie is not found, display a message in the text area indicating that this movie does not exist. Use an array to store the information of a number of movies.

I'd really appreciate it if someone explained how can I get this code to work properly.
My attempt:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class movie {
    public String title;
    public String year;
    public String genre;
    public movie(String t, String y, String g) {
        title = t;
        year = y;
        genre = g;
    }
    public String toString() {
        return "TITLE: " + title + "\nYEAR: " + year + "\nGENRE: " + genre;
    }
}

public class searchMovieFrame extends JFrame implements ActionListener {
    public movie m1 = new movie("Iron Man", "2008", "Action,Adventure");
    public movie m2 = new movie("Iron Man", "2010", "Action,Adventure");
    public movie m3 = new movie("Total Recall", "2012", "Action,Adventure");
    public movie[] movies = {
        m1, m2, m3
    };

    private static final int width = 300;
    private static final int height = 200;
    private static final int x = 360;
    private static final int y = 150;
    private JButton search;
    private JTextField input;
    private JTextArea output;
    private JLabel message;

    public searchMovieFrame() {
        Container contentPane = getContentPane();
        setSize(width, height);
        setResizable(false);
        setTitle("Search Movie Frame");
        setLocation(x, y);
        contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));
        message = new JLabel();
        message.setText("Enter the movie title please");
        message.setSize(150, 25);
        contentPane.add(message);
        input = new JTextField();
        input.setColumns(15);
        contentPane.add(input);
        input.addActionListener(this);
        search = new JButton("Search");
        contentPane.add(search);
        search.addActionListener(this);
        output = new JTextArea();
        output.setColumns(23);
        output.setRows(5);
        output.setEditable(false);
        contentPane.add(output);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent event) {
        for (int i = 0; i < 3; i++)
        if (input.getText().equals(movies[i].title)) output.setText(toString());
        else output.setText("THAT MOVIE IS NOT AVAILABLE");
    }

    public static void main(String[] args) {
        searchMovieFrame frame = new searchMovieFrame();
        frame.setVisible(true);
    }
}

You need to break once you find the correct movie.

Also use Movie#toString rather than the toString representation of the current JFrame . Don't limit your search to just the first 3 movies, use movies.length as the upper bound for your search. For efficiency, any component updates should take place after the loop has been processed.

Movie searchMovie = null;
for (int i = 0; i < movies.length; i++) {
   if (input.getText().equals(movies[i].title)) {
      searchMovie = movies[i];
      break;
   }
}

if (searchMovie == null) {
   output.setText("THAT MOVIE IS NOT AVAILABLE");
} else {
   output.setText(searchMovie.toString());
}

Aside: Use Java naming conventions to distinguish classes such as Movie .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM