⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 recordwindow.java

📁 myMileage是一个简单地系统
💻 JAVA
字号:
 import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import java.util.*;public class RecordWindow extends JFrame implements ActionListener , WindowListener , KeyListener {			private static RecordWindow instance = null;	private final String OK = "Ok";	private final String CANCEL = "Cancel";		private Color bgcolor = Color.WHITE;		private JTextField vehicleInput= new JTextField( 10 );	private JTextField odometerInput= new JTextField( 10 );	private JTextField gallonsInput= new JTextField( 10 );	private JTextField costInput= new JTextField( 10 );		private String[] months = { "Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec" };	private JComboBox monthBox = new JComboBox();	private JComboBox dayBox = new JComboBox();	private JComboBox yearBox = new JComboBox();		private String[] data;		public static RecordWindow getInstance() {		if( instance == null ) instance = new RecordWindow();		return instance;	}		protected RecordWindow() { super(); this.makeGUI(); }		private void makeGUI() {		setTitle( "New Record" );		Mileage m = Mileage.getInstance();		Point p = m.getLocation();		int x = (int)p.getX();		int y = (int)p.getY();		setLocation( x + 100 , y + 100 );		getContentPane().setLayout( new BorderLayout() );		setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE );		addWindowListener( this );		addContent();		pack();		setVisible( true );	}		private void addContent() {		setBackground( bgcolor );		JPanel inputPanel = new JPanel();		inputPanel.setLayout( new GridLayout( 5 , 1 ) );		inputPanel.add( createComboBoxes() );		inputPanel.add( createInput( "Vehicle:             " , vehicleInput ) );		inputPanel.add( createInput( "Odometer:         " , odometerInput ) );		inputPanel.add( createInput( "Gallons:            " ,gallonsInput ) );		inputPanel.add( createInput( "Cost:                 " , costInput ) );		//inputPanel.add( new JLabel() );		inputPanel.setBackground( bgcolor );		getContentPane().add( inputPanel , BorderLayout.CENTER );		JPanel buttonPanel = new JPanel();		buttonPanel.setLayout( new FlowLayout() );		buttonPanel.setBackground( bgcolor );		buttonPanel.add( makeButton( OK ) );		buttonPanel.add( makeButton( CANCEL ) );		getContentPane().add( buttonPanel , BorderLayout.SOUTH );	}		private JPanel createComboBoxes() {		Calendar date = Calendar.getInstance();				for( int i = 0; i < months.length; i++ ) {			monthBox.addItem( months[i] );		}		for( int i = 1; i < 32; i++ ) {			dayBox.addItem( (new Integer( i )).toString() );		}		int curYear = date.get( Calendar.YEAR );		for( int i = curYear - 1; i < curYear+3; i++ ) {			yearBox.addItem( (new Integer( i )).toString() );		}		int curMonth = date.get( Calendar.MONTH );		int curDay = date.get( Calendar.DAY_OF_MONTH );		monthBox.setSelectedIndex( curMonth );		dayBox.setSelectedIndex( curDay - 1 );		yearBox.setSelectedIndex( 1 );				monthBox.setBackground( bgcolor );		dayBox.setBackground( bgcolor );		yearBox.setBackground( bgcolor );				JPanel datePanel = new JPanel();		datePanel.setBackground( bgcolor );		datePanel.setLayout( new FlowLayout() );		datePanel.add( new JLabel( "Date: " ) );		JPanel menus = new JPanel();		menus.setBackground( bgcolor );		menus.setLayout( new FlowLayout() );		menus.add( monthBox );		menus.add( dayBox );		menus.add( yearBox );		datePanel.add( menus );				return datePanel;		}		private JPanel createInput( String title , JTextField textField ) {		JPanel input = new JPanel();		input.setBackground( bgcolor );		input.setLayout( new FlowLayout() );		input.add( new JLabel( title ) );		textField.addKeyListener( this );		input.add( textField );		return input;	}		private JButton makeButton( String text ) {		JButton button = new JButton( text );		button.setActionCommand( text );		button.setToolTipText( text );		//button.setPreferredSize( new Dimension( 40 , 40 ) );		button.addActionListener( this );		return button;	}		public void actionPerformed( ActionEvent e ) {		String command = e.getActionCommand();		if( command.equals( OK ) ) {			next();		} else if( command.equals( CANCEL ) ) {			windowClosing( null );		} 	}		private void next() {		boolean okToContinue = true;		String vehicle = vehicleInput.getText();		String date = (monthBox.getSelectedIndex() + 1) + "/" + dayBox.getSelectedItem() + "/" + yearBox.getSelectedItem();		String odometer = odometerInput.getText();		String gallons = gallonsInput.getText();		String cost = costInput.getText();				Double odometerDouble;		Double gallonsDouble;		Double costDouble;							if( vehicle.equals( "" ) || odometer.equals("")||gallons.equals("")||cost.equals("") ) {			JOptionPane.showMessageDialog( null , "Please fill in all of the fields." , 											"Oops" , JOptionPane.WARNING_MESSAGE );			okToContinue = false;		} else {			try {				odometerDouble = new Double( odometer );				gallonsDouble = new Double( gallons );				costDouble = new Double(cost);								Vector entries = Mileage.getInstance().getEntries();				if( entries.size() > 1 ) {					String lastOdometerVal = ( (TableEntry)entries.get( entries.size() - 1 )).getOdometer();					if( (new Double( lastOdometerVal )).doubleValue() >= odometerDouble.doubleValue() ) {						JOptionPane.showMessageDialog( null , 							"The odometer must increase! Please enter an appropriate value for odometer." , 							"Oops" , JOptionPane.WARNING_MESSAGE );						okToContinue = false;					}				}							} catch( NumberFormatException nf ) {				JOptionPane.showMessageDialog( null , 					"Please fill in only numeric values for odometer, gallons, and 	cost." , 					"Oops" , JOptionPane.WARNING_MESSAGE );				okToContinue = false;			}		}				if( okToContinue ) {			String[] record = new String[5];			record[0] = vehicle;			record[1] = date;			record[2] = odometer.toString();			record[3] = gallons.toString();			record[4] = cost.toString();			Mileage.getInstance().addRecord( record );			windowClosing( null );		}	}	public void keyReleased( KeyEvent e ) {		if( e.getKeyCode() == KeyEvent.VK_ENTER )			next();	}	public void keyTyped( KeyEvent e ) {}	public void keyPressed( KeyEvent e ) {}					public void windowClosing( WindowEvent e ) {		dispose();		instance = null;	}	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 + -