【发布时间】:2021-12-29 19:55:18
【问题描述】:
我的程序遇到了问题。目标是收集输入(电影名称、媒体类型和年份)并在单击“添加电影”时将其附加到列表中。然后,当单击“显示电影”按钮时,列表将显示在文本区域中。我不确定我错过了什么或做错了什么。
import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class MovieDatabase extends JFrame implements ActionListener{
//create LinkedList of Movie Objects
LinkedList<Movie> list = new LinkedList<Movie>();
//JPanel for input movie
private JPanel inputJPanel;
//JLable and JTextField for Movie Name
private JLabel movieJLabel;
private JTextField movieJTextField;
//JLable and JTextField for Media
private JLabel mediaJLabel;
private JTextField mediaJTextField;
//JLable and JTextField for Release Year
private JLabel yearJLabel;
private JTextField yearJTextField;
//JButton to add movie to a list
private JButton addJButton;
//JButton to show movie in text area
private JLabel showJLabel;
private JButton showJButton;
//JTextArea to display movies from list
private JTextArea showJTextArea;
private JPanel listJPanel;
//no argument constructor
public MovieDatabase(){
createUserInterface();
}
//create GUI window with components
private void createUserInterface(){
//get content pane window and set layout to null - no layout manager
Container contentPane = getContentPane();
contentPane.setLayout(null);
//set up input panel
inputJPanel = new JPanel();
inputJPanel.setLayout(null);
inputJPanel.setBorder(new TitledBorder("Input Movie")); //anonymous object
inputJPanel.setBounds(20,4,260,178);// (x,y,w,h)
contentPane.add(inputJPanel);
//set up input panel
showJLabel = new JLabel();
showJLabel.setLayout(null);
showJLabel.setText("Movies: ");
showJLabel.setBounds(300,0,260,25);// (x,y,w,h)
contentPane.add(showJLabel);
//set up payment JTextArea here
showJTextArea = new JTextArea();
showJTextArea.setBounds(300,30,300,145);// (x,y,w,h)
showJTextArea.setEditable(false);
contentPane.add(showJTextArea);
//show movies JButton
showJButton = new JButton();
showJButton.setBounds(480,175,110,30);
showJButton.setText("Show Movies");
contentPane.add(showJButton);
//movie name JLabel
movieJLabel = new JLabel();
movieJLabel.setBounds(10,24,100,21);// (x,y,w,h)
movieJLabel.setText("Movie Name:");
inputJPanel.add(movieJLabel);
//movie name JTextField
movieJTextField = new JTextField();
movieJTextField.setBounds(104,24,120,21);
movieJTextField.setHorizontalAlignment(JTextField.RIGHT);
inputJPanel.add(movieJTextField);
//media name JLabel
mediaJLabel = new JLabel();
mediaJLabel.setBounds(10,54,100,21);// (x,y,w,h)
mediaJLabel.setText("Media:");
inputJPanel.add(mediaJLabel);
//media name JTextField
mediaJTextField = new JTextField();
mediaJTextField.setBounds(104,54,120,21);
mediaJTextField.setHorizontalAlignment(JTextField.RIGHT);
inputJPanel.add(mediaJTextField);
//year name JLabel
yearJLabel = new JLabel();
yearJLabel.setBounds(10,84,100,21);// (x,y,w,h)
yearJLabel.setText("Release Year:");
inputJPanel.add(yearJLabel);
//year name JTextField
yearJTextField = new JTextField();
yearJTextField.setBounds(104,84,80,21);
yearJTextField.setHorizontalAlignment(JTextField.RIGHT);
inputJPanel.add(yearJTextField);
//add movie JButton
addJButton = new JButton();
addJButton.setBounds(92,138,94,24);
addJButton.setText("Add Movie");
inputJPanel.add(addJButton);
//set window properties
setTitle("Movie"); //set title bar
setSize(625, 250);//window size
setVisible(true); //display window
addJButton.addActionListener(this);
showJButton.addActionListener(this);
}
public void addButtonactionPerformed(ActionEvent e){
String movieName = movieJTextField.getText();
String media = mediaJTextField.getText();
int year = Integer.parseInt(yearJTextField.getText());
//create instance of Movie
Movie movie = new Movie(movieName, media, year);
list.add(movie);
movieJTextField.setText("");
mediaJTextField.setText("");
yearJTextField.setText("");
}
private void showButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
showJTextArea.setText("");
String str = String.format("%-20s%-20s%-20s\n", "Year", "Media", "Title");
showJTextArea.append(str);
for (Movie movie : list) {
str = String.format("%-20s%-19s%-22s\n", Integer.toString(movie.year), movie.media, movie.name);
showJTextArea.append(str);
}
}
}
class Movie{
String name;
String media;
int year;
public Movie(String n, String m, int y){
name = n;
media = m;
year = y;
}
public class MovieGUI {
public static void main(String[] args) {
MovieDatabase application = new MovieDatabase();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}//close and stop application
}
}
【问题讨论】:
-
ActionListener的接口只有一种方法可以实现actionPerformed(ActionEvent e)- 这将被两个按钮调用。根据ActionEvent的Source,您应该采取不同的行动 -
null布局很烂,花点时间学习如何使用布局管理器Laying Out Components Within a Container;您还没有实现ActionListener的要求(showButtonActionPerformed表明您正在使用诸如 Netbeans 表单编辑器之类的东西)。也许你应该看看How to Write an Action Listener -
你可能还想看看How to Use Tables
标签: java list user-interface actionlistener