import java.awt.Event;
import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.io.File;public class SketchFrame extends JFrame implements Constants { // Constructor
public SketchFrame(String title) {
setTitle(title); // Set the window title
setJMenuBar(menuBar); // Add the menu bar to the window
setDefaultCloseOperation(EXIT_ON_CLOSE); 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 // Create the action items for the file menu
newAction = new FileAction("New", KeyStroke.getKeyStroke('N',Event.CTRL_MASK ), "Create new sketch");
openAction = new FileAction("Open", KeyStroke.getKeyStroke('O',Event.CTRL_MASK), "Open existing sketch");
closeAction = new FileAction("Close", "Close sketch");
saveAction = new FileAction("Save", KeyStroke.getKeyStroke('S',Event.CTRL_MASK), "Save sketch");
saveAsAction = new FileAction("Save As...", "Save as new file");
printAction = new FileAction("Print", KeyStroke.getKeyStroke('P',Event.CTRL_MASK), "Print sketch");
exitAction = new FileAction("Exit", KeyStroke.getKeyStroke('X',Event.ALT_MASK), "Exit"); // Construct the file pull down menu
addMenuItem(fileMenu, newAction);
addMenuItem(fileMenu, openAction);
addMenuItem(fileMenu, closeAction);
fileMenu.addSeparator(); // Add separator
addMenuItem(fileMenu, saveAction);
addMenuItem(fileMenu, saveAsAction);
fileMenu.addSeparator(); // Add separator
addMenuItem(fileMenu, printAction);
fileMenu.addSeparator(); // Add separator
addMenuItem(fileMenu, exitAction); // 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")); fileMenu.addSeparator(); // Add separator 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 toolBar.setFloatable(false); // Inhibit toolbar floating
getContentPane().add(toolBar, BorderLayout.NORTH); // Add file buttons
toolBar.addSeparator(); // Space at the start
addToolBarButton(newAction);
addToolBarButton(openAction);
addToolBarButton(saveAction);
addToolBarButton(printAction); toolBar.addSeparator();
addToolBarButton(exitAction);
// 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);
chooser=new JFileChooser();
}
import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.io.File;public class SketchFrame extends JFrame implements Constants { // Constructor
public SketchFrame(String title) {
setTitle(title); // Set the window title
setJMenuBar(menuBar); // Add the menu bar to the window
setDefaultCloseOperation(EXIT_ON_CLOSE); 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 // Create the action items for the file menu
newAction = new FileAction("New", KeyStroke.getKeyStroke('N',Event.CTRL_MASK ), "Create new sketch");
openAction = new FileAction("Open", KeyStroke.getKeyStroke('O',Event.CTRL_MASK), "Open existing sketch");
closeAction = new FileAction("Close", "Close sketch");
saveAction = new FileAction("Save", KeyStroke.getKeyStroke('S',Event.CTRL_MASK), "Save sketch");
saveAsAction = new FileAction("Save As...", "Save as new file");
printAction = new FileAction("Print", KeyStroke.getKeyStroke('P',Event.CTRL_MASK), "Print sketch");
exitAction = new FileAction("Exit", KeyStroke.getKeyStroke('X',Event.ALT_MASK), "Exit"); // Construct the file pull down menu
addMenuItem(fileMenu, newAction);
addMenuItem(fileMenu, openAction);
addMenuItem(fileMenu, closeAction);
fileMenu.addSeparator(); // Add separator
addMenuItem(fileMenu, saveAction);
addMenuItem(fileMenu, saveAsAction);
fileMenu.addSeparator(); // Add separator
addMenuItem(fileMenu, printAction);
fileMenu.addSeparator(); // Add separator
addMenuItem(fileMenu, exitAction); // 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")); fileMenu.addSeparator(); // Add separator 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 toolBar.setFloatable(false); // Inhibit toolbar floating
getContentPane().add(toolBar, BorderLayout.NORTH); // Add file buttons
toolBar.addSeparator(); // Space at the start
addToolBarButton(newAction);
addToolBarButton(openAction);
addToolBarButton(saveAction);
addToolBarButton(printAction); toolBar.addSeparator();
addToolBarButton(exitAction);
// 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);
chooser=new JFileChooser();
}
解决方案 »
- 老题重谈 静态属性和方法的继承
- 数组名称循环表出的问题,求解呀
- 进入index.jsp页面不能识别<f:view>标签
- 使用String的方法indexof
- 请教一个问题(about JTable and JComboBox)
- 关于String字符集转换的一个问题,100分
- 聊天室程序的服务端在获取socket的InputStream时有问题,请帮忙
- 关于<<,>>运算付的问题
- 以前没学过JAVA,现在想学,请各位帮帮小弟,我想买书,介绍几本好书来啊。
- 请教.linux中classpath的设置?
- 有没有好的java游戏开发包?
- 在一个项目中如何使用Logger进行记录所有信息,然后保存至数据!
dispose();
System.exit(0);
} private JButton addToolBarButton(Action action) {
JButton button = toolBar.add(action); // Add toolbar button
button.setBorder(BorderFactory.createRaisedBevelBorder());// Add button border
return button;
} // We will add inner classes defining action objects here...
class FileAction extends AbstractAction {
// Constructor
FileAction(String name) {
super(name);
String iconFileName = "Images/" + name + ".gif";
if(new File(iconFileName).exists())
putValue(SMALL_ICON, new ImageIcon(iconFileName));
} // Constructor
FileAction(String name, KeyStroke keystroke) {
this(name);
if(keystroke != null)
putValue(ACCELERATOR_KEY, keystroke);
} 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
} FileAction(String name, KeyStroke keystroke, String tooltip) {
this(name, keystroke); // Call the other constructor
if(tooltip != null) // If there is tooltip text
putValue(SHORT_DESCRIPTION, tooltip); // ...squirrel it away
}
// Event handler
public void actionPerformed(ActionEvent e) {
// We will add action code here eventually...
// If it is an Exit action call exit() for the frame window
if(getValue(NAME).equals("Exit"))
exit();
}
} 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;
// This is temporary - just to show it works
getContentPane().setBackground(color);
} private Color color;
} private JMenuItem addMenuItem(JMenu menu, Action action) {
JMenuItem item = menu.add(action); // Add the menu item KeyStroke keystroke = (KeyStroke)action.getValue(action.ACCELERATOR_KEY);
if(keystroke != null)
item.setAccelerator(keystroke);
// remove this comments to disable icons
// item.setIcon(null); // Remove the icon
return item; // Return the menu item
} // We will add action objects as members here... // Element color actions
private ColorAction redAction, yellowAction, greenAction, blueAction; // Element type actions
private TypeAction lineAction, rectangleAction, circleAction, curveAction; // File actions
private FileAction newAction, openAction, closeAction, saveAction, saveAsAction,
printAction, exitAction; 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
private JFileChooser chooser;
}
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;public class Sketcher { public static void main(String[] args) {
theApp = new Sketcher(); // Create the application object
theApp.init(); // ...and initialize it
} public void init() {
window = new SketchFrame("Sketcher"); // Create the app window
Toolkit theKit = window.getToolkit(); // Get the window toolkit
Dimension wndSize = theKit.getScreenSize(); // Get screen size // Set the position to screen center & size to half screen size
window.setBounds(wndSize.width/4, wndSize.height/4, // Position
wndSize.width/2, wndSize.height/2); // Size window.addWindowListener(new WindowHandler()); // Add window listener
window.setVisible(true);
}
// Handler class for window events
class WindowHandler extends WindowAdapter {
// Handler for window closing event
public void windowClosing(WindowEvent e) {
window.dispose(); // Release the window resources
System.exit(0); // End the application
}
} private static SketchFrame window; // The application window
private static Sketcher theApp; // The application object
}
主程序上面是框架程序!