📄 movieeditor.java
字号:
/* MovieEditor - edits a Movie object for MovieCat * Copyright (c) 2001, Bruce E. Wampler */import java.awt.*;import java.awt.event.*;import javax.swing.*;public class MovieEditor extends JDialog implements ActionListener{ // Implement MovieEditor with Singleton pattern private static MovieEditor theMovieEditor = null; private Movie editedMovie = null; protected GridBagLayout gridbag; protected GridBagConstraints c; private JPanel itemPanel; // Various components needed for constructing the view // We use private statics of each of these since there will // be only one instance of the editor private static JLabel lblTitle = new JLabel("Movie Title: "); private static JTextField fldTitle = new JTextField(30); private static JLabel lblDirector=new JLabel("Director: "); private static JTextField fldDirector = new JTextField(30); private static JLabel lblYear = new JLabel("Year: "); private static JTextField fldYear = new JTextField(6); private static JLabel lblRating = new JLabel("Rating: "); private static JComboBox fldRating; private static JLabel lblGenre = new JLabel(" Genre: "); private static JComboBox fldGenre; private static JLabel lblFormat = new JLabel(" Format: "); private static JComboBox fldFormat; private static JLabel lblLabel = new JLabel(" Label: "); private static JTextField fldLabel = new JTextField(8); private static JLabel lblEvaluation = new JLabel("My Rating: "); private static JComboBox fldEvaluation; private static JLabel lblComments = new JLabel("Comments: "); private static JTextArea textArea; private static JButton bPrevious; private static JButton bNext; private static JButton bUpdate; private static JButton bRevert; private Movie movie; // for a local copy protected void setAndAdd(JComponent comp, int gx, int gy, int gheight, int gwidth) { // to simplify laying out the gridbag c.anchor = c.WEST; c.gridx = gx; c.gridy = gy; c.gridheight = gheight; c.gridwidth = gwidth; gridbag.setConstraints(comp,c); itemPanel.add(comp); } static public MovieEditor getInstance() { if (theMovieEditor == null) { theMovieEditor = new MovieEditor(); } return theMovieEditor; } public MovieEditor() { // call JDialog constructor super(WmvcApp.getFrame(), "Edit Movie", true); } public void initialize() { // We will use a GridBag for simple layout of itemView itemPanel = new JPanel(); // surrounding Panel gridbag = new GridBagLayout(); c = new GridBagConstraints(); itemPanel.setLayout(gridbag); itemPanel.setBorder(BorderFactory.createEmptyBorder( 5, 5, 5, 5)); // Movie Title: ________________________ setAndAdd(lblTitle, 0, 0, 1, 1); setAndAdd(fldTitle, 1, 0, 1, c.REMAINDER); // Director: _____________________ setAndAdd(lblDirector, 0, 1, 1, 1); setAndAdd(fldDirector, 1, 1, 1, c.REMAINDER); // Year: _______ Genre: _________ setAndAdd(lblYear, 0, 2, 1, 1); setAndAdd(fldYear, 1, 2, 1, 1); setAndAdd(lblGenre, 2, 2, 1, c.RELATIVE); fldGenre = new JComboBox(MovieGenre.getNames()); setAndAdd(fldGenre, 3, 2, 1, c.REMAINDER); // Rating: _______ Format: __________ setAndAdd(lblRating, 0, 3, 1, 1); fldRating = new JComboBox(MovieRating.getNames()); setAndAdd(fldRating, 1, 3, 1, 1); setAndAdd(lblFormat, 2, 3, 1, c.RELATIVE); fldFormat = new JComboBox(MovieFormat.getNames()); setAndAdd(fldFormat, 3, 3, 1, c.REMAINDER); // My Rating: ______ Label: _________ setAndAdd(lblEvaluation, 0, 4, 1, 1); fldEvaluation = new JComboBox( MovieEvaluation.getNames()); setAndAdd(fldEvaluation, 1, 4, 1, 1); setAndAdd(lblLabel, 2, 4, 1, c.RELATIVE); setAndAdd(fldLabel, 3, 4, 1, c.REMAINDER); // Comment box: setAndAdd(lblComments, 0,5,1,1); textArea = new JTextArea(4,30); JScrollPane textScroll = new JScrollPane(textArea); setAndAdd(textScroll, 1,5,4,c.REMAINDER); // Command Buttons bRevert = new JButton(" Cancel "); bRevert.setActionCommand("revert"); bRevert.addActionListener(this); setAndAdd(bRevert, 2,9,1,1); bUpdate = new JButton(" OK "); bUpdate.setActionCommand("update"); bUpdate.addActionListener(this); setAndAdd(bUpdate, 3,9,1,1); Container contentPane = getContentPane(); contentPane.add(itemPanel,BorderLayout.CENTER); pack(); } public Movie showDialog(Component comp, Movie m) { if (theMovieEditor == null || m == null) return null; editedMovie = null; movie = (Movie)m.clone();// make a copy to work with // Set box to current fields fldTitle.setText(movie.getTitle()); fldDirector.setText(movie.getDirector()); fldYear.setText(movie.getYear()); fldLabel.setText(movie.getLabel()); fldRating.setSelectedIndex(movie.getRating()); fldGenre.setSelectedIndex(movie.getGenre()); fldFormat.setSelectedIndex(movie.getFormat()); fldEvaluation.setSelectedIndex(movie.getEvaluation()); textArea.setText(movie.getComments()); setLocationRelativeTo(comp); setVisible(true); // will now wait here until actionPerformed // calls setVisible(false) return editedMovie; } // Implement ActionListener public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("update")) { movie.setTitle(fldTitle.getText()); movie.setDirector(fldDirector.getText()); movie.setYear(fldYear.getText()); movie.setLabel(fldLabel.getText()); movie.setRating(fldRating.getSelectedIndex()); movie.setGenre(fldGenre.getSelectedIndex()); movie.setFormat(fldFormat.getSelectedIndex()); movie.setEvaluation( fldEvaluation.getSelectedIndex()); movie.setComments(textArea.getText()); editedMovie = movie; setVisible(false); } else if (e.getActionCommand().equals("revert")) { editedMovie = null; setVisible(false); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -