📄 checkgaswindow.java
字号:
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import java.util.*;public class CheckGasWindow extends JFrame implements ActionListener , WindowListener , KeyListener { private static CheckGasWindow instance = null; private final String CONTINUE = "Continue"; private Color bgcolor = Color.WHITE; private double remainingGas; private JTextField gallonsInput= new JTextField( 10 ); public static void getInstance() { if( instance == null ) instance = new CheckGasWindow(); } protected CheckGasWindow() { super(); this.makeGUI(); } private void makeGUI() { setTitle( "Gas Level" ); setSize( new Dimension( 200 , 180 ) ); 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( 3 , 1 , 5 , 0 ) ); Vector entries = Mileage.getInstance().getEntries(); JPanel labelWrapper = new JPanel(); labelWrapper.setBackground( bgcolor ); labelWrapper.setLayout( new FlowLayout() ); JPanel labels = new JPanel(); labels.setBackground( bgcolor ); labels.setLayout( new GridLayout( 3 , 1 , 5 , 0 ) ); labels.add( new JLabel( "Approximately how much gas" ) ); labels.add( new JLabel( "was in the vehicle after your" ) ); labels.add( new JLabel( "fueling on " + ( (TableEntry)entries.get( entries.size() - 1 ) ).getDate() + "?" ) ); labelWrapper.add( labels ); getContentPane().add( labelWrapper ); JPanel input = new JPanel(); input.setBackground( bgcolor ); input.setLayout( new FlowLayout() ); gallonsInput.addKeyListener( this ); input.add( gallonsInput ); getContentPane().add( input ); JPanel buttonPanel = new JPanel(); buttonPanel.setBackground( bgcolor ); buttonPanel.setLayout( new FlowLayout() ); buttonPanel.add( makeButton( CONTINUE ) ); getContentPane().add( buttonPanel ); addWindowListener( this ); addKeyListener( this ); setVisible( true ); } private JButton makeButton( String text ) { JButton button = new JButton( text ); button.setActionCommand( text ); button.setToolTipText( text ); button.addActionListener( this ); return button; } public void windowClosing( WindowEvent e ) { Vector entries = Mileage.getInstance().getEntries(); 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 ) {} public void actionPerformed( ActionEvent e ) { String command = e.getActionCommand(); if( command.equals( CONTINUE ) ) { next(); } } private void next() { double remainingGas = 0; String input = gallonsInput.getText(); String errorMsg = ""; boolean okToContinue = true; if( input.equals( "" ) ) { errorMsg = "Please fill in the text field."; okToContinue = false; } else { try { remainingGas = (new Double( input )).doubleValue(); Vector entries = Mileage.getInstance().getEntries(); double lastFill = (new Double( ( (TableEntry)entries.get( entries.size() - 1 ) ).getGallons() ) ).doubleValue(); if( remainingGas < lastFill ) { errorMsg = "You pumped " + lastFill + " gallons last time, you must " + "\nhave had at least that much gas!"; okToContinue = false; } } catch( NumberFormatException nf ) { errorMsg = "Please enter a valid numeric value."; okToContinue = false; } } if( okToContinue ) { ReportWindow.getInstance( remainingGas ); windowClosing( null ); } else { JOptionPane.showMessageDialog( null , errorMsg ,"Oops" , JOptionPane.WARNING_MESSAGE ); } } public void keyReleased( KeyEvent e ) { if( e.getKeyCode() == KeyEvent.VK_ENTER ) next(); } public void keyTyped( KeyEvent e ) {} public void keyPressed( KeyEvent e ) {} }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -