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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
package ihm;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
public class test extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L ;
private JFrame fen = new JFrame();
private JLabel[] tab = new JLabel[24];
private JTextField[] ta = new JTextField[24];
private JTextField[] tap = new JTextField[24];
private JTextField[] tar = new JTextField[24];
private JButton valider = new JButton ("Valider");
private JButton rec = new JButton ("Enregistrer");
private JTextField jtf = new JTextField();
private JTextField jtfp = new JTextField();
private JLabel jtfr = new JLabel();
private JPanel pan = new JPanel();
private JPanel panel = new JPanel();
/**
* Construteur de la classe
*/
public test() throws IOException {
fen.setSize(1300,300);
fen.setLayout(null);
JScrollPane scrollBar=new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
fen.add(scrollBar);
fen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// exit
pan.setLayout(null);
pan.setBounds(50,0,1200,50);
fen.add(pan);
jtf.setLayout(null);
jtf.setBounds(50,50,1200,20);
fen.add(jtf);// on ajoute
jtfp.setLayout(null);
jtfp.setBounds(50,75,1200,20);
fen.add(jtfp);
jtfr.setLayout(null);
jtfr.setBounds(50,100,1200,20);
fen.add(jtfr);
valider.setLayout(null);
valider.setBounds(0,175,200,20);
fen.add(valider);
rec.setLayout(null);
rec.setBounds(210,175,200,20);
fen.add(rec);
for(int i = 0; i<24;i++){
int j = i + 1;
tab[i]= createJLabel();
tab[i].setText("ep"+j);
ta[i]= createTextField();
ta[i].setText("0");
tap[i]= createTextField();
tap[i].setText("0");
}
for(int i = 0; i<24;i++){
/**
* Ici on va positionner à mano les label pour bien voir l'effet
* On leur met une largeur de 50, on en a 10 et la fenêtre fait 50
* il doivent donc chacun à leur tour être espacé de 50px
* donc i*50
*/
tab[i].setBounds((i*50), 0, 50, 50);
ta[i].setBounds((i*50), 0, 50, 20);
tap[i].setBounds((i*50), 0, 50, 20);
pan.add(tab[i]);
jtf.add(ta[i]);
jtfp.add(tap[i]);
}
fen.setVisible(true);
fen.validate();
fen.repaint();
valider.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for(int i = 0; i<24;i++){
Double[] re = new Double[24];
Double[] epc = new Double[24];
Double[] pdsd = new Double[24];
String[] ep = new String[24];
String[] pds = new String[24];
String[] res = new String[24];
ep[i] = ta[i].getText().toString();
pds[i] = tap[i].getText().toString();
epc[i] = new Double(ep[i]);
pdsd[i] = new Double(pds[i]);
re[i]= epc[i]*pdsd[i];
res[i] = String.valueOf(re[i]);
tar[i]= createTextField();
tar[i].setText(res[i]);
tar[i].setBounds((i*50), 0, 50, 20);
jtfr.add(tar[i]);
}
}
});
rec.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String filename = File.separator+"tmp";
JFileChooser fc = new JFileChooser(new File(filename));
fc.showOpenDialog(fen);
File save = fc.getSelectedFile();
try {
FileWriter out = new FileWriter(save);
for(int i = 0; i<24;i++){
String[] res = new String[24];
Double[] re = new Double[24];
res[i] = tar[i].getText().toString();
re[i] = new Double(res[i]);
out.write(res[i]);
out.write("\r\n");
}
out.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
public JLabel createJLabel(){
JLabel jl = new JLabel();
return jl;
}
public JTextField createTextField(){
JTextField jt = new JTextField(10);
return jt;
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
} |
Partager