📄 mileage.java
字号:
import java.io.*; import javax.swing.*;import javax.swing.event.*;import javax.swing.table.*;import java.awt.*;import java.awt.event.*;import java.util.*;public class Mileage extends JFrame implements ActionListener , WindowListener { private static Mileage instance = null; private final String DEFAULT_FILENAME = "records.jmd"; private String serialize_file = "records.jmd"; private String windowTitle = "Mileage"; private JScrollPane tableScroller; private JPanel tableFrame; private JPanel buttonFrame; private JTable infoTable; private JMenuBar menubar; private boolean modified = false; private boolean initialLoading = true; private Color bgcolor = Color.WHITE; // Action commands private final String ADD_RECORD = "Add Record"; private final String REMOVE_RECORD = "Remove Record"; private final String CREATE_REPORT = "Create Report"; private final String OPEN = "Open..."; private final String SAVE = "Save"; private final String SAVE_AS = "Save As..."; private final String QUIT = "Quit"; private Vector entries = new Vector(); public static Mileage getInstance() { if( instance == null ) instance = new Mileage(); return instance; } protected Mileage() { super(); readData(); makeGUI(); } private void makeGUI() { setTitle( windowTitle ); setBackground( bgcolor ); setLocation( 100 , 100 ); getContentPane().setLayout( new BorderLayout() ); setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE ); setSize( new Dimension( 400 , 300 ) ); setResizable( true ); addWindowListener( this ); buildMenus(); menubar.setBackground( bgcolor ); setJMenuBar( menubar ); tableFrame = new JPanel(); tableFrame.setBackground( bgcolor ); addTable(); tableFrame.add( tableScroller ); getContentPane().add( tableFrame , BorderLayout.CENTER ); buttonFrame = new JPanel(); addButtons(); getContentPane().add( buttonFrame , BorderLayout.SOUTH ); setVisible( true ); } private void buildMenus() { JMenu file = new JMenu( "File" ); file.setBackground( bgcolor ); menubar = new JMenuBar(); file.add( makeMenuItem( OPEN ) ); file.add( makeMenuItem( SAVE ) ); file.add( makeMenuItem( SAVE_AS ) ); file.add( makeMenuItem( QUIT ) ); menubar.add( file ); } private JMenuItem makeMenuItem( String text ) { JMenuItem item = new JMenuItem( text ); item.setBackground( bgcolor ); item.addActionListener( this ); return item; } private void addTable() { final Vector tableEntries = entries; TableModel model = new AbstractTableModel() { public int getColumnCount() { return 5; } public int getRowCount() { return entries.size(); } public String getColumnName( int column ) { switch( column ) { case 0: return "Vehicle"; case 1: return "Date"; case 2: return "Odometer"; case 3: return "Gallons"; case 4: return "Cost"; default: return ""; } } public Object getValueAt( int row, int col ) { switch( col ) { case 0: return ( (TableEntry)tableEntries.get( row ) ).getVehicle(); case 1: return ( (TableEntry)tableEntries.get( row ) ).getDate(); case 2: return ( (TableEntry)tableEntries.get( row ) ).getOdometer() + " m."; case 3: return ( (TableEntry)tableEntries.get( row ) ).getGallons() + " gal."; case 4: return "$" + ( (TableEntry)(tableEntries.get( row )) ).getCost(); default: return null; } } }; infoTable = new JTable( entries.size() , 5 ); infoTable.setBackground( bgcolor ); infoTable.setSelectionMode( ListSelectionModel.SINGLE_SELECTION ); infoTable.setModel( model ); infoTable.setAutoResizeMode( JTable.AUTO_RESIZE_ALL_COLUMNS ); tableScroller = new JScrollPane( infoTable ); tableScroller.setPreferredSize( new Dimension( 380 , 210 ) ); tableScroller.setBackground( bgcolor ); tableScroller.setVerticalScrollBar( new JScrollBar() ); tableScroller.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED ); } private void addButtons() { buttonFrame.setLayout( new FlowLayout() ); buttonFrame.add( makeButton( ADD_RECORD ) ); buttonFrame.add( makeButton( REMOVE_RECORD ) ); buttonFrame.add( makeButton( CREATE_REPORT ) ); buttonFrame.setBackground( bgcolor ); } private JButton makeButton( String text ) { JButton button = new JButton( text ); button.setActionCommand( text ); button.setToolTipText( text ); button.addActionListener( this ); return button; } public void actionPerformed( ActionEvent e ) { String command = e.getActionCommand(); if( command.equals( ADD_RECORD ) ) { RecordWindow.getInstance(); } else if( command.equals( REMOVE_RECORD ) ) { if( entries.size() > 0 ) { RemoveRecordWindow.getInstance( infoTable.getSelectedRow() ); } else { JOptionPane.showMessageDialog( null , "There are no records to remove." , "Oops" , JOptionPane.WARNING_MESSAGE ); } } else if( command.equals( CREATE_REPORT ) ) { switch( entries.size() ) { case 0: JOptionPane.showMessageDialog( null , "There are no records to report on." , "Oops" , JOptionPane.WARNING_MESSAGE ); break; case 1: ReportWindow.getInstance( 0 ); break; default: CheckGasWindow.getInstance(); } } else if( command.equals( QUIT ) ) { windowClosing( null ); } else if( command.equals( SAVE ) ) { saveData( serialize_file ); modified = false; } else if( command.equals( SAVE_AS ) ) { // open file menu, then back up serialize_file JFileChooser dialog = new JFileChooser( "." ); dialog.setMultiSelectionEnabled( false ); dialog.setFileFilter( new MileageFileFilter() ); dialog.showSaveDialog( null ); String filePath = null; try { filePath = dialog.getSelectedFile().toString(); } catch( Exception ex ){} if( filePath != null ) saveData( dialog.getSelectedFile().toString() ); } else if( command.equals( OPEN ) ) { // open file menu, then back up serialize_file JFileChooser dialog = new JFileChooser( "." ); dialog.setMultiSelectionEnabled( false ); dialog.setFileFilter( new MileageFileFilter() ); dialog.showOpenDialog( null ); String filePath = null; try { filePath = dialog.getSelectedFile().toString(); } catch( Exception ex ){} if( filePath != null ) { serialize_file = filePath; readData(); updateTable(); } } } public void addRecord( String[] data ) { entries.add( new TableEntry( data[0] , data[1] , data[2] , data[3] , data[4] ) ); updateTable(); modified = true; } public void removeRecord( String entryID ) { String vehicle = entryID.substring( 0 , entryID.indexOf( ": " ) ); String date = entryID.substring( entryID.indexOf( ": " ) + 2 , entryID.length() ); boolean found = false; for( int i = 0; ( i < entries.size() ) && !found; i++ ) { TableEntry cur = (TableEntry)entries.get( i ); if( cur.getDate().equals( date ) && cur.getVehicle().equals( vehicle ) ) { entries.remove( i ); found = true; } } updateTable(); modified = true; } public Vector getEntries() { return entries; } public void updateTable() { tableFrame.setVisible( false ); tableFrame.removeAll(); tableFrame.setBackground( bgcolor ); addTable(); tableFrame.add( tableScroller ); getContentPane().add( tableFrame , BorderLayout.CENTER ); tableFrame.setVisible( true ); } private void readData() { try { FileInputStream ins = new FileInputStream( serialize_file ); ObjectInputStream p = new ObjectInputStream( ins ); entries = (Vector)p.readObject(); ins.close(); modified = false; } catch ( Exception e ) { if( !initialLoading ) { JOptionPane.showMessageDialog( null , "An error occurred while trying to read " + serialize_file + "!" , "Oops" , JOptionPane.ERROR_MESSAGE ); } } finally { initialLoading = false; } } private void saveData( String fileName ) { //serialize the map try { FileOutputStream out = new FileOutputStream( fileName ); ObjectOutputStream s = new ObjectOutputStream( out ); s.writeObject( entries ); s.flush(); out.close(); } catch( Exception ex ) { JOptionPane.showMessageDialog( null , "An error occurred while trying to write " + fileName + "!" , "Oops" , JOptionPane.ERROR_MESSAGE ); } } public void windowClosing( WindowEvent e ) { if( modified ) { int sel = JOptionPane.showConfirmDialog( null , "Save changes to " + serialize_file + "?" , "Quitting..." , JOptionPane.YES_NO_CANCEL_OPTION , JOptionPane.QUESTION_MESSAGE ); switch( sel ) { case JOptionPane.YES_OPTION: saveData( serialize_file ); case JOptionPane.NO_OPTION: dispose(); System.exit( 0 ); break; } } else { dispose(); System.exit( 0 ); } } public void windowActivated( WindowEvent e ) {} public void windowClosed( WindowEvent e ) {} public void windowDeactivated( WindowEvent e ) {} public void windowDeiconified( WindowEvent e ) {} public void windowIconified( WindowEvent e ) {} public void windowOpened( WindowEvent e ) {} public static void main( String[] args ) { Mileage.getInstance(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -