6

Layout managers

  1. import java.awt.event.*;
  2. import java.awt.*;
  3.  
  4. public class SimpleFlow {
  5.  
  6.     public static void main(String[] args) {
  7.         ApplicationWindow appwin = new ApplicationWindow("Application");
  8.         appwin.setVisible(true);
  9.     }
  10.  
  11.     static class ApplicationWindow extends Frame {
  12.         ApplicationWindow(String name) {
  13.             super(name);
  14.  
  15.             addWindowListener(new WindowAdapter() {
  16.                 public void windowClosing(WindowEvent e) {
  17.                     System.exit(0);
  18.                 }
  19.             });
  20.  
  21.             Menu filemenu = new Menu("File", true);
  22.             MenuItem quit = new MenuItem("Quit");
  23.             quit.addActionListener(new ActionListener() {
  24.                 public void actionPerformed(ActionEvent e) {
  25.                     System.exit(0);
  26.                 }
  27.             });
  28.             filemenu.add(quit);
  29.  
  30.             MenuBar menubar = new MenuBar();
  31.             menubar.add(filemenu);
  32.  
  33.             setMenuBar(menubar);
  34.  
  35.             setLayout(new FlowLayout(FlowLayout.CENTER, 50, 20));
  36.             Label l1 = new Label("Do you really want to quit?");
  37.             add(l1);
  38.             Button b1 = new Button("Yes");
  39.             add(b1);
  40.             Button b2 = new Button("No");
  41.             add(b2);
  42.  
  43.             Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  44.  
  45.             int width = 200;
  46.             int height = 150;
  47.             int x = (screenSize.width - width) / 2;
  48.             int y = (screenSize.height - height) / 3;
  49.  
  50.             setBounds(x, y, width, height);
  51.             setResizable(true);
  52.         }
  53.     }
  54. }
$ javac SimpleFlow.java
$ java SimpleFlow

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].