etsavの日記: ぢゃば
日記 by
etsav
// M2Forecast.java
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
// Application launcher class.
public class M2Forecast {
// The entry point.
public static void main(String[] args) {
// Create the application.
TheApp = new M2ForecastApp();
}
public static M2ForecastApp TheApp; // The application.
}
// Application class.
class M2ForecastApp {
// Default constructor.
public M2ForecastApp() {
// Create the main window.
_mainWindow = new MainWindow(TheAppName);
}
private MainWindow _mainWindow;// The main window.
private static final String TheAppName = "M2 Forecast";
// The application name.
}
// Main window class.
class MainWindow extends JFrame implements ChangeListener {
private static final int _width = 600; // Window width.
private static final int _height = 200; // Window height.
private static final int _hGap = 5; // Horizontal gap between contents.
private static final int _vGap = 10; // Vertical gap between contents.
// The sole constructor.
public MainWindow(String windowTitle) {
super(windowTitle);
setDefaultCloseOperation(EXIT_ON_CLOSE);
SetContents();
ReadyContents();
setSize(_width, _height);
setVisible(true);
}
// Set the window constents.
private void SetContents() {
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(3, 3, _hGap, _vGap));
contentPane.add(
new JLabel("Your ID and registration date:", JLabel.RIGHT));
contentPane.add(
_yourID = new JSpinner(
_yourIDModel = new SpinnerNumberModel(15000, 0, 1000000, 1)));
contentPane.add(
_yourRegDate = new JSpinner(
_yourRegDateModel = new SpinnerDateModel()));
contentPane.add(
new JLabel("Current ID and date:", JLabel.RIGHT));
contentPane.add(
_currID = new JSpinner(
_currIDModel = new SpinnerNumberModel(16000, 0, 1000000, 1)));
contentPane.add(
_currDate = new JSpinner(
_currDateModel = new SpinnerDateModel()));
contentPane.add(
new JLabel("Target ID and reaching date:", JLabel.RIGHT));
contentPane.add(
_targetID = new JTextField());
_targetID.setHorizontalAlignment(JTextField.RIGHT);
contentPane.add(
_reachDate = new JTextField());
_reachDate.setHorizontalAlignment(JTextField.RIGHT);
_yourID.addChangeListener(this);
_yourRegDate.addChangeListener(this);
_currID.addChangeListener(this);
_currDate.addChangeListener(this);
_targetID.setEditable(false);
_reachDate.setEditable(false);
}
// Ready the window contents.
private void ReadyContents() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
_currDate.setValue(calendar.getTime());
calendar.set(2003, 2, 28);
_yourRegDate.setValue(calendar.getTime());
}
// State-changed event handler.
public void stateChanged(ChangeEvent e) {
// Get the values of controls.
int yourID = _yourIDModel.getNumber().intValue();
int currID = _currIDModel.getNumber().intValue();
Date yourDate = _yourRegDateModel.getDate();
Date currDate = _currDateModel.getDate();
// Control value constraints.
if (currID < yourID) currID = yourID;
if (currDate.before(yourDate)) currDate = yourDate;
_currIDModel.setMinimum(new Integer(yourID));
_currDateModel.setStart(yourDate);
_currID.setValue(new Integer(currID));
_currDate.setValue(currDate);
// Calculate the target ID number and show it.
int targetID = yourID * 10 / 9;
_targetID.setText(new Integer(targetID).toString());
// Avoid division-by-zero.
if ((currID == yourID) || (currDate.equals(yourDate))) {
_reachDate.setText("Unable to forecast.");
return;
}
// Forecast the reaching date.
long time = (currDate.getTime() - yourDate.getTime())
* (targetID - yourID)
/ (currID - yourID);
_reachDate.setText(
new SimpleDateFormat().format(
new Date(yourDate.getTime() + time)));
}
private JSpinner _yourID; // Your ID spinner.
private SpinnerNumberModel _yourIDModel; // Your ID spinner model.
private JSpinner _yourRegDate; // Registration date spinner.
private SpinnerDateModel _yourRegDateModel;
// Registration date spinner model.
private JSpinner _currID; // Current ID spinner.
private SpinnerNumberModel _currIDModel; // Current ID spinner model.
private JSpinner _currDate; // Current Date spinner.
private SpinnerDateModel _currDateModel; // Current Date spinner model.
private JTextField _targetID; // Target ID field.
private JTextField _reachDate; // Reaching date field.
}
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
// Application launcher class.
public class M2Forecast {
// The entry point.
public static void main(String[] args) {
// Create the application.
TheApp = new M2ForecastApp();
}
public static M2ForecastApp TheApp; // The application.
}
// Application class.
class M2ForecastApp {
// Default constructor.
public M2ForecastApp() {
// Create the main window.
_mainWindow = new MainWindow(TheAppName);
}
private MainWindow _mainWindow;// The main window.
private static final String TheAppName = "M2 Forecast";
// The application name.
}
// Main window class.
class MainWindow extends JFrame implements ChangeListener {
private static final int _width = 600; // Window width.
private static final int _height = 200; // Window height.
private static final int _hGap = 5; // Horizontal gap between contents.
private static final int _vGap = 10; // Vertical gap between contents.
// The sole constructor.
public MainWindow(String windowTitle) {
super(windowTitle);
setDefaultCloseOperation(EXIT_ON_CLOSE);
SetContents();
ReadyContents();
setSize(_width, _height);
setVisible(true);
}
// Set the window constents.
private void SetContents() {
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(3, 3, _hGap, _vGap));
contentPane.add(
new JLabel("Your ID and registration date:", JLabel.RIGHT));
contentPane.add(
_yourID = new JSpinner(
_yourIDModel = new SpinnerNumberModel(15000, 0, 1000000, 1)));
contentPane.add(
_yourRegDate = new JSpinner(
_yourRegDateModel = new SpinnerDateModel()));
contentPane.add(
new JLabel("Current ID and date:", JLabel.RIGHT));
contentPane.add(
_currID = new JSpinner(
_currIDModel = new SpinnerNumberModel(16000, 0, 1000000, 1)));
contentPane.add(
_currDate = new JSpinner(
_currDateModel = new SpinnerDateModel()));
contentPane.add(
new JLabel("Target ID and reaching date:", JLabel.RIGHT));
contentPane.add(
_targetID = new JTextField());
_targetID.setHorizontalAlignment(JTextField.RIGHT);
contentPane.add(
_reachDate = new JTextField());
_reachDate.setHorizontalAlignment(JTextField.RIGHT);
_yourID.addChangeListener(this);
_yourRegDate.addChangeListener(this);
_currID.addChangeListener(this);
_currDate.addChangeListener(this);
_targetID.setEditable(false);
_reachDate.setEditable(false);
}
// Ready the window contents.
private void ReadyContents() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
_currDate.setValue(calendar.getTime());
calendar.set(2003, 2, 28);
_yourRegDate.setValue(calendar.getTime());
}
// State-changed event handler.
public void stateChanged(ChangeEvent e) {
// Get the values of controls.
int yourID = _yourIDModel.getNumber().intValue();
int currID = _currIDModel.getNumber().intValue();
Date yourDate = _yourRegDateModel.getDate();
Date currDate = _currDateModel.getDate();
// Control value constraints.
if (currID < yourID) currID = yourID;
if (currDate.before(yourDate)) currDate = yourDate;
_currIDModel.setMinimum(new Integer(yourID));
_currDateModel.setStart(yourDate);
_currID.setValue(new Integer(currID));
_currDate.setValue(currDate);
// Calculate the target ID number and show it.
int targetID = yourID * 10 / 9;
_targetID.setText(new Integer(targetID).toString());
// Avoid division-by-zero.
if ((currID == yourID) || (currDate.equals(yourDate))) {
_reachDate.setText("Unable to forecast.");
return;
}
// Forecast the reaching date.
long time = (currDate.getTime() - yourDate.getTime())
* (targetID - yourID)
/ (currID - yourID);
_reachDate.setText(
new SimpleDateFormat().format(
new Date(yourDate.getTime() + time)));
}
private JSpinner _yourID; // Your ID spinner.
private SpinnerNumberModel _yourIDModel; // Your ID spinner model.
private JSpinner _yourRegDate; // Registration date spinner.
private SpinnerDateModel _yourRegDateModel;
// Registration date spinner model.
private JSpinner _currID; // Current ID spinner.
private SpinnerNumberModel _currIDModel; // Current ID spinner model.
private JSpinner _currDate; // Current Date spinner.
private SpinnerDateModel _currDateModel; // Current Date spinner model.
private JTextField _targetID; // Target ID field.
private JTextField _reachDate; // Reaching date field.
}
ぢゃば More ログイン