User Tools

Site Tools


swing:first_program

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision Both sides next revision
swing:first_program [2018/03/21 14:21]
gthanos [Βασικές μέθοδοι της κλάσης JFrame]
swing:first_program [2018/03/22 09:51]
gthanos
Line 6: Line 6:
 import javax.swing.JFrame; import javax.swing.JFrame;
 import javax.swing.JButton; import javax.swing.JButton;
 +import java.awt.event.*;
  
-public class FirstWindow {+public class FirstSwingDemo2 {
   public static final int WIDTH = 300;   public static final int WIDTH = 300;
   public static final int HEIGHT = 200;   public static final int HEIGHT = 200;
Line 17: Line 18:
     firstWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);     firstWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
     JButton endButton = new JButton("Click to end program.");     JButton endButton = new JButton("Click to end program.");
-    EndingListener buttonEar = new EndingListener();+     
 +    ActionListener buttonEar = new ActionListener() 
 +      public void actionPerformed(ActionEvent e) { 
 +        System.exit(0); 
 +      } 
 +    }; 
 +    
     endButton.addActionListener(buttonEar);     endButton.addActionListener(buttonEar);
     firstWindow.add(endButton);     firstWindow.add(endButton);
 +    firstWindow.pack();
     firstWindow.setVisible(true);     firstWindow.setVisible(true);
   }   }
Line 29: Line 37:
       }        } 
     });     });
-  } 
-} 
-</code> 
- 
-<code java EndingListener.java> 
-import java.awt.event.*; 
- 
-public class EndingListener implements ActionListener { 
-  public void actionPerformed(ActionEvent e) { 
-    System.exit(0); 
   }   }
 } }
Line 59: Line 57:
 Στην συνέχεια ορίζουμε ένα αντικείμενο τύπου JButton (κουμπί) με τίτλο "Click to end program." και δημιουργούμε και έναν Listener τον οποίο συνδέουμε με το συγκεκριμένο κουμπί.  Στην συνέχεια ορίζουμε ένα αντικείμενο τύπου JButton (κουμπί) με τίτλο "Click to end program." και δημιουργούμε και έναν Listener τον οποίο συνδέουμε με το συγκεκριμένο κουμπί. 
 <code java> <code java>
-JButton endButton = new JButton("Click to end program."); 
-EndingListener buttonEar = new EndingListener(); 
-endButton.addActionListener(buttonEar); 
-</code> 
- 
-Προσθέτουμε το κουμπί στο παράθυρο και ορίζουμε ότι το παράθυρο που φτιάξαμε θέλουμε να είναι ορατό. 
-<code java> 
-firstWindow.add(endButton); 
-firstWindow.setVisible(true); 
-</code> 
- 
-===== Χρήση μίας ανώνυμης κλάσης ActionListener σε αντικατάσταση της κλάσης EndingListener ===== 
- 
-Για να μην έχουμε πολλαπλά αρχεία, μία παραλλαγή των παραπάνω είναι να τοποθετήσουμε την κλάση EndingListener σε μία ανώνυμη κλάση, ως εξής: 
- 
-<code java FirstSwingDemo2.java> 
-import javax.swing.JFrame; 
-import javax.swing.JButton; 
-import java.awt.event.*; 
- 
-public class FirstSwingDemo2 { 
-  public static final int WIDTH = 300; 
-  public static final int HEIGHT = 200; 
-   
-  public static void showGUI() { 
-    JFrame firstWindow = new JFrame(); 
-    firstWindow.setSize(WIDTH, HEIGHT); 
-    firstWindow.setTitle("First Window Program!"); 
-    firstWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
     JButton endButton = new JButton("Click to end program.");     JButton endButton = new JButton("Click to end program.");
-     
     ActionListener buttonEar = new ActionListener() {     ActionListener buttonEar = new ActionListener() {
       public void actionPerformed(ActionEvent e) {       public void actionPerformed(ActionEvent e) {
         System.exit(0);         System.exit(0);
       }       }
-    }; +    };    
-    +
     endButton.addActionListener(buttonEar);     endButton.addActionListener(buttonEar);
 +</code>
 +
 +Προσθέτουμε το κουμπί στο παράθυρο και ορίζουμε ότι το παράθυρο που φτιάξαμε θέλουμε να είναι ορατό.
 +<code java>
     firstWindow.add(endButton);     firstWindow.add(endButton);
 +    firstWindow.pack();
     firstWindow.setVisible(true);     firstWindow.setVisible(true);
-  } 
-   
-  public static void main(String[] args) { 
-    javax.swing.SwingUtilities.invokeLater(new Runnable() { 
-      public void run() { 
-        showGUI(); 
-      }  
-    }); 
-  } 
-} 
 </code> </code>
  
-===== Παραλλαγή - Το παραπάνω παράθυρο ως αυτόνομη κλάση ===== +Το πρόγραμμα εκκινεί μέσω της στατικής μεθόδου **main** ως εξής: 
- +<code>
-Αν θέλατε να δημιουργήσετε μία κλάση που να υλοποιεί το συγκεκριμένο παράθυρο, θα μπορούσατε να το κάνετε ως εξής: +
- +
-<code java FirstWindow.java> +
-import javax.swing.JFrame; +
-import javax.swing.JButton; +
-import java.awt.event.*; +
- +
-public class FirstWindow extends JFrame { +
-  public static final int WIDTH = 300; +
-  public static final int HEIGHT = 200; +
-   +
-  public static void showGUI() { +
-    JFrame firstWindow = new JFrame(); +
-    firstWindow.setSize(WIDTH, HEIGHT); +
-    firstWindow.setTitle("First Window Program!"); +
-    firstWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); +
-    JButton endButton = new JButton("Click to end program."); +
-    ActionListener buttonEar = new ActionListener() { +
-      public void actionPerformed(ActionEvent e) { +
-        System.exit(0); +
-      } +
-    }; +
-    endButton.addActionListener(buttonEar); +
-    firstWindow.add(endButton); +
-    firstWindow.setVisible(true); +
-  } +
-  +
   public static void main(String[] args) {   public static void main(String[] args) {
     javax.swing.SwingUtilities.invokeLater(new Runnable() {     javax.swing.SwingUtilities.invokeLater(new Runnable() {
Line 147: Line 82:
     });     });
   }   }
-} 
 </code> </code>
  
  
swing/first_program.txt · Last modified: 2021/04/12 06:19 (external edit)