www.pudn.com > lxc.rar > MovieEditor.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MovieEditor extends JDialog implements ActionListener{
private static MovieEditor theMovieEditor=null;
private Movie editedMovie=null;
protected GridBagLayout gridbag;
protected GridBagConstraints c;
private JPanel itemPanel;
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("Evaluation:");
private static JComboBox fldEvaluation;
private static JLabel lblComments=new JLabel("Comments:");
private static JTextArea textArea;
private static JButton bPrivious;
private static JButton bNext;
private static JButton bUpdate;
private static JButton bRevert;
private Movie movie;
protected void setAndAdd(JComponent comp,int gx,int gy,int gheight,int gwidth){
c.anchor=c.WEST;
c.gridx=gx;
c.gridy=gy;
c.gridheight=gheight;
c.gridwidth=gwidth;
gridbag.setConstraints(comp,c);
itemPanel.add(comp);
}
public static MovieEditor getInstance(){
if(theMovieEditor==null){
theMovieEditor=new MovieEditor();
}
return theMovieEditor;
}
public MovieEditor(){
super(WmvcApp.getFrame(),"Edit Movie",true);
}
public void initialize(){
itemPanel=new JPanel();
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);
//Evaluation 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();
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);
//将等待直到actionPerformed调用了
//setVisble(false)
return editedMovie;
}
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);
}
}
}