Java GUI – JButton

Σε αυτό το άρθρο, θα εξετάσουμε ένα κύριο component το JButton. Επιτρέπει στους χρήστες να εκτελούν ενέργειες, από την υποβολή φορμών μέχρι το άνοιγμα νέων παραθύρων.

Τι είναι ένα JButton;

Το JButton βασικά είναι ένα κουμπί στην οθόνη. Ο χρήστης μπορεί να αλληλεπιδράσει με αυτό κάνοντας κλικ, ενεργοποιώντας μια συγκεκριμένη ενέργεια στην εφαρμογή.

Τα κουμπιά μπορούν να εμφανίζουν κείμενο, εικονίδια ή και τα δύο, καθιστώντας τα προσαρμόσιμα σε διάφορες περιπτώσεις χρήσης, από κουμπιά “OK” και “Άκυρο” έως πιο σύνθετα στοιχεία.

Δημιουργία JButton

Για να φτοιάξουμε ένα JButton, το πρώτο βήμα είναι η δημιουργία ενός αντικειμένου της κλάσης javax.swing.JButton. Η κλάση JButton παρέχει διάφορους constructors:

  • JButton(): Δημιουργεί ένα άδειο κουμπί, χωρίς αρχικό κείμενο ή εικόνα.
JButton emptyButton = new JButton();
emptyButton.setText("Πατήστε με!");
  • JButton(String text):  Αυτός είναι ο συνηθισμένος τρόπος για τη δημιουργία κουμπιών.
JButton submitButton = new JButton("Υποβολή");
  • Button(Icon icon): Δημιουργεί ένα κουμπί που εμφανίζει μια εικόνα, αυτό είναι χρήσιμο για κουμπιά στην toolbar ή κουμπιά που εμφανίζουν μόνο εικονίδιο.
ImageIcon saveIcon = new ImageIcon("save.png");
JButton saveButton = new JButton(saveIcon);
  • JButton(String text, Icon icon): Δημιουργεί ένα κουμπί που εμφανίζει ταυτόχρονα κείμενο και εικονίδιο.

Μπορούμε να ρυθμίσουμε τη θέση του κειμένου και του εικονιδίου

ImageIcon printIcon = new ImageIcon("print.png");
JButton printButton = new JButton("Εκτύπωση", printIcon);

JButton: Event Handling

Ένα κουμπί από μόνο του δεν εκτελεί καμία ενέργεια. Για να γίνει λειτουργικό, πρέπει να προσθέσουμε μηχανισμούς που “ακούνε” το κλικ. Αυτό επιτυγχάνεται μέσω του Event Handling.

  1. Υλοποίηση του ActionListener: Ο πιο συνηθισμένος τρόπος για να χειριστείτε ένα κλικ είναι να κάνετε μια κλάση να υλοποιεί το interface java.awt.event.ActionListener. Αυτό απαιτεί την υλοποίηση της μεθόδου actionPerformed(ActionEvent e), η οποία καλείται όταν συμβεί το κλικ.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyButtonListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        // Κώδικας που εκτελείται όταν πατηθεί το κουμπί
        System.out.println("Hello!");
    }
}
  1.  Προσθήκη του Listener στο Κουμπί: Στη συνέχεια, ένα αντικείμενο της κλάσης που υλοποιεί το ActionListener πρέπει να προστεθεί στο JButton χρησιμοποιώντας τη μέθοδο addActionListener().
JButton myButton = new JButton("Click Me");
MyButtonListener listener = new MyButtonListener();
myButton.addActionListener(listener);

Εναλλακτικά, μπορεί να χρησιμοποιηθεί μια εσωτερική κλάση, ειδικά όταν ο listener χρησιμοποιείται μόνο για ένα κουμπί:

JButton anotherButton = new JButton("Click Me");
anotherButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Και αυτό το κουμπί πατήθηκε!");
    }
});

Βασικές λειτουργίες και ρυθμίσεις του JButton.

Η κλάση JButton παρέχει πολλές μεθόδους για την τροποποίηση της εμφάνισης και της συμπεριφοράς των κουμπιών:

  • setText(String text): Ορίζει το κείμενο που εμφανίζεται στο κουμπί.
  • getText(): Επιστρέφει το τρέχον κείμενο του κουμπιού.
  • setIcon(Icon icon): Ορίζει την εικόνα που εμφανίζεται στο κουμπί.
  • getIcon(): Επιστρέφει την τρέχουσα εικόνα του κουμπιού.
  • setEnabled(boolean b): Ενεργοποιεί (true) ή απενεργοποιεί (false) το κουμπί. Ένα απενεργοποιημένο κουμπί είναι συνήθως γκριζαρισμένο) και δεν ανταποκρίνεται σε κλίκ.
  • isSelected() / setSelected(boolean b): Χρησιμοποιείται για κουμπιά που μπορούν να έχουν δύο καταστάσεις (επιλεγμένο/μη επιλεγμένο), αν και για τέτοιες περιπτώσεις είναι πιο συνηθισμένη η χρήση του JToggleButton.
  • setFont(Font font): Ορίζει τη γραμματοσειρά του κειμένου.
  • setForeground(Color color): Ορίζει το χρώμα του κειμένου.
  • setBackground(Color color): Ορίζει το χρώμα φόντου.
  • setToolTipText(String text): Ορίζει ένα επεξηγηματικό κείμενο που εμφανίζεται όταν ο χρήστης τοποθετεί τον δείκτη του ποντικιού πάνω από το κουμπί.

