This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
swing:start [2015/05/02 13:00] gthanos |
swing:start [2015/05/02 13:26] gthanos [FlowLayout Manager] |
||
---|---|---|---|
Line 76: | Line 76: | ||
{{ :swing:borderlayoutdemo.png?400 |}} | {{ :swing:borderlayoutdemo.png?400 |}} | ||
- | ===== Flow Layout Manager ===== | + | ===== FlowLayout Manager ===== |
+ | |||
+ | Όπως προαναφέραμε ο //FlowLayout Manager// εμφανίζει τα στοιχεία με τη σειρά που προστίθενται σε μία σειρά από αριστερά προς τα δεξιά. Αν το μέγεθος του παραθύρου δεν επαρκεί η τοποθέτηση των στοιχείων συνεχίζει και στην επόμενη σειρά. Δείτε το παραπάνω παράδειγμα κώδικα παραλλαγμένο ώστε να χρησιμοποιεί //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, 150); | ||
+ | setTitle("FlowLayout Demo"); | ||
+ | |||
+ | /* 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("North Button"); | ||
+ | northButton.addActionListener( listener ); | ||
+ | add(northButton); | ||
+ | |||
+ | JButton southButton = new JButton("South Button"); | ||
+ | southButton.addActionListener( listener ); | ||
+ | add(southButton); | ||
+ | |||
+ | JButton westButton = new JButton("West Button"); | ||
+ | westButton.addActionListener( listener ); | ||
+ | add(westButton); | ||
+ | |||
+ | JButton eastButton = new JButton("East Button"); | ||
+ | eastButton.addActionListener( listener ); | ||
+ | add(eastButton); | ||
+ | |||
+ | JButton centerButton = new JButton("Center Button"); | ||
+ | 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); | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | Το παράθυρο που προκύπτει δίνεται παρακάτω. Δοκιμάστε να αλλάξετε την στοίχιση σε δεξιά ή κεντρική μεταβάλλοντας την γραμμή ''setLayout(new FlowLayout(FlowLayout.LEFT)); '' σε ''setLayout(new FlowLayout(FlowLayout.RIGHT)); '' ή ''setLayout(new FlowLayout(FlowLayout.CENTER));'' | ||
+ | |||
+ | {{ :swing:flowlayoutdemo.png |}} | ||
===== Grid Layout Manager ===== | ===== Grid Layout Manager ===== |