Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.FlowLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.BorderFactory;
- import javax.swing.JButton;
- import javax.swing.JPanel;
- public class Toolbar extends JPanel implements ActionListener{
- // Variables-components
- private JButton helloButton;
- private JButton goodbyeButton;
- private StringListener textListener; // ΟΡΙΖΩ ΝΕΑ ΜΕΤΑΒΛΗΤΗ ΤΥΠΟΥ TextPanel, την textPanelHere, στην οποία θα ανατεθεί η γενική μεταβλητή/component textPanel
- // Constructor
- public Toolbar() {
- setBorder(BorderFactory.createEtchedBorder());
- // Initialize the variables
- helloButton = new JButton("Hello");
- goodbyeButton = new JButton("Goodbye");
- // Listen to the buttons
- helloButton.addActionListener(this); // Katalavainei otan patietai to koumpi
- goodbyeButton.addActionListener(this);
- // Create the layout and add the 2 buttons
- setLayout(new FlowLayout());
- add(helloButton);
- add(goodbyeButton);
- }
- // Methods
- public void setStringListener(StringListener listener) {
- this.textListener = listener;
- }
- // Unimplemented Method
- @Override
- public void actionPerformed(ActionEvent e) {
- JButton clicked = (JButton)e.getSource(); // Orizw ena JButton pou diavazei to source (typou JButton) tou event(patima koumpiou) e
- // diladi to clicked ginetai (san timi) h helloButton h goodbyeButton
- if(clicked == helloButton) {
- //System.out.println("Hello (console)"); // Τι γίνεται σε κονσόλα και JFrame, όταν πατήσω το κουμπί
- if(textListener != null) {
- textListener.textEmitted("Hello\n");
- }
- }
- else {
- //System.out.println("Goodbye (console)");
- if(textListener != null) {
- textListener.textEmitted("Goodbye\n");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement