9

SimpleGridBag

  1. import java.awt.event.*;
  2. import java.awt.*;
  3.  
  4. public class SimpleGridBag {
  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 implements ActionListener {
  12.         Label prompt;
  13.         Button yes, no;
  14.        
  15.         ApplicationWindow(String name) {
  16.             super(name);
  17.  
  18.             addWindowListener(new WindowAdapter() {
  19.                 public void windowClosing(WindowEvent e) {
  20.                     System.exit(0);
  21.                 }
  22.             });
  23.  
  24.             Menu filemenu = new Menu("File", true);
  25.             MenuItem quit = new MenuItem("Quit");
  26.             quit.addActionListener(new ActionListener() {
  27.                 public void actionPerformed(ActionEvent e) {
  28.                     System.exit(0);
  29.                 }
  30.             });
  31.             filemenu.add(quit);
  32.  
  33.             MenuBar menubar = new MenuBar();
  34.             menubar.add(filemenu);
  35.  
  36.             setMenuBar(menubar);
  37.  
  38.             setLayout(new GridBagLayout());
  39.             GridBagConstraints c = new GridBagConstraints();
  40.            
  41.             prompt = new Label("Do you really want to quit?");
  42.             c.weightx = 1;
  43.             c.ipadx = 10;
  44.             c.gridx = 0;
  45.             c.gridy = 0;
  46.             c.gridwidth = 2;
  47.             c.gridheight = 1;
  48.             c.insets = new Insets(20, 10, 10, 10);
  49.             add(prompt, c);
  50.            
  51.             yes = new Button("Yes");
  52.             yes.addActionListener(new ActionListener() {
  53.                 public void actionPerformed(ActionEvent e) {
  54.                     System.exit(0);
  55.                 }
  56.             });
  57.             c.gridx = 0;
  58.             c.gridy = 1;
  59.             c.gridwidth = 1;
  60.             c.gridheight = 1;
  61.             c.insets = new Insets(10, 0, 10, 0);
  62.             add(yes, c);
  63.  
  64.             no = new Button("No");
  65.             no.setActionCommand("no");
  66.             no.addActionListener(this);
  67.             c.gridx = 1;
  68.             c.gridy = 1;
  69.             c.gridwidth = 1;
  70.             c.gridheight = 1;
  71.             c.insets = new Insets(10, 0, 10, 0);
  72.             add(no, c);
  73.            
  74.             pack();
  75.            
  76.             Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  77.  
  78.             int width = getSize().width;
  79.             int height = getSize().height;
  80.             int x = (screenSize.width - width) / 2;
  81.             int y = (screenSize.height - height) / 3;
  82.  
  83.             setLocation(x, y);
  84.             setResizable(true);
  85.         }
  86.        
  87.         public void actionPerformed(ActionEvent e) {
  88.             if (e.getActionCommand() == "no") {
  89.                 prompt.setForeground(new Color(255, 0, 0));
  90.                 prompt.repaint();
  91.             }
  92.         }
  93.     }
  94. }
$ javac SimpleGridBag.java
$ java SimpleGridBag

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].