import java.awt.BorderLayout; import java.awt.event.*; import javax.swing.*; /* Copyright (c) 1999 Nathan Meyers $Id: GuiSwing.java,v 1.3 1999/11/12 19:46:34 nathanm Exp $ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ public class GuiSwing extends JApplet { // Constructor: Fill up with a passel of GUI components. public GuiSwing() { // We'll use the BorderLayout manager getContentPane().setLayout(new BorderLayout()); // Start adding things... an option menu JComboBox choice = new JComboBox(); // We don't want user editing of the input choice.setEditable(false); getContentPane().add(choice, BorderLayout.NORTH); choice.addItem("Choice 1"); choice.addItem("Choice 2"); choice.addItem("Choice 3"); choice.addItem("Choice 4"); choice.addItem("Choice 5"); // A checkbox getContentPane().add(new JCheckBox("Checkbox"), BorderLayout.WEST); // A scrolled text area. Unlike AWT, the Swing text area needs a // scrollpane supplied externally. JTextArea text = new JTextArea(); text.setText("The quick brown fox jumps over the lazy dog."); text.setRows(4); getContentPane().add(new JScrollPane(text), BorderLayout.CENTER); // A button getContentPane().add(new JButton("Button"), BorderLayout.EAST); // And a list Unlike AWT, the Swing list component area needs a // scrollpane supplied externally. DefaultListModel listModel = new DefaultListModel(); listModel.addElement("Item 1"); listModel.addElement("Item 2"); listModel.addElement("Item 3"); listModel.addElement("Item 4"); listModel.addElement("Item 5"); JList list = new JList(listModel); list.setVisibleRowCount(4); getContentPane().add(new JScrollPane(list), BorderLayout.SOUTH); } public static void main(String[] argv) { JFrame frame = new JFrame(); GuiSwing guiSwing = new GuiSwing(); frame.getContentPane().add(guiSwing); frame.pack(); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent ev) { System.exit(0); } }); } }