This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
swing:start [2015/05/02 12:03] gthanos [Containers & Layout Managers] |
swing:start [2018/03/27 20:56] gthanos removed |
||
|---|---|---|---|
| Line 16: | Line 16: | ||
| ===== Border Layout Manager ===== | ===== Border Layout Manager ===== | ||
| - | Η διάταξη ενός παραθύρου με BorderLayout manager φαίνεται στο παρακάτω σχήμα. | + | Η διάταξη ενός παραθύρου με [[http:// |
| - | {{ : | + | {{ : |
| - | ===== Flow Layout | + | Όπως φαίνεται και από το σχήμα ο 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 | ||
| + | JButton northButton | ||
| + | northButton.addActionListener( listener ); | ||
| + | add(northButton, | ||
| + | |||
| + | JButton southButton | ||
| + | southButton.addActionListener( listener ); | ||
| + | add(southButton, | ||
| + | |||
| + | JButton westButton | ||
| + | westButton.addActionListener( listener ); | ||
| + | add(westButton, | ||
| + | |||
| + | JButton eastButton | ||
| + | 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 | ||
| + | |||
| + | Όπως προαναφέραμε ο // | ||
| + | |||
| + | <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 ===== | ===== Grid Layout Manager ===== | ||
| + | Ο //Grid Layout Manager// τοποθετεί τα στοιχεία με την σειρά σε ένα πλέγμα (grid) από γραμμές και στήλες. Με την χρήση του //Grid Layout Manager// όλες οι θέσεις του πλέγματος έχουν το ίδιο μέγεθος. Για παράδειγμα, | ||
| + | <code java> | ||
| + | setLayout(new GridLayout(2, | ||
| + | </ | ||
| + | |||
| + | Από το παρακάτω σχήμα θα πρέπει να έχετε υπόψη σας ότι οι γραμμές το πλέγματος δεν είναι ορατές και ότι τα στοιχεία που περιέχονται σε αυτές " | ||
| + | |||
| + | Αν και μπορείτε να προσδιορίσετε τον αριθμό γραμμών και στηλών του πλέγματος, | ||
| + | <code java> | ||
| + | setLayout(new GridLayout(2, | ||
| + | ή | ||
| + | setLayout(new GridLayout(2, | ||
| + | </ | ||
| + | Αν θέλετε να __κλειδώσετε τον αριθμό των στηλών__ και να παίξετε με τον αριθμό των γραμμών μπορείτε να μηδενίσετε τον αριθμό των γραμμών και να προσδιορίσετε τον αριθμό των στηλών που επιθυμείτε. Σε αυτή την περίπτωση ο αριθμός των στηλών παραμένει σταθερός και ο αριθμός των γραμμών αυξάνει, | ||
| + | <code java> | ||
| + | setLayout(new GridLayout(0, | ||
| + | </ | ||
| + | |||
| + | Παρακάτω δίνεται ένα παράδειγμα χρήσης του //Grid Layout Manager//: | ||
| + | <code java GridLayoutDemo.java> | ||
| + | import javax.swing.*; | ||
| + | import java.awt.event.ActionListener; | ||
| + | import java.awt.event.ActionEvent; | ||
| + | import java.awt.*; | ||
| + | |||
| + | public class GridLayoutDemo extends JFrame { | ||
| + | public GridLayoutDemo () { | ||
| + | super(); | ||
| + | setSize(400, | ||
| + | setTitle(" | ||
| + | setLayout(new GridLayout(3, | ||
| + | setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); | ||
| + | | ||
| + | ButtonListener listener = new ButtonListener(); | ||
| + | JButton close1 = new JButton(" | ||
| + | close1.addActionListener( listener ); | ||
| + | add(close1); | ||
| + | | ||
| + | JButton close2 = new JButton(" | ||
| + | close2.addActionListener( listener ); | ||
| + | add(close2); | ||
| + | | ||
| + | JButton close3 = new JButton(" | ||
| + | close3.addActionListener( listener ); | ||
| + | add(close3); | ||
| + | | ||
| + | JButton close4 = new JButton(" | ||
| + | close4.addActionListener( listener ); | ||
| + | add(close4); | ||
| + | | ||
| + | JButton close5 = new JButton(" | ||
| + | close5.addActionListener( listener ); | ||
| + | add(close5); | ||
| + | | ||
| + | JButton close6 = new JButton(" | ||
| + | close6.addActionListener( listener ); | ||
| + | add(close6); | ||
| + | | ||
| + | JButton close7 = new JButton(" | ||
| + | close7.addActionListener( listener ); | ||
| + | add(close7); | ||
| + | } | ||
| + | | ||
| + | public class ButtonListener implements ActionListener { | ||
| + | public void actionPerformed(ActionEvent e) { | ||
| + | System.out.println(e); | ||
| + | System.exit(1); | ||
| + | } | ||
| + | } | ||
| + | | ||
| + | public static void main(String[] args) { | ||
| + | GridLayoutDemo w = new GridLayoutDemo(); | ||
| + | w.setVisible(true); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | {{ : | ||
| ===== Box Layout Manager ===== | ===== Box Layout Manager ===== | ||
| + | Ο //Box Layout Manager// καταχωρεί όλα τα στοιχεία κατακόρυφα ή κάθετα ανάλογα με την παράμετρο που περνάμε στον κατασκευαστή της κλάσης // | ||
| + | * X_AXIS | ||
| + | * Υ_AXIS | ||
| + | * LINE_AXIS (ανάλογο του X_AXIS) | ||
| + | * PAGE_AXIS (ανάλογο του Y_AXIS) | ||
| + | |||
| + | Δείτε το παρακάτω παράδειγμα και παίξτε με τον κατασκευαστή της κλάσης // | ||
| + | |||
| + | <code java BoxLayoutDemo.java> | ||
| + | import javax.swing.*; | ||
| + | import java.awt.event.ActionListener; | ||
| + | import java.awt.event.ActionEvent; | ||
| + | import java.awt.*; | ||
| + | |||
| + | public class BoxLayoutDemo extends JFrame { | ||
| + | public BoxLayoutDemo () { | ||
| + | super(); | ||
| + | setSize(150, | ||
| + | setTitle(" | ||
| + | setLayout(new BoxLayout(this.getContentPane(), | ||
| + | setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); | ||
| + | | ||
| + | ButtonListener listener = new ButtonListener(); | ||
| + | JButton close1 = new JButton(" | ||
| + | close1.addActionListener( listener ); | ||
| + | add(close1); | ||
| + | | ||
| + | JButton close2 = new JButton(" | ||
| + | close2.addActionListener( listener ); | ||
| + | add(close2); | ||
| + | | ||
| + | JButton close3 = new JButton(" | ||
| + | close3.addActionListener( listener ); | ||
| + | add(close3); | ||
| + | | ||
| + | JButton close4 = new JButton(" | ||
| + | close4.addActionListener( listener ); | ||
| + | add(close4); | ||
| + | | ||
| + | JButton close5 = new JButton(" | ||
| + | close5.addActionListener( listener ); | ||
| + | add(close5); | ||
| + | | ||
| + | JButton close6 = new JButton(" | ||
| + | close6.addActionListener( listener ); | ||
| + | add(close6); | ||
| + | | ||
| + | JButton close7 = new JButton(" | ||
| + | close7.addActionListener( listener ); | ||
| + | add(close7); | ||
| + | } | ||
| + | | ||
| + | public class ButtonListener implements ActionListener { | ||
| + | public void actionPerformed(ActionEvent e) { | ||
| + | System.out.println(e); | ||
| + | System.exit(1); | ||
| + | } | ||
| + | } | ||
| + | | ||
| + | public static void main(String[] args) { | ||
| + | BoxLayoutDemo w = new BoxLayoutDemo(); | ||
| + | w.setVisible(true); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | | {{ : | ||
| ===== Group Layout Manager ===== | ===== Group Layout Manager ===== | ||
| + | |||
| + | Πρόκειται για ένα σύνθετο layout το οποίο χρησιμοποιείται από εργαλεία IDE, όπως το NetBeans και το Eclipse. Αν και δείνει μεγαλύτερη ευελιξία σε σχέση με τα προηγούμενα layouts το προγραμματιστικό μοντέλο είναι αρκετά σύνθετο και δεν θα εξεταστεί περαιτέρω. | ||