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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
| import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.text.NumberFormat;
import javax.swing.JFormattedTextField;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Fenetre extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private CardLayout pile;
// JPannels
private JPanel container = new JPanel();
private JPanel Panel1 = new JPanel();
private JPanel Panel2 = new JPanel();
private JPanel Panel2_0 = new JPanel();
private JPanel Panel2_1 = new JPanel();
private JPanel Panel2_2 = new JPanel();
private JPanel Panel2_3 = new JPanel();
// JTextArea
private JTextArea results = new JTextArea();
// RadioButtons (choix de la méthode)
private JRadioButton choice1 = new JRadioButton("Carrés médians");
private JRadioButton choice2 = new JRadioButton("Fibonacci");
private JRadioButton choice3 = new JRadioButton("GCL");
private JRadioButton choice4 = new JRadioButton("LFSR");
// ButtonGroup associé au RadioButtons
private ButtonGroup bg1 = new ButtonGroup();
// Button normal
private JButton test = new JButton("Test");
// JFormattedTextField (champs du formulaire des coefficients)
private JFormattedTextField germe1 = new JFormattedTextField(
NumberFormat.getIntegerInstance());
private JFormattedTextField germe2 = new JFormattedTextField(
NumberFormat.getIntegerInstance());
private JFormattedTextField multiplicateur = new JFormattedTextField(
NumberFormat.getIntegerInstance());
private JFormattedTextField incrément = new JFormattedTextField(
NumberFormat.getIntegerInstance());
private JFormattedTextField modulo = new JFormattedTextField(
NumberFormat.getIntegerInstance());
// JLabel
private JLabel label = new JLabel("0");
public Fenetre() {
this.setTitle("Générateur de nombres pseudo aléatoires");
this.setSize(900, 600);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// ContentPane : container
container.setBackground(Color.ORANGE);
container.setLayout(new BorderLayout());
this.setContentPane(container);
// JPanel des choix de l'utilisateur (méthode) : Panel1
Panel1.setLayout(new GridLayout(4, 1));
Panel1.setPreferredSize(new Dimension(150, 500));
// Panel1.setLocation(0, 0);
container.add(Panel1, BorderLayout.WEST);
//
choice1.addActionListener(new ChoiceListener());
choice2.addActionListener(new ChoiceListener());
choice3.addActionListener(new ChoiceListener());
choice4.addActionListener(new ChoiceListener());
//
choice1.setMnemonic(0);
choice2.setMnemonic(1);
choice3.setMnemonic(2);
choice4.setMnemonic(3);
//
Panel1.add(choice1);
Panel1.add(choice2);
Panel1.add(choice3);
Panel1.add(choice4);
//
bg1.add(choice1);
bg1.add(choice2);
bg1.add(choice3);
bg1.add(choice4);
// JPanel du formulaire des coefficients : Panel2
Panel2.setPreferredSize(new Dimension(150, 250));
Panel2.setBackground(Color.GREEN);
Panel2.setLayout(pile);
container.add(Panel2, BorderLayout.CENTER);
// JPanel pour le choix "Carrées médians" : Panel2_0
Panel2_0.add(germe1);
Panel2.add(Panel2_0, "2_0");
// JPanel pour le choix "Fibonacci" : Panel2_1
Panel2_1.add(germe1);
Panel2_1.add(germe2);
Panel2_1.add(modulo);
Panel2.add(Panel2_1, "2_1");
// JPanel pour le choix "GCL" : Panel2_2
Panel2_2.add(germe1);
Panel2_2.add(multiplicateur);
Panel2_2.add(incrément);
Panel2_2.add(modulo);
Panel2.add(Panel2_2, "2_2");
// JPanel pour le choix "LFSR" : Panel2_3
Panel2_3.add(germe1);
Panel2.add(Panel2_3, "2_3");
// JButton : test
test.addActionListener(this);
// test.setPreferredSize(new Dimension(150, 250));
// test.setLocation(400, 0);
container.add(test, BorderLayout.NORTH);
// JScrollPane (Scrollbar)
JScrollPane scroll = new JScrollPane(results,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
// results.add(scroll, null);
scroll.setBounds(new Rectangle(-4, 1, 397, 198));
// JTextArea des résultats
results.setPreferredSize(new Dimension(150, 400));
results.setLocation(600, 0);
results.setEditable(false);
container.add(results, BorderLayout.EAST);
// Modification de la sortie système (console)
JTextAreaOutputStream outStream = new JTextAreaOutputStream(results);
JTextAreaOutputStream errStream = new JTextAreaOutputStream(results);
System.setOut(new PrintStream(outStream));
System.setErr(new PrintStream(errStream));
}
public void actionPerformed(ActionEvent arg0) {
// Listener Button
for (int i = 0; i < 100; i++) {
System.out.println(i);
}
}
class ChoiceListener implements ActionListener {
// Listener des RadioButton (choix de la méthode)
public void actionPerformed(ActionEvent e) {
if (bg1.getSelection().getMnemonic() == 0) {
pile.show(Panel2, "2_0");
}
if (bg1.getSelection().getMnemonic() == 1) {
pile.show(Panel2, "2_1");
}
if (bg1.getSelection().getMnemonic() == 2) {
pile.show(Panel2, "2_2");
}
if (bg1.getSelection().getMnemonic() == 3) {
pile.show(Panel2, "2_3");
}
}
}
class JTextAreaOutputStream extends OutputStream {
// // Modification de la sortie système (console)
private JTextArea m_textArea = null;
/**
* Method JTextAreaOutputStream.
*
* @param aTextArea
* le JTextArea qui recevra les caractères.
*/
public JTextAreaOutputStream(JTextArea aTextArea) {
m_textArea = aTextArea;
}
/**
* Écrit un caractère dans le JTextArea. Si le caractère est un retour
* chariot, scrolling.
*
* @see java.io.OutputStream#write(int)
*/
public void write(int b) throws IOException {
byte[] bytes = new byte[1];
bytes[0] = (byte) b;
String newText = new String(bytes);
m_textArea.append(newText);
if (newText.indexOf('\n') > -1) {
try {
m_textArea.scrollRectToVisible(m_textArea
.modelToView(m_textArea.getDocument().getLength()));
} catch (javax.swing.text.BadLocationException err) {
err.printStackTrace();
}
}
}
/**
* Écrit un tableau de bytes dans le JTextArea. Scrolling du JTextArea à
* la fin du texte ajouté.
*
* @see java.io.OutputStream#write(byte[])
*/
public final void write(byte[] arg0) throws IOException {
String txt = new String(arg0);
m_textArea.append(txt);
try {
m_textArea.scrollRectToVisible(m_textArea
.modelToView(m_textArea.getDocument().getLength()));
} catch (javax.swing.text.BadLocationException err) {
err.printStackTrace();
}
}
}
} |
Partager