📄 reportwindow.java
字号:
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import java.util.*;public class ReportWindow extends JFrame implements ActionListener , WindowListener { private static ReportWindow instance = null; private final String OK = "Ok"; private final String CONTINUE = "Continue"; private double galsOffset; private JFrame curGals; private Color bgcolor = Color.WHITE; public static ReportWindow getInstance( double offset ) { if( instance == null ) instance = new ReportWindow( offset ); return instance; } protected ReportWindow( double offset ) { super(); galsOffset = offset; this.makeGUI(); } private void makeGUI() { setTitle( "Report" ); Mileage m = Mileage.getInstance(); Point p = m.getLocation(); int x = (int)p.getX(); int y = (int)p.getY(); setLocation( x + 100 , y + 100 ); getContentPane().setBackground( bgcolor ); getContentPane().setLayout( new GridLayout( 5 , 1 ) ); setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE ); addWindowListener( this ); addContent(); pack(); setVisible( true ); } private void addContent() { Mileage mileage = Mileage.getInstance(); Vector entries = mileage.getEntries(); String avgMPG = "Average Miles per Gallon: "; String avgMPF = "Average Miles per Fill: "; String avgCPG = "Average Cost per Gallon: $"; String avgCPM = "Average Cost per Mile: $"; String cost = "Total Amount Spent: $"; double mpg = 0; double totalMiles = 0; double mpf = 0; double cpm = 0; double totalGallons = 0; double totalCost = 0; for( int i = 0; i < entries.size(); i++ ) { totalGallons+= (new Double( ( (TableEntry)entries.get( i ) ).getGallons() ).doubleValue() ); } for( int i = 0; i < entries.size(); i++ ) { totalCost+= (new Double( ( (TableEntry)entries.get( i ) ).getCost() ).doubleValue() ); } if( entries.size() > 1 ) { double startMiles = new Double( ( (TableEntry)entries.get( 0 ) ).getOdometer() ).doubleValue(); double endMiles = (new Double( ( (TableEntry)entries.get( entries.size() - 1 ) ).getOdometer() ).doubleValue() ); totalMiles = endMiles - startMiles; cpm = (totalCost - (new Double( ( (TableEntry)entries.get( entries.size() - 1 ) ).getCost() ).doubleValue() ) ) / totalMiles; double gallonsUsed = totalGallons - galsOffset ; mpg = totalMiles / gallonsUsed; mpf = totalMiles / ( entries.size() - 1 ); } double cpg = totalCost / totalGallons; avgMPG += mpg; int index; if( avgMPG.length() > ( index = (avgMPG.lastIndexOf( "." ) + 3) ) ) avgMPG = avgMPG.substring( 0 , index ); avgMPF += mpf; if( avgMPF.length() > ( index = (avgMPF.lastIndexOf( "." ) + 2) ) ) avgMPF = avgMPF.substring( 0 , index ); avgCPG += cpg; if( avgCPG.length() > ( index = (avgCPG.lastIndexOf( "." ) + 4) ) ) avgCPG = avgCPG.substring( 0 , index ); avgCPM += cpm; if( avgCPM.length() > ( index = (avgCPM.lastIndexOf( "." ) + 4) ) ) avgCPM = avgCPM.substring( 0 , index ); cost += totalCost; String costString = new String( cost ); try { if( costString.indexOf( "." ) != -1 ) { costString = costString.substring( 0 , costString.indexOf( "." ) + 3 ); } } catch( Exception e ) {} getContentPane().add( new JLabel( avgMPG ) ); getContentPane().add( new JLabel( avgMPF ) ); getContentPane().add( new JLabel( avgCPG ) ); getContentPane().add( new JLabel( avgCPM ) ); getContentPane().add( new JLabel( costString ) ); } public void actionPerformed( ActionEvent e ) { String command = e.getActionCommand(); if( command.equals( CONTINUE ) ) dispose(); } public void windowClosing( WindowEvent e ) { instance = null; dispose(); } 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 ) {} }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -