/* 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);
}
}
}
Java与UML面向对象程序设计的源码
需积分: 50 141 浏览量
2006-02-23
09:05:59
上传
评论
收藏 78KB RAR 举报
普通网友
- 粉丝: 882
- 资源: 2万+
最新资源
- 机械设计整经机上纱自动化sw20非常好的设计图纸100%好用.zip
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