maininterface.java

来自「本人写的一个简单VCD租赁系统」· Java 代码 · 共 318 行

JAVA
318
字号
//************************************************//MainInterface.java//This class is used to create the main GUI window.//Execute this class to run the application.//Created by Xiaobin Lin               3/Dec/2004//************************************************package Main;import javax.swing.*;import javax.swing.WindowConstants.*;import java.io.*;import java.awt.*;import java.awt.event.*;public class MainInterface extends JFrame {        // Creates new form MainInterface    public MainInterface() {        boolean fileExist = true;        // Create file object...        File loadFile = new File("save.dat");        // Create the file input stream...        FileInputStream fileInputStream = null;        try        {            fileInputStream = new FileInputStream(loadFile);        }        catch(FileNotFoundException e) //throw out a exception when file not found        {            fileExist = false;                    }        if (fileExist == true){            ObjectInputStream objectInputStream = null;            try{                // Create the object input stream...                objectInputStream = new ObjectInputStream(fileInputStream);                 // Read the object from the object input stream...                memberSet = (MemberSet) objectInputStream.readObject();                videoSet = (VideoSet) objectInputStream.readObject();                lendingSet = (LendingSet) objectInputStream.readObject();              }            catch(ClassNotFoundException e){                System.out.println(e);            }            catch(IOException e){                System.out.println(e);            }        }        else{            memberSet = new MemberSet();             videoSet = new VideoSet();            lendingSet = new LendingSet();        }        //set table model               tableHandlerMember = new TableHandler(memberSet);        tableHandlerVideo = new TableHandler(videoSet);        initComponents();    }        // This method is called from within the constructor to    // initialize the form.    private void initComponents() {        tabbedPaneMain = new JTabbedPane();        scrollPaneMembers = new JScrollPane();        tableMembers = new JTable();        scrollPaneVideos = new JScrollPane();        tableVideos = new JTable();        menuBarMain = new JMenuBar();        menuOperation = new JMenu();        menuItemBorrow = new JMenuItem();        menuItemReturn = new JMenuItem();        separator1 = new JSeparator();        menuItemAddVideo = new JMenuItem();        menuItemDelVideo = new JMenuItem();        separator2 = new JSeparator();        menuItemExit = new JMenuItem();        menuHelp = new JMenu();        menuItemAbout = new JMenuItem();        getContentPane().setLayout(null);        setDefaultCloseOperation(EXIT_ON_CLOSE);        setTitle("Have-Videos-Must-Watch");        setName("frameMainInterface");        setResizable(false);        tabbedPaneMain.setFont(new Font("Times New Roman", 0, 12));        tabbedPaneMain.setPreferredSize(new Dimension(457, 300));        scrollPaneMembers.setFont(new Font("Times New Roman", 0, 12));        scrollPaneMembers.setPreferredSize(new Dimension(452, 270));        tableMembers.setFont(new Font("Times New Roman", 0, 12));        tableMembers.setModel(tableHandlerMember);        scrollPaneMembers.setViewportView(tableMembers);        tabbedPaneMain.addTab("View All Member Details", scrollPaneMembers);        scrollPaneVideos.setFont(new Font("Times New Roman", 0, 12));        scrollPaneVideos.setPreferredSize(new Dimension(452, 270));        tableVideos.setFont(new Font("Times New Roman", 0, 12));        tableVideos.setModel(tableHandlerVideo);        scrollPaneVideos.setViewportView(tableVideos);        tabbedPaneMain.addTab("View All Video Details", scrollPaneVideos);        getContentPane().add(tabbedPaneMain);        tabbedPaneMain.setBounds(10, 10, 540, 350);        menuOperation.setMnemonic('o');        menuOperation.setText("Operation");        menuOperation.setFont(new Font("Times New Roman", 0, 12));        menuItemBorrow.setFont(new Font("Times New Roman", 0, 12));        menuItemBorrow.setMnemonic('b');        menuItemBorrow.setText("Borrow a Video");        menuItemBorrow.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent evt) {                menuItemBorrowActionPerformed(evt);            }        });        menuOperation.add(menuItemBorrow);        menuItemReturn.setFont(new Font("Times New Roman", 0, 12));        menuItemReturn.setMnemonic('r');        menuItemReturn.setText("Return a Video");        menuItemReturn.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent evt) {                menuItemReturnActionPerformed(evt);            }        });        menuOperation.add(menuItemReturn);        menuOperation.add(separator1);        menuItemAddVideo.setFont(new Font("Times New Roman", 0, 12));        menuItemAddVideo.setMnemonic('a');        menuItemAddVideo.setText("Add a New Video");        menuItemAddVideo.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent evt) {                menuItemAddVideoActionPerformed(evt);            }        });        menuOperation.add(menuItemAddVideo);        menuItemDelVideo.setFont(new Font("Times New Roman", 0, 12));        menuItemDelVideo.setMnemonic('d');        menuItemDelVideo.setText("Delete a Video");        menuItemDelVideo.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent evt) {                menuItemDelVideoActionPerformed(evt);            }        });        menuOperation.add(menuItemDelVideo);        menuOperation.add(separator2);        menuItemExit.setFont(new Font("Times New Roman", 0, 12));        menuItemExit.setMnemonic('e');        menuItemExit.setText("Exit");        menuItemExit.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent evt) {                menuItemExitActionPerformed(evt);            }        });        menuOperation.add(menuItemExit);        menuBarMain.add(menuOperation);        menuHelp.setMnemonic('h');        menuHelp.setFont(new Font("Times New Roman", 0, 12));        menuHelp.setLabel("Help");        menuItemAbout.setFont(new Font("Times New Roman", 0, 12));        menuItemAbout.setMnemonic('a');        menuItemAbout.setText("About");        menuItemAbout.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent evt) {                menuItemAboutActionPerformed(evt);            }        });        menuHelp.add(menuItemAbout);        menuBarMain.add(menuHelp);        setJMenuBar(menuBarMain);        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();        setBounds((screenSize.width-570)/2, (screenSize.height-420)/2, 570, 420);    }//the end of initComponents    //when "about" menuitem is selected    private void menuItemAboutActionPerformed(ActionEvent evt) {        aboutDialog = new AboutDialog(this, true);        aboutDialog.setVisible(true);    }        //when "del-video" menuitem is selected    private void menuItemDelVideoActionPerformed(ActionEvent evt) {        deleteVideoDialog = new DeleteVideoDialog(this, true, videoSet, lendingSet);        deleteVideoDialog.setVisible(true);        saveToFile();        tableHandlerVideo.updateTable(videoSet);        tableVideos.updateUI();    }        //when "add-video" menuitem is selected    private void menuItemAddVideoActionPerformed(ActionEvent evt) {        addNewVideoDialog = new AddNewVideoDialog(this, true, videoSet);        addNewVideoDialog.setVisible(true);        saveToFile();        tableHandlerVideo.updateTable(videoSet);        tableVideos.updateUI();    }    //when "return" menuitem is selected    private void menuItemReturnActionPerformed(ActionEvent evt) {        returnDialog = new ReturnDialog(this, true, memberSet, videoSet, lendingSet);        returnDialog.setVisible(true);        //save to file        saveToFile();    }        //when "exit" menuitem is selected    private void menuItemExitActionPerformed(ActionEvent evt) {        exitApplication();    }        //when "borrow" menuitem is selected    private void menuItemBorrowActionPerformed(ActionEvent evt) {        borrowDialog = new BorrowDialog(this, true, memberSet, videoSet, lendingSet);        borrowDialog.setVisible(true);        saveToFile();    }        //exit application    public void exitApplication(){	setVisible(false);	dispose();	System.exit(0);		    }        //save memberSet, videoSet and lendingSet to "save.dat" file        private void saveToFile(){         // Create out file object...        File saveFile = new File("save.dat");               // Create the file output stream...        FileOutputStream fileOutputStream = null;        try        {            fileOutputStream = new FileOutputStream(saveFile);        }            catch(FileNotFoundException e)        {           System.out.println(e);        }               // Create the object output stream...        ObjectOutputStream objectOutputStream = null;                try        {           objectOutputStream = new ObjectOutputStream(fileOutputStream);                  // Write the object to the object output stream...           objectOutputStream.writeObject(memberSet);           objectOutputStream.writeObject(videoSet);           objectOutputStream.writeObject(lendingSet);        }        catch(IOException e)        {           System.out.println(e);        }    }        //main method    public static void main(String args[]) {        java.awt.EventQueue.invokeLater(new Runnable() {            public void run() {                new MainInterface().setVisible(true);            }        });    }        // Variables declaration - GUI-related    private JMenuBar menuBarMain;    private JMenu menuHelp;    private JMenuItem menuItemAbout;    private JMenuItem menuItemAddVideo;    private JMenuItem menuItemBorrow;    private JMenuItem menuItemDelVideo;    private JMenuItem menuItemExit;    private JMenuItem menuItemReturn;    private JMenu menuOperation;    private JScrollPane scrollPaneMembers;    private JScrollPane scrollPaneVideos;    private JSeparator separator1;    private JSeparator separator2;    private JTabbedPane tabbedPaneMain;    private JTable tableMembers;    private JTable tableVideos;    // Variables declaration - non-GUI-related    private TableHandler tableHandlerMember;    private TableHandler tableHandlerVideo;    private MemberSet memberSet;    private VideoSet videoSet;    private LendingSet lendingSet;    private BorrowDialog borrowDialog;    private ReturnDialog returnDialog;    private AddNewVideoDialog addNewVideoDialog;    private DeleteVideoDialog deleteVideoDialog;    private AboutDialog aboutDialog;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?