I have a some issue: For my master work, i need to implement a feedback system. The good thing is that what the programm do, does not need to interest, there are only 2 events where i must give the feedback when they occur. user work something push the "save" Button and ...here comes my issue: i need give a feedback about the number of his clicks. On the push "save" event it must happen: 1) update variable with click number 2) give a feedback To get a idea, here the very simple simulation: Code: import javax.swing.*; import java.awt.Graphics; import java.awt.Image; import java.awt.event.*; import java.awt.image.ImageObserver; public class fensterTest2 implements ActionListener{ JButton button; private int anzahlLocClicks = 10; private int anzahlGlobalClicks = 10; private int prozentAnzeige; public static void main(String args[]){ fensterTest2 gui = new fensterTest2(); gui.startEvent(); } public void startEvent(){ JFrame displayFenster = new JFrame(); // hier definiere ich mein fenster, unten die eigenschaften button = new JButton("Start Mein Event"); button.addActionListener(this); //registriere bei Button, werde in Liste aufgenommen -> // Argument MUSS objekt Klasse die ActionListener implementiert displayFenster.getContentPane().add(button); displayFenster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); displayFenster.setSize(600,300); displayFenster.setVisible(true); } public void actionPerformed(ActionEvent event){ button.setText("You are "+ prozentAnzeige + "% better as the Rest!!!!"); prozentAnzeige = (anzahlLocClicks*100)/anzahlGlobalClicks; anzahlLocClicks++; anzahlGlobalClicks = anzahlGlobalClicks+3; button.revalidate(); } I don´t know how to make from my one event (user click on the "save" button) two events: 1)one that updates my variable 2) one who give the feedback out.
I can not post pictures from my concept here ..." Too many live links/images found in your post content. Please edit your post or contact the administrator." End version of my program: update variable and feedback using an feedback interface, so that the administrator can chosse wich feedback the user get displayed. needed to control the amout of information wich the user receives Exemples of interface and feedback clases Interface: Code: public interface ShowFeedback { public void feedback(); } No information only gratitude: Code: public class ShowFeedbackGratitude implements ShowFeedback, FeedbackObserver { private int anzahlGlobalClicks; private int anzahlLocClicks; private FeedbackSimulatorInterface feedbackSimulator; public ShowFeedbackGratitude(FeedbackSimulatorInterface feedbackSimulator){ this.feedbackSimulator = feedbackSimulator; feedbackSimulator.registerFeedbackObserver(this); } public void update(int anzahlGlobalClicks, int anzahlLocClicks ){ feedback(); } public void feedback(){ System.out.println("Vielen Dank für Ihren Beitrag!"); } } User get only history of his clicks: Code: public class ShowFeedbackHistory implements ShowFeedback, FeedbackObserver{ private int anzahlLocClicks; private int anzahlGlobalClicks; private FeedbackSimulatorInterface feedbackSimulator; public ShowFeedbackHistory(FeedbackSimulatorInterface feedbackSimulator){ this.feedbackSimulator = feedbackSimulator; feedbackSimulator.registerFeedbackObserver(this); } public void update(int anzahlLocClicks, int anzahlGlobalClicks){ this.anzahlLocClicks = anzahlLocClicks; feedback(); } public void feedback(){ System.out.println("Vielen Dank, Sie haben bereits " + anzahlLocClicks + " Beiträge"); } } and here the user get a relative History of his clicks, anzahlGlobalClicks -> summ of clicks of all users, anzahlLocClicks -> only the users clicks Code: public class ShowFeedbackRelative1 implements ShowFeedback, FeedbackObserver { private int anzahlLocClicks; private int anzahlGlobalClicks; private FeedbackSimulatorInterface feedbackSimulator; public ShowFeedbackRelative1(FeedbackSimulatorInterface feedbackSimulator){ this.feedbackSimulator = feedbackSimulator; feedbackSimulator.registerFeedbackObserver(this); } public void update(int anzahlLocClicks, int anzahlGlobalClicks ){ this.anzahlLocClicks = anzahlLocClicks; this.anzahlGlobalClicks = anzahlGlobalClicks; feedback(); } public void feedback(){ int relativeBeiträge; relativeBeiträge = (anzahlLocClicks * 100) / anzahlGlobalClicks; System.out.println("Vielen Dank, sie haben mehr Beiträge als " + relativeBeiträge+ "% der Teilnehmer"); } }
Very short version of my issue: How create a Listener that can register 2 objects, and call their methods (feedback, update variable) when the click on "save" button event occurs
i hope this example is what you want Code: import javax.swing.*; import java.awt.event.*; public class SaveButton extends JFrame{ private FirstClass firstClass; private SecondClass secondClass; public SaveButton() { firstClass=new FirstClass(); secondClass=new SecondClass(firstClass); setDefaultCloseOperation(EXIT_ON_CLOSE); JButton saveButton=new JButton("SAVE"); saveButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event){ secondClass.update(); secondClass.feedBack(); } }); add(saveButton); setSize(200,200); setVisible(true); } public static void main(String args[]){ new SaveButton(); } class FirstClass{ private int number=0; public void update(){ number++; } public int times(){ return number; } } class SecondClass{ private FirstClass firstClass; public SecondClass(FirstClass first){ firstClass=first; } public void feedBack(){ System.out.println("you pressed save button "+firstClass.times()+" times!!!"); } public void update(){ firstClass.update(); } } }