Advertisement
makispaiktis

Toolbar (Swing)

Feb 19th, 2019 (edited)
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1. import java.awt.FlowLayout;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5. import javax.swing.BorderFactory;
  6. import javax.swing.JButton;
  7. import javax.swing.JPanel;
  8.  
  9. public class Toolbar extends JPanel implements ActionListener{
  10.    
  11.     // Variables-components
  12.     private JButton helloButton;
  13.     private JButton goodbyeButton;
  14.     private StringListener textListener;                        // ΟΡΙΖΩ ΝΕΑ ΜΕΤΑΒΛΗΤΗ ΤΥΠΟΥ TextPanel, την textPanelHere, στην οποία θα ανατεθεί η γενική μεταβλητή/component textPanel
  15.    
  16.     // Constructor
  17.     public Toolbar() {
  18.        
  19.         setBorder(BorderFactory.createEtchedBorder());
  20.         // Initialize the variables
  21.         helloButton = new JButton("Hello");
  22.         goodbyeButton = new JButton("Goodbye");
  23.        
  24.         // Listen to the buttons
  25.         helloButton.addActionListener(this);                // Katalavainei otan patietai to koumpi
  26.         goodbyeButton.addActionListener(this);
  27.        
  28.         // Create the layout and add the 2 buttons
  29.         setLayout(new FlowLayout());
  30.         add(helloButton);
  31.         add(goodbyeButton);
  32.        
  33.     }
  34.    
  35.     // Methods
  36.     public void setStringListener(StringListener listener) {
  37.         this.textListener = listener;
  38.     }
  39.  
  40.     // Unimplemented Method
  41.     @Override
  42.     public void actionPerformed(ActionEvent e) {
  43.         JButton clicked = (JButton)e.getSource();       // Orizw ena JButton pou diavazei to source (typou JButton) tou event(patima koumpiou) e
  44.                                                 // diladi to clicked ginetai (san timi) h helloButton h goodbyeButton
  45.         if(clicked == helloButton) {
  46.             //System.out.println("Hello (console)");                        // Τι γίνεται σε κονσόλα και JFrame, όταν πατήσω το κουμπί
  47.             if(textListener != null) {
  48.                 textListener.textEmitted("Hello\n");
  49.             }
  50.         }
  51.         else {
  52.             //System.out.println("Goodbye (console)");
  53.             if(textListener != null) {
  54.                 textListener.textEmitted("Goodbye\n");
  55.             }
  56.         }
  57.    
  58.     }
  59.    
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement