/* * 1.1 version. */ import java.awt.*; import java.awt.event.*; public class GUIWindow extends Frame implements ActionListener { boolean inAnApplet = true; final String FILEDIALOGMENUITEM = "File dialog..."; public GUIWindow() { Panel bottomPanel = new Panel(); Panel centerPanel = new Panel(); setLayout(new BorderLayout()); //Set up the menu bar. MenuBar mb = new MenuBar(); Menu m = new Menu("Menu"); m.add(new MenuItem("Menu item 1")); m.add(new CheckboxMenuItem("Menu item 2")); m.add(new MenuItem("Menu item 3")); m.add(new MenuItem("-")); MenuItem fileMenuItem = new MenuItem(FILEDIALOGMENUITEM); fileMenuItem.addActionListener(this); m.add(fileMenuItem); mb.add(m); setMenuBar(mb); //Add small things at the bottom of the window. bottomPanel.add(new TextField("TextField")); bottomPanel.add(new Button("Button")); bottomPanel.add(new Checkbox("Checkbox")); Choice c = new Choice(); c.add("Choice Item 1"); c.add("Choice Item 2"); c.add("Choice Item 3"); bottomPanel.add(c); add("South", bottomPanel); //Add big things to the center area of the window. centerPanel.setLayout(new GridLayout(1,2)); //Put a canvas in the left column. centerPanel.add(new MyCanvas()); //Put a label and a text area in the right column. Panel p = new Panel(); p.setLayout(new BorderLayout()); p.add("North", new Label("Label", Label.CENTER)); p.add("Center", new TextArea("TextArea", 5, 20)); centerPanel.add(p); add("Center", centerPanel); //Put a list on the right side of the window. List l = new List(3, false); for (int i = 1; i <= 10; i++) { l.add("List item " + i); } add("East", l); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { if (inAnApplet) { dispose(); } else { System.exit(0); } } }); } public void actionPerformed(ActionEvent event) { //The only action event we get is when the //user requests we bring up a FileDialog. FileDialog fd = new FileDialog(this, "FileDialog"); fd.setVisible(true); } public static void main(String[] args) { GUIWindow window = new GUIWindow(); window.inAnApplet = false; window.setTitle("The AWT Components"); window.pack(); window.setVisible(true); } } //We can't just instantiate Canvas, since its default implementation //gives us nothing interesting to look at or do. So here's a Canvas //subclass that draws something slightly interesting. class MyCanvas extends Canvas { public void paint(Graphics g) { int w = getSize().width; int h = getSize().height; g.drawRect(0, 0, w - 1, h - 1); g.drawString("Canvas", (w - g.getFontMetrics().stringWidth("Canvas"))/2, 10); g.setFont(new Font("Helvetica", Font.PLAIN, 8)); g.drawLine(10,10, 100,100); g.fillRect(9,9,3,3); g.drawString("(10,10)", 13, 10); g.fillRect(49,49,3,3); g.drawString("(50,50)", 53, 50); g.fillRect(99,99,3,3); g.drawString("(100,100)", 103, 100); } //If we don't specify this, the canvas might not show up at all //(depending on the layout manager). public Dimension getMinimumSize() { return new Dimension(150,130); } //If we don't specify this, the canvas might not show up at all //(depending on the layout manager). public Dimension getPreferredSize() { return getMinimumSize(); } }