public boolean comparer() { return jTextField1.getText().equals(undoStack.lastElement().oldtext1); }
public int comparer() { return jTextField1.getText().compareTo(undoStack.lastElement().oldtext1); }
Code : //******************************************* //Fichier GUIState.java //******************************************* package mybudgetingtool; public class GUIState { //****************************************************** // Déclarations des variables pour la sauvegarde des TextsFields : String oldtext1, oldtext2, oldtext3; String oldtext4, oldtext5, oldtext6; String oldtext7, oldtext8, oldtext9; //****************************************************** // Déclarations des variables pour la sauvegarde des ComboBox : int oldBox1, oldBox2, oldBox3, oldBox4, oldBox5, oldBox6; //******** // Constructeur : public GUIState(budgetingtool g) { oldtext1 = g.jTextField1.getText(); oldtext2 = g.jTextField2.getText(); oldtext3 = g.jTextField3.getText(); oldtext4 = g.jTextField4.getText(); oldtext5 = g.jTextField5.getText(); oldtext6 = g.jTextField6.getText(); oldtext7 = g.jTextField7.getText(); oldtext8 = g.jTextField8.getText(); oldtext9 = g.jTextField9.getText(); oldBox1 = g.jComboBox1.getSelectedIndex(); oldBox2 = g.jComboBox2.getSelectedIndex(); oldBox3 = g.jComboBox3.getSelectedIndex(); oldBox4 = g.jComboBox4.getSelectedIndex(); oldBox5 = g.jComboBox5.getSelectedIndex(); oldBox6 = g.jComboBox6.getSelectedIndex(); } //Méthode : public void setGUI(budgetingtool g) { g.jTextField1.setText(oldtext1); g.jTextField2.setText(oldtext2); g.jTextField3.setText (oldtext3); g.jTextField4.setText(oldtext4); g.jTextField5.setText(oldtext5); g.jTextField6.setText(oldtext6); g.jTextField7.setText(oldtext7); g.jTextField8.setText( oldtext8); g.jTextField9.setText(oldtext9); g.jComboBox1.setSelectedIndex(oldBox1); g.jComboBox2.setSelectedIndex(oldBox2); g.jComboBox3.setSelectedIndex(oldBox3); g.jComboBox4.setSelectedIndex(oldBox4); g.jComboBox5.setSelectedIndex(oldBox5); g.jComboBox6.setSelectedIndex(oldBox6); } public void setinitializeGUI(budgetingtool g) { g.jTextField1.setText("0.00"); g.jTextField2.setText("0.00"); g.jTextField3.setText("0.00"); g.jTextField4.setText("0.00"); g.jTextField5.setText("0.00"); g.jTextField6.setText("0.00"); g.jTextField7.setText("0.00"); g.jTextField8.setText("0.00"); g.jTextField9.setText("0.00"); g.jComboBox1.setSelectedIndex(0); g.jComboBox2.setSelectedIndex(0); g.jComboBox3.setSelectedIndex(0); g.jComboBox4.setSelectedIndex(0); g.jComboBox5.setSelectedIndex(0); g.jComboBox6.setSelectedIndex(0); } } //******************************************* // Fichier budgetingtool.java //******************************************* //utilisation d'un TextField private void jTextField1FocusLost(java.awt.event.FocusEvent evt) { Calculate(); if(jTextField1.getText().equals(undoStack.lastElement().oldtext1)) { JOptionPane.showMessageDialog(this," pas d'élément à ajouter pour textField1"); } else { addElementStack(); JOptionPane.showMessageDialog(this,undoStack.lastElement().oldtext1 +" élément 1 ajouté"); } } //******************************************************** Stack<GUIState> undoStack=new Stack<GUIState>(); Stack<GUIState> redoStack=new Stack<GUIState>(); undoStack.push(new GUIState(this)); redoStack.push(new GUIState(this)); private void addElementStack() { //Get GUI State : oldState = new GUIState(this); //put on undoStack undoStack.push(oldState); } private void restoreundoState() { undoStack.pop(); oldState=undoStack.peek(); oldState.setGUI(this); oldState=new GUIState(this); redoStack.push(oldState); } private void restoreredoState() { redoStack.pop(); oldState=redoStack.peek(); oldState.setGUI(this); addElementStack(); }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question//************************************************ // Bouton UNDO : //************************************************ private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { restoreundoState(); } //************************************************ // Bouton REDO : //************************************************ private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { restoreredoState(); }