swing:start
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
swing:start [2015/05/02 13:27] – [FlowLayout Manager] gthanos | swing:start [2018/03/27 20:56] (current) – removed gthanos | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Containers & Layout Managers ====== | ||
- | |||
- | Μέχρι τώρα είδαμε πως μπορούμε να προσθέσουμε τα components buttons και labels σε ένα JFrame. Αν και έχουμε την δυνατότητα να προσθέσουμε πολλαπλά components δεν έχουμε καμία ένδειξη για το πως αυτά θα διαταχθούν μέσα στο Frame. Προκειμένου να ορίσετε τον τρόπο διάταξης των components θα χρειαστείτε ένα **layout manager**, ο οποίος διατάσει τα επιμέρους components μέσα στον χώρο. | ||
- | |||
- | Συνοπτικά οι layout managers είναι οι εξής: | ||
- | * **Border Layout Manager:** ο Border Layout Manager είναι ο default layout manager όταν δεν ορίζεται κάποιος άλλος. | ||
- | * **Grid Layout Manager:** ο Grid Layout Manager διατάσει τα στοιχεία σε ένα πίνακα Ν γραμμών και Μ στηλών | ||
- | * **Flow Layout Manager**: ο Flow Layout Manager διατάσει τα στοιχεία με την σειρά το ένα δίπλα στο άλλο. Εάν τα στοιχεία δεν χωράνε στην ίδια γραμμή τότε συνεχίζει στην επόμενη γραμμή. | ||
- | * **Box Layout Manager**: προσθέτει τα στοιχεία σε μία γραμμή ή σε μία στήλη. | ||
- | * **Group Layout Manager:** χρησιμοποιείται από GUIs όπως NetBeans και Eclipse και δεν θα μας απασχολήσει. | ||
- | |||
- | <WRAP important 80% center round> | ||
- | Κάθε //layout manager// χωρίζει το παράθυρο σε περιοχές. Αυτό που θα πρέπει να προσέξετε είναι ότι μπορείτε να προσθέσετε μόνο ένα component σε κάθε περιοχή. Αν προσπαθήσετε να προσθέσετε περισσότερα του ενός, αυτό που θα γίνει είναι ότι θα αποθηκευθεί το τελευταίο που προσθέσατε. Σε επόμενη ενότητα θα δούμε πως μπορείτε να αποθηκεύετε περισσότερα του ενός components σε μία περιοχή με την χρήση των panels (JPanel). | ||
- | </ | ||
- | |||
- | ===== Border Layout Manager ===== | ||
- | |||
- | Η διάταξη ενός παραθύρου με [[http:// | ||
- | |||
- | {{ : | ||
- | |||
- | Όπως φαίνεται και από το σχήμα ο BorderLayout manager έχει 5 περιοχές **NORTH**, **SOUTH**, **WEST**, **CENTER**, **EAST**. Δείτε το παρακάτω πρόγραμμα το οποίο χρησιμοποιεί ένα παράθυρο με 5 κουμπιά, | ||
- | |||
- | <code java BorderLayoutDemo.java> | ||
- | import javax.swing.*; | ||
- | import java.awt.event.ActionListener; | ||
- | import java.awt.event.ActionEvent; | ||
- | import java.awt.*; | ||
- | |||
- | public class BorderLayoutDemo extends JFrame { | ||
- | public BorderLayoutDemo () { | ||
- | super(); | ||
- | setSize(400, | ||
- | setTitle(" | ||
- | setLayout(new BorderLayout()); | ||
- | setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); | ||
- | | ||
- | ButtonListener listener = new ButtonListener(); | ||
- | JButton northButton = new JButton(" | ||
- | northButton.addActionListener( listener ); | ||
- | add(northButton, | ||
- | | ||
- | JButton southButton = new JButton(" | ||
- | southButton.addActionListener( listener ); | ||
- | add(southButton, | ||
- | | ||
- | JButton westButton = new JButton(" | ||
- | westButton.addActionListener( listener ); | ||
- | add(westButton, | ||
- | | ||
- | JButton eastButton = new JButton(" | ||
- | eastButton.addActionListener( listener ); | ||
- | add(eastButton, | ||
- | | ||
- | JButton centerButton = new JButton(" | ||
- | centerButton.addActionListener( listener ); | ||
- | add(centerButton, | ||
- | } | ||
- | | ||
- | public class ButtonListener implements ActionListener { | ||
- | public void actionPerformed(ActionEvent e) { | ||
- | System.out.println(e); | ||
- | System.exit(1); | ||
- | } | ||
- | } | ||
- | | ||
- | public static void main(String[] args) { | ||
- | BorderLayoutDemo w = new BorderLayoutDemo(); | ||
- | w.setVisible(true); | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | Τι θα γίνει αν αλλάξετε την περιοχή στην οποία θα τοποθετηθούν όλα τα buttons, ώστε να είναι η περιοχή // | ||
- | |||
- | {{ : | ||
- | |||
- | ===== FlowLayout Manager ===== | ||
- | |||
- | Όπως προαναφέραμε ο // | ||
- | |||
- | <code java FlowLayoutDemo.java> | ||
- | import javax.swing.*; | ||
- | import java.awt.event.ActionListener; | ||
- | import java.awt.event.ActionEvent; | ||
- | import java.awt.*; | ||
- | |||
- | public class FlowLayoutDemo extends JFrame { | ||
- | public FlowLayoutDemo () { | ||
- | super(); | ||
- | setSize(300, | ||
- | setTitle(" | ||
- | | ||
- | /* Select FlowLayout.LEFT for left alignment. | ||
- | * Select FlowLayout.RIGHT for right alignment. | ||
- | * Select FlowLayout.CENTER for center alignment (this is the default). | ||
- | */ | ||
- | setLayout(new FlowLayout(FlowLayout.LEFT)); | ||
- | setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); | ||
- | | ||
- | ButtonListener listener = new ButtonListener(); | ||
- | JButton northButton = new JButton(" | ||
- | northButton.addActionListener( listener ); | ||
- | add(northButton); | ||
- | | ||
- | JButton southButton = new JButton(" | ||
- | southButton.addActionListener( listener ); | ||
- | add(southButton); | ||
- | | ||
- | JButton westButton = new JButton(" | ||
- | westButton.addActionListener( listener ); | ||
- | add(westButton); | ||
- | | ||
- | JButton eastButton = new JButton(" | ||
- | eastButton.addActionListener( listener ); | ||
- | add(eastButton); | ||
- | | ||
- | JButton centerButton = new JButton(" | ||
- | centerButton.addActionListener( listener ); | ||
- | add(centerButton); | ||
- | } | ||
- | | ||
- | public class ButtonListener implements ActionListener { | ||
- | public void actionPerformed(ActionEvent e) { | ||
- | System.out.println(e); | ||
- | System.exit(1); | ||
- | } | ||
- | } | ||
- | | ||
- | public static void main(String[] args) { | ||
- | FlowLayoutDemo w = new FlowLayoutDemo(); | ||
- | w.setVisible(true); | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | Το παράθυρο που προκύπτει δίνεται παρακάτω. Δοκιμάστε να αλλάξετε την στοίχιση σε δεξιά ή κεντρική μεταβάλλοντας την γραμμή '' | ||
- | *'' | ||
- | *'' | ||
- | |||
- | {{ : | ||
- | |||
- | ===== Grid Layout Manager ===== | ||
- | |||
- | ===== Box Layout Manager ===== | ||
- | |||
- | ===== Group Layout Manager ===== | ||
- | |||
- | |||
- | |||
- | |||
swing/start.1430573230.txt.gz · Last modified: 2015/05/02 12:27 (external edit)