Электронная библиотека книг Александра Фролова и Григория Фролова.
Shop2You.ru Создайте свой интернет-магазин
Библиотека
Братьев
Фроловых
[Назад]

Исходный текст программы GridBagConsole.java

Оглавление

Назад

// ==========================================
// GridBagConsole.java
//(C) Alexandr Frolov, 1998
// E-mail: frolov@glasnet.ru
// Web:    http://www.glasnet.ru/~frolov 
// ==========================================
import java.awt.*;
import java.awt.event.*;

// =======================================
// ConsFrame
// =======================================
public class GridBagConsole
{
  public static void main(String args[])
  {
    FrameWindow frame;
    frame = 
      new FrameWindow("Application window");
    frame.setVisible(true);
  }
}

// =======================================
// Class FrameWindow
// =======================================
class FrameWindow extends Frame
  implements ActionListener, WindowListener
{
  Button btOpen;
  Button btClose;
  Button btExit;
  Button btAbout;
  TextArea ta;
  
  MenuBar mb;
  
  Menu mFile;
  MenuItem miOpen;
  MenuItem miClose;
  MenuItem miExit;
  
  Menu mHelp;
  MenuItem miAbout;
  
  // ============================================
  // FrameWindow
  // ============================================
  public FrameWindow(String szTitle)
  {
    super(szTitle);
    setSize(400, 300);
    
    mb = new MenuBar();
    mFile = new Menu("File");
    mHelp = new Menu("Help");
    
    miOpen = new MenuItem("Open...");
    mFile.add(miOpen);
    
    miClose = new MenuItem("Close");
    mFile.add(miClose);
    
    mFile.add("-");
    
    miExit = new MenuItem("Exit");
    mFile.add(miExit);
    
    miAbout = new MenuItem("About...");
    mHelp.add(miAbout);
    
    mb.add(mFile);
    mb.add(mHelp);
    
    miOpen.addActionListener(this);
    miClose.addActionListener(this);
    miExit.addActionListener(this);
    miAbout.addActionListener(this);
    
    setMenuBar(mb);
    
    this.addWindowListener(this);
    
    btOpen = new Button("Open");
    btClose = new Button("Close");
    btExit = new Button("Exit");
    btAbout = new Button("About");
    
    btOpen.addActionListener(this);
    btClose.addActionListener(this);
    btExit.addActionListener(this);
    btAbout.addActionListener(this);
  
    ta = new TextArea(10, 30);

    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints c = 
      new GridBagConstraints();
    
    setLayout(gbl);
    
    c.anchor = GridBagConstraints.NORTHWEST; 
    c.fill   = GridBagConstraints.NONE;  
    c.gridheight = 1;
    c.gridwidth  = 1;
    c.gridx = GridBagConstraints.RELATIVE; 
    c.gridy = GridBagConstraints.RELATIVE; 
    c.insets = new Insets(0, 0, 0, 0);
    
    gbl.setConstraints(btOpen, c);
    add(btOpen);
    
    gbl.setConstraints(btClose, c);
    add(btClose);
    
    gbl.setConstraints(btExit, c);
    add(btExit);
    
    c.gridwidth  = GridBagConstraints.REMAINDER;
    c.weightx = 1.0;
    gbl.setConstraints(btAbout, c);
    add(btAbout);
    
    c.gridwidth  = GridBagConstraints.REMAINDER;
    c.weighty = 1.0;
    c.fill = GridBagConstraints.BOTH;
    gbl.setConstraints(ta, c);
    add(ta);
  }

  // ============================================
  // actionPerformed
  // ============================================
  public void actionPerformed(ActionEvent e)
  {
    if(e.getSource().equals(btOpen))
    {
      ta.append("Open\n");
    }
    
    else if(e.getSource().equals(miOpen))
    {
      ta.append("Open (menu)\n");
    }
    
    else if(e.getSource().equals(btClose))
    {
      ta.append("Close\n");
    }
      
    else if(e.getSource().equals(miClose))
    {
      ta.append("Close (menu)\n");
    }
      
    else if(e.getSource().equals(btExit) ||
      e.getSource().equals(miExit))
    {
      setVisible(false);
      System.exit(0);
    }
      
    else if(e.getSource().equals(btAbout))
    {
      ta.append("About\n");
    }
    
    else if(e.getSource().equals(miAbout))
    {
      ta.append("About (menu)\n");
    }
  }
  
  // ============================================
  // windowClosing
  // ============================================
  public void windowClosing(WindowEvent e)
  {
    setVisible(false);
    System.exit(0);
  }
  
  public void windowOpened(WindowEvent e) {}
  public void windowClosed(WindowEvent e) {}
  public void windowIconified(WindowEvent e) {}
  public void windowDeiconified(WindowEvent e) {}
  public void windowActivated(WindowEvent e) {}
  public void windowDeactivated(WindowEvent e) {}
}

[Назад]