Πότε να χρησιμοποιήσετε ένα JButton;

Η χρήση του JButton είναι ξεκάθαρη: κάθε φορά που απαιτείται από τον χρήστη να εκτελέσει μια ενέργεια στην εφαρμογή μέσω ενός κλικ. Παραδείγματα περιλαμβάνουν:

  • Υποβολή δεδομένων σε φόρμες.
  • Άνοιγμα νέων παραθύρων ή διαλόγων.
  • Εκτέλεση λειτουργιών όπως εκτύπωση, αποθήκευση ή διαγραφή.
  • Πλοήγηση μεταξύ διαφορετικών τμημάτων της εφαρμογής.

Σημαντικές σκέψεις για τα JButtons.

  • Σαφής επισήμανση: Το κείμενο ή το εικονίδιο ενός κουμπιού πρέπει να είναι επεξηγηματικό και να υποδεικνύει την ενέργεια που θα πραγματοποιηθεί με το κλικ.
  • Συνέπεια: Η οπτική εμφάνιση και η τοποθέτηση των κουμπιών πρέπει να είναι συνεπείς σε όλη την εφαρμογή.
  • Προσβασιμότητα: Για την υποστήριξη χρηστών με ειδικές ανάγκες, μπορεί να οριστεί ένα “accessible name”.

Συμπέρασμα.

Το JButton είναι ένα βασικό component για τη δημιουργία διαδραστικών εφαρμογών. Επιτρέπει την αλληλεπίδραση του χρήστη με την εφαρμογή, κάνοντάς την πιο δυναμική. Η κατανόηση της δημιουργίας, της διαχείρισης γεγονότων και της προσαρμογής των JButton είναι ουσιαστική για κάθε προγραμματιστή.

Αφήστε ένα Σχόλιο

Η ηλ. διεύθυνση σας δεν δημοσιεύεται. Τα υποχρεωτικά πεδία σημειώνονται με *

We use cookies to personalise content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners. View more
Cookies settings
Accept
Decline
Privacy & Cookie policy
Privacy & Cookies policy
Cookie name Active

Who we are

Our website address is: http://stavrosbyte.eu.

Comments

When visitors leave comments on the site we collect the data shown in the comments form, and also the visitor’s IP address and browser user agent string to help spam detection. An anonymized string created from your email address (also called a hash) may be provided to the Gravatar service to see if you are using it. The Gravatar service privacy policy is available here: https://automattic.com/privacy/. After approval of your comment, your profile picture is visible to the public in the context of your comment.

Media

If you upload images to the website, you should avoid uploading images with embedded location data (EXIF GPS) included. Visitors to the website can download and extract any location data from images on the website.

Cookies

If you leave a comment on our site you may opt-in to saving your name, email address and website in cookies. These are for your convenience so that you do not have to fill in your details again when you leave another comment. These cookies will last for one year. If you visit our login page, we will set a temporary cookie to determine if your browser accepts cookies. This cookie contains no personal data and is discarded when you close your browser. When you log in, we will also set up several cookies to save your login information and your screen display choices. Login cookies last for two days, and screen options cookies last for a year. If you select "Remember Me", your login will persist for two weeks. If you log out of your account, the login cookies will be removed. If you edit or publish an article, an additional cookie will be saved in your browser. This cookie includes no personal data and simply indicates the post ID of the article you just edited. It expires after 1 day.

Embedded content from other websites

Articles on this site may include embedded content (e.g. videos, images, articles, etc.). Embedded content from other websites behaves in the exact same way as if the visitor has visited the other website. These websites may collect data about you, use cookies, embed additional third-party tracking, and monitor your interaction with that embedded content, including tracking your interaction with the embedded content if you have an account and are logged in to that website.

Who we share your data with

If you request a password reset, your IP address will be included in the reset email.

How long we retain your data

If you leave a comment, the comment and its metadata are retained indefinitely. This is so we can recognize and approve any follow-up comments automatically instead of holding them in a moderation queue. For users that register on our website (if any), we also store the personal information they provide in their user profile. All users can see, edit, or delete their personal information at any time (except they cannot change their username). Website administrators can also see and edit that information.

What rights you have over your data

If you have an account on this site, or have left comments, you can request to receive an exported file of the personal data we hold about you, including any data you have provided to us. You can also request that we erase any personal data we hold about you. This does not include any data we are obliged to keep for administrative, legal, or security purposes.

Where your data is sent

Visitor comments may be checked through an automated spam detection service.
Save settings
Cookies settings