?? sketchframe.java
字號(hào):
// Frame for the Sketcher application
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class SketchFrame extends JFrame
implements Constants
{
// Constructor
public SketchFrame(String title, Sketcher theApp)
{
setTitle(title); // Set the window title
this.theApp = theApp;
setJMenuBar(menuBar); // Add the menu bar to the window
JMenu fileMenu = new JMenu("File"); // Create File menu
JMenu elementMenu = new JMenu("Elements"); // Create Elements menu
fileMenu.setMnemonic('F'); // Create shortcut
elementMenu.setMnemonic('E'); // Create shortcut
// Construct the file pull down menu
addMenuItem(fileMenu,
newAction = new FileAction("New", "Create new sketch"),
KeyStroke.getKeyStroke('N',Event.CTRL_MASK ));
addMenuItem(fileMenu,
openAction = new FileAction("Open", "Open existing sketch"),
KeyStroke.getKeyStroke('O',Event.CTRL_MASK ));
addMenuItem(fileMenu,
closeAction = new FileAction("Close", "Close sketch"), null);
fileMenu.addSeparator(); // Add separator
addMenuItem(fileMenu,
saveAction = new FileAction("Save", "Save sketch"),
KeyStroke.getKeyStroke('S',Event.CTRL_MASK ));
addMenuItem(fileMenu,
saveAsAction = new FileAction("Save As...","Save as new file"),
null);
fileMenu.addSeparator(); // Add separator
addMenuItem(fileMenu,
printAction = new FileAction("Print", "Print sketch"),
KeyStroke.getKeyStroke('P',Event.CTRL_MASK ));
// Construct the Element pull down menu
addMenuItem(elementMenu,
lineAction = new TypeAction("Line", LINE, "Draw lines"));
addMenuItem(elementMenu,
rectangleAction = new TypeAction("Rectangle", RECTANGLE,
"Draw rectangles"));
addMenuItem(elementMenu,
circleAction = new TypeAction("Circle", CIRCLE, "Draw circles"));
addMenuItem(elementMenu,
curveAction = new TypeAction("Curve", CURVE, "Draw curves"));
elementMenu.addSeparator();
JMenu colorMenu = new JMenu("Color"); // Color sub-menu
elementMenu.add(colorMenu); // Add the sub-menu
addMenuItem(colorMenu,
redAction = new ColorAction("Red", Color.red, "Draw in red"));
addMenuItem(colorMenu,
yellowAction = new ColorAction("Yellow", Color.yellow,
"Draw in yellow"));
addMenuItem(colorMenu,
greenAction = new ColorAction("Green", Color.green,
"Draw in green"));
addMenuItem(colorMenu,
blueAction = new ColorAction("Blue", Color.blue, "Draw in blue"));
menuBar.add(fileMenu); // Add the file menu
menuBar.add(elementMenu); // Add the element menu
// Add file buttons
toolBar.addSeparator(); // Space at the start
addToolBarButton(newAction);
addToolBarButton(openAction);
addToolBarButton(saveAction);
addToolBarButton(printAction);
// Add element type buttons
toolBar.addSeparator();
addToolBarButton(lineAction);
addToolBarButton(rectangleAction);
addToolBarButton(circleAction);
addToolBarButton(curveAction);
// Add element color buttons
toolBar.addSeparator();
addToolBarButton(redAction);
addToolBarButton(yellowAction);
addToolBarButton(greenAction);
addToolBarButton(blueAction);
toolBar.addSeparator(); // Space at the end
toolBar.setBorder(BorderFactory.createCompoundBorder( // Toolbar border
BorderFactory.createLineBorder(Color.darkGray),
BorderFactory.createEmptyBorder(2,2,4,2)));
toolBar.setFloatable(false); // Inhibit toolbar floating
getContentPane().add(toolBar, BorderLayout.NORTH); // Add the toolbar
// Disable actions
saveAction.setEnabled(false);
closeAction.setEnabled(false);
printAction.setEnabled(false);
}
private JButton addToolBarButton(Action action)
{
JButton button = toolBar.add(action); // Add toolbar button
button.setToolTipText((String)action.getValue(action.SHORT_DESCRIPTION));
button.setBorder(BorderFactory.createRaisedBevelBorder()); // Add button border
button.setText(null); // No button text
return button;
}
private JMenuItem addMenuItem(JMenu menu, Action action)
{
JMenuItem item = menu.add(action); // Add the menu item
item.setIcon(null); // Remove the icon
return item; // Return the menu item
}
private JMenuItem addMenuItem(JMenu menu, Action action, KeyStroke keystroke)
{
JMenuItem item = addMenuItem(menu, action); // Add the menu item
item.setAccelerator(keystroke); // Set the accelerator
return item; // Return the menu item
}
// We will add inner classes defining action objects here...
class FileAction extends AbstractAction
{
FileAction(String name)
{
super(name);
String iconFileName = "Images/" + name + ".gif";
if(new File(iconFileName).exists())
putValue(SMALL_ICON, new ImageIcon(iconFileName));
}
FileAction(String name, String tooltip)
{
this(name); // Call the other constructor
if(tooltip != null) // If there is tooltip text
putValue(SHORT_DESCRIPTION, tooltip); // ...squirrel it away
}
public void actionPerformed(ActionEvent e)
{
// We will add action code here eventually...
}
}
class TypeAction extends AbstractAction
{
TypeAction(String name, int typeID)
{
super(name);
this.typeID = typeID;
String iconFileName = "Images/" + name + ".gif";
if(new File(iconFileName).exists())
putValue(SMALL_ICON, new ImageIcon(iconFileName));
}
TypeAction(String name, int typeID, String tooltip)
{
this(name, typeID);
if(tooltip != null) // If there is a tooltip
putValue(SHORT_DESCRIPTION, tooltip); // ...squirrel it away
}
public void actionPerformed(ActionEvent e)
{
elementType = typeID;
}
private int typeID;
}
// Handles color menu items
class ColorAction extends AbstractAction
{
public ColorAction(String name, Color color)
{
super(name);
this.color = color;
String iconFileName = "Images/" + name + ".gif";
if(new File(iconFileName).exists())
putValue(SMALL_ICON, new ImageIcon(iconFileName));
}
public ColorAction(String name, Color color, String tooltip)
{
this(name, color);
if(tooltip != null) // If there is a tooltip
putValue(SHORT_DESCRIPTION, tooltip); // ...squirrel it away
}
public void actionPerformed(ActionEvent e)
{
elementColor = color;
}
private Color color;
}
public Color getElementColor()
{
return elementColor;
}
public int getElementType()
{
return elementType;
}
// File actions
private FileAction newAction, openAction, closeAction,
saveAction, saveAsAction, printAction;
// Element type actions
private TypeAction lineAction, rectangleAction, circleAction,
curveAction;
// Element color actions
private ColorAction redAction, yellowAction,
greenAction, blueAction;
private JMenuBar menuBar = new JMenuBar(); // Window menu bar
private Color elementColor = DEFAULT_ELEMENT_COLOR; // Current element color
private int elementType = DEFAULT_ELEMENT_TYPE; // Current element type
private JToolBar toolBar = new JToolBar(); // Window toolbar
Sketcher theApp;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -