See also: Java Highlighter
Todo:
import java.awt.*;
/*
* StackLayout.java (Market View Java Applet)
* Copyright (C) 1996 Softbear Inc. (info@softbear.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/**
* A layoutManager which stacks components one on top of the other,
* regardless of their size.
* @version Improved by Mark Grand
*/
public class StackLayout implements LayoutManager {
private int vgap;
private static final int DEFAULT_VGAP = 3;
/**
* Create a StackLayout with a default vertical gap.
*/
public StackLayout() {
this(DEFAULT_VGAP);
} // StackLayout()
/**
* Create a StackLayout with the specified vertical gap.
* @param vgap the number of pixels to leave between Components.
*/
public StackLayout(int vgap) {
this.vgap = vgap;
} // StackLayout(int)
/**
* Adds the specified component with the specified name to
* the layout.
* The implementation of this method does not actually do anything
* because this LayoutManager uses the Container's information
* about its contents.
*/
public void addLayoutComponent(String name, Component comp) {}
/**
* Removes the specified component from the layout.
* @param comp the component ot be removed
* The implementation of this method does not actually do anything
* because this LayoutManager uses the Container's information
* about its contents.
*/
public void removeLayoutComponent(Component comp) {
} // removeLayoutComponent(Component)
/**
* Calculates the preferred size dimensions for the specified
* panel given the components in the specified parent container.
* @param parent the component to be laid out
* @see #minimumLayoutSize
*/
public Dimension preferredLayoutSize(Container parent) {
Insets insets = parent.insets();
int ncomponents = parent.countComponents();
int w = 0;
int h = 0;
for (int i = 0 ; i < ncomponents ; i++) {
Component comp = parent.getComponent(i);
if (!comp.isVisible())
continue;
Dimension d = comp.preferredSize();
if (w < d.width) {
w = d.width;
} // if
h += d.height;
if (i != 0) {
h += this.vgap;
} // if
} // for
return new Dimension(insets.left + insets.right + w,
insets.top + insets.bottom + h);
} // preferredLayoutSize(Container)
/**
* Calculates the minimum size dimensions for the specified
* panel given the components in the specified parent container.
* @param parent the component to be laid out
* @see #preferredLayoutSize
*/
public Dimension minimumLayoutSize(Container parent) {
Insets insets = parent.insets();
int ncomponents = parent.countComponents();
int w = 0;
int h = 0;
for (int i = 0 ; i < ncomponents ; i++) {
Component comp = parent.getComponent(i);
if (!comp.isVisible())
continue;
Dimension d = comp.minimumSize();
if (w < d.width) {
w = d.width;
} // if
h += d.height;
if (i != 0) {
h += this.vgap;
} // if
} // for
return new Dimension(insets.left + insets.right + w,
insets.top + insets.bottom + h);
}
/**
* Lays out the specified container.
* @param parent the Container to lay out
*/
public void layoutContainer(Container parent) {
int availableHeight; // Actual height available layout.
Insets insets = parent.insets();
int x = insets.left;
int y = insets.top;
Dimension parentSize = parent.size();
int usableWidth = parentSize.width - insets.left - insets.right;
int ncomponents = parent.countComponents();
availableHeight = parentSize.height-insets.top-insets.bottom;
boolean useMinimum;
useMinimum = preferredLayoutSize(parent).height>availableHeight;
for (int i = 0; i < ncomponents; ++i) {
Component comp = parent.getComponent(i);
if (!comp.isVisible())
continue;
Dimension d = useMinimum ? comp.minimumSize()
: comp.preferredSize();
comp.reshape(x, y, usableWidth, d.height);
y += (d.height + this.vgap);
}
} // layoutContainer(Container)
} // class StackLayout
// IntroExample.java
// An introduction to building menus and menu items. Accelerators and
// mnemonics are added to various items.
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class IntroExample extends JMenuBar {
String[] fileItems = new String[] { "New", "Open", "Save", "Exit" };
String[] editItems = new String[] { "Undo", "Cut", "Copy", "Paste" };
char[] fileShortcuts = { 'N','O','S','X' };
char[] editShortcuts = { 'Z','X','C','V' };
public IntroExample() {
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu otherMenu = new JMenu("Other");
JMenu subMenu = new JMenu("SubMenu");
JMenu subMenu2 = new JMenu("SubMenu2");
// Assemble the File menus with mnemonics
ActionListener printListener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("Menu item [" + event.getActionCommand() +
"] was pressed.");
}
};
for (int i=0; i < fileItems.length; i++) {
JMenuItem item = new JMenuItem(fileItems[i], fileShortcuts[i]);
item.addActionListener(printListener);
fileMenu.add(item);
}
// Assemble the File menus with keyboard accelerators
for (int i=0; i < editItems.length; i++) {
JMenuItem item = new JMenuItem(editItems[i]);
item.setAccelerator(KeyStroke.getKeyStroke(editShortcuts[i],
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false));
item.addActionListener(printListener);
editMenu.add(item);
}
// Insert a separator in the Edit Menu in Position 1 after "Undo"
editMenu.insertSeparator(1);
// Assemble the submenus of the Other Menu
JMenuItem item;
subMenu2.add(item = new JMenuItem("Extra 2"));
item.addActionListener(printListener);
subMenu.add(item = new JMenuItem("Extra 1"));
item.addActionListener(printListener);
subMenu.add(subMenu2);
// Assemble the Other Menu itself
otherMenu.add(subMenu);
otherMenu.add(item = new JCheckBoxMenuItem("Check Me"));
item.addActionListener(printListener);
otherMenu.addSeparator();
ButtonGroup buttonGroup = new ButtonGroup();
otherMenu.add(item = new JRadioButtonMenuItem("Radio 1"));
item.addActionListener(printListener);
buttonGroup.add(item);
otherMenu.add(item = new JRadioButtonMenuItem("Radio 2"));
item.addActionListener(printListener);
buttonGroup.add(item);
otherMenu.addSeparator();
otherMenu.add(item = new JMenuItem("Potted Plant",
new ImageIcon("image.gif")));
item.addActionListener(printListener);
// Finally, add all the menus to the menu bar
add(fileMenu);
add(editMenu);
add(otherMenu);
}
public static void main(String s[]) {
JFrame frame = new JFrame("Simple Menu Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(new IntroExample());
frame.pack();
frame.setVisible(true);
}
}