1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| package d5;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
public class PanHCB extends JPanel {
public PanHCB() {
this.setFocusable(true);
}
Font ftitre = new Font("Courrier", Font.BOLD, 60);
Font fontLabel1 = new Font("Courrier", Font.PLAIN, 40);
Font fontLabel2 = new Font("Courrier", Font.PLAIN, 30);
Font fontLabel3 = new Font("Courrier", Font.ITALIC, 40);
Font font4 = new Font("Courrier", Font.BOLD, 15);
private String masseString = "0", allureString = "0", newStopTime;
JLabel newStopTimeLabel = new JLabel();
private int c1 = 535, c2 = 885, c3 = 1100, l1 = 350, l2 = 450;
JTextField masseT = new JTextField();
JTextField allureText = new JTextField();
JLabel masse = new JLabel();
JLabel masseU = new JLabel();
JLabel allureT = new JLabel();
JLabel allureU = new JLabel();
JButton calcHCB = new JButton();
public void paintComponent (Graphics g) {
g.setColor(Color.DARK_GRAY);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.GRAY);
g.setFont(ftitre);
g.drawString("Calcul de l'heure de changement de bac", 270, 150);
masse.setText("Masse pompable :");
masse.setFont(fontLabel1);
masse.setForeground(Color.GRAY);
masse.setBounds(c1, l1, 400, 75);
masse.setVisible(true);
this.add(masse);
masseT.setFont(fontLabel1);
masseT.setForeground(Color.GRAY);
masseT.setBounds(c2, l1, 200, 75);
masseT.setHorizontalAlignment(JTextField.CENTER);
masseT.setText(masseString);
this.add(masseT);
masseU.setText("t");
masseU.setFont(fontLabel1);
masseU.setForeground(Color.GRAY);
masseU.setBounds(c3, l1, 25, 75);
this.add(masseU);
allureT.setText("Allure :");
allureT.setFont(fontLabel1);
allureT.setForeground(Color.GRAY);
allureT.setBounds(c1, l2, 400, 75);
this.add(allureT);
allureText.setFont(fontLabel1);
allureText.setForeground(Color.GRAY);
allureText.setBounds(c2, l2, 200, 75);
allureText.setHorizontalAlignment(JTextField.CENTER);
allureText.setText(allureString);
this.add(allureText);
allureU.setText("t/j");
allureU.setFont(fontLabel1);
allureU.setForeground(Color.GRAY);
allureU.setBounds(c3, l2, 35, 75);
this.add(allureU);
calcHCB.setText("Calcul");
calcHCB.setBounds(745, 575, 200, 50);
calcHCB.setFont(fontLabel2);
calcHCB.setBackground(Color.GRAY);
calcHCB.setBorder(BorderFactory.createLineBorder(Color.GRAY));
calcHCB.setFocusPainted(false);
this.add(calcHCB);
calcHCB.addActionListener(e->{
whatToDo();
});
addAncestorListener(new AncestorListener() {
@Override
public void ancestorRemoved(AncestorEvent event) {}
@Override
public void ancestorMoved(AncestorEvent event) {}
@Override
public void ancestorAdded(AncestorEvent event) {
JFrame FenetreMainD5 = (JFrame)SwingUtilities.getWindowAncestor(PanHCB.this);
FenetreMainD5.getRootPane().setDefaultButton(calcHCB);
}
});
}
public void whatToDo() {
this.add(newStopTimeLabel);
masseString = masseT.getText();
double masse = Double.parseDouble(masseString);
allureString = allureText.getText();
double allure = Double.parseDouble(allureString);
double allureMinute = allure/1440;
double minutesToAdd = masse/allureMinute;
int minutasToAdd = (int) minutesToAdd;
SimpleDateFormat sdfStopTime = new SimpleDateFormat ("HH:mm");
Calendar calend = Calendar.getInstance();
calend.add(Calendar.MINUTE, minutasToAdd);
Date d = calend.getTime();
newStopTime = sdfStopTime.format(d);
newStopTimeLabel.setFont(fontLabel3);
newStopTimeLabel.setForeground(Color.GRAY);
if (masse <= 0.0 && allure > 0.0) {
newStopTimeLabel.setText("Veuillez saisir une valeur correcte pour la masse pompable.");
newStopTimeLabel.setBounds(320, 650, 1100, 150);}
else if (masse > 0.0 && allure <= 0.0) {
newStopTimeLabel.setText("Veuillez saisir une valeur correcte pour l'allure.");
newStopTimeLabel.setBounds(440, 650, 900, 150);}
else if (masse <= 0.0 && allure <= 0.0) {
newStopTimeLabel.setText("Veuillez saisir des valeurs correctes.");
newStopTimeLabel.setBounds(520, 650, 800, 150);}
else {
newStopTimeLabel.setText("Le changement de bac est prévu à " + newStopTime);
newStopTimeLabel.setBounds(480, 650, 800, 150);}
newStopTimeLabel.setVisible(true);
this.requestFocus();
revalidate();
}
} |