0
我在GUI和java新,我曾與actionPerformed方法的一個問題,當我試圖寫爲這個問題的代碼:GUI類(JTextField中-的JTextArea)
- 使用幀佈局所示下面,編寫一個程序,用於搜索在文本字段中輸入標題的電影。當用戶按下SEARCH按鈕或按下ENTER鍵時,電影的信息(標題,年份和流派)將顯示在文本區域中。如果未找到該電影,則在文本區域中顯示一條消息,指示該電影不存在。使用數組來存儲許多電影的信息。
我真的很感激,如果有人解釋我如何讓這段代碼正常工作。
我嘗試:
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);
}
}
它的工作!!!非常感謝你的幫助 – user2252700 2013-04-06 19:13:06