import javax.swing.*;
import javax.swing.border.*;
import javax.swing.text.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.io.*;
import java.lang.*;
import javax.swing.undo.*;
public class MainFrame
{
JDesktopPane desktop;
JFrame topFrame;
JMenuBar menuBar;
JPopupMenu popupMenu;
public MainFrame()
{
topFrame = new JFrame("MainFrame-Look and Feel");
topFrame.setBounds(100,100,630,400); // --- setBounds(x座標,y座標,寬,高)
buildContent();
buildMenuBar();
buildToolBar();
popupMenu = buildPopupMenu();
topFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); //end of addWindowListener
topFrame.setVisible(true);
} //end of MainFrame()
public void buildContent()
{
desktop = new JDesktopPane();
topFrame.getContentPane().add(desktop);
}//end of buildContent()
public void buildMenuBar()
{
String menuName;
menuBar = new JMenuBar();
menuName = "File";
String[] fileFunctions = {"New", "Open", "Close", "Exit"};
char[] fileMnemonic = {'N', 'O', 'L', 'X'};
JMenu mfile = buildMenu(menuName,fileFunctions,fileMnemonic);
menuName = "Edit";
JMenuItem item;
JMenu medit = new JMenu(menuName);
item = medit.add(new ActionSet("Undo",null));
item.setActionCommand("Undo");
item.setMnemonic('Z');
item.setAccelerator( KeyStroke.getKeyStroke('Z',java.awt.Event.CTRL_MASK,false));
item = medit.add(new ActionSet("Redo",null));
item.setActionCommand("Redo");
item.setMnemonic('Y');
item.setAccelerator( KeyStroke.getKeyStroke('Y',java.awt.Event.CTRL_MASK,false));
medit.addSeparator();
Action action = new DefaultEditorKit.CopyAction();
action.putValue(Action.NAME, "Copy");
item = medit.add(action);
item.setMnemonic('T');
item.setAccelerator( KeyStroke.getKeyStroke('T',java.awt.Event.CTRL_MASK,false));
action = new DefaultEditorKit.CutAction();
action.putValue(Action.NAME, "Cut");
item = medit.add(action);
item.setMnemonic('C');
item.setAccelerator( KeyStroke.getKeyStroke('C',java.awt.Event.CTRL_MASK,false));
action = new DefaultEditorKit.PasteAction();
action.putValue(Action.NAME, "Paste");
item = medit.add(action);
item.setMnemonic('P');
item.setAccelerator( KeyStroke.getKeyStroke('P',java.awt.Event.CTRL_MASK,false));
JMenu malign = buildAlignMenu();
menuName = "Insert";
String[] insertFunctions = {"Picture", "Object"};
char[] insertMnemonic = null;
JMenu minsert = buildMenu(menuName,insertFunctions,insertMnemonic);
menuName = "Demo";
String[] demoFunctions = {"Slider", "Tree Structure", "Table Structure", "Password"};
char[] demoMnemonic = {'S', 'T', 'A', 'P'};
JMenu mdemo = buildMenu(menuName,demoFunctions,demoMnemonic);
menuName = "Help";
String[] helpFunctions = {"About this Program", "Open Help Window"};
char[] helpMnemonic = {'B', 'H'};
JMenu mhelp = buildMenu(menuName,helpFunctions,helpMnemonic);
//設定Hot Key Alt+'?'
mfile.setMnemonic('F');
medit.setMnemonic('E');
malign.setMnemonic('A');
minsert.setMnemonic('I');
mdemo.setMnemonic('D');
mhelp.setMnemonic('H');
menuBar.add(mfile); //將 File 選項加到MenuBar中
menuBar.add(medit); //將 Edit 選項加到MenuBar中
menuBar.add(malign); //將 Align 選項加到MenuBar中
menuBar.add(mdemo); //將 Demo 選項加到MenuBar中
menuBar.add(mhelp); //將 Help 選項加到MenuBar中
topFrame.setJMenuBar(menuBar);
}//end of buildMenuBar()
public JMenu buildMenu(String menuName, String[] functions, char[] mnemonic)
{
ActionSet action;
JMenuItem item;
JMenu menu = new JMenu(menuName);
for (int i=0; i<functions.length; i++)
{
item = menu.add(new ActionSet(functions[i],null));
item.setActionCommand(functions[i]);
if (mnemonic != null)
{
item.setMnemonic(mnemonic[i]);
item.setAccelerator( KeyStroke.getKeyStroke(mnemonic[i],java.awt.Event.CTRL_MASK,false));
}
}
return menu;
}//end of buildMenu()
public JMenu buildAlignMenu()
{
JRadioButtonMenuItem aL;
JRadioButtonMenuItem aC;
JRadioButtonMenuItem aR;
JMenu malign = new JMenu("Align");
aL = new JRadioButtonMenuItem("Align Left");
aC = new JRadioButtonMenuItem("Align Center");
aR = new JRadioButtonMenuItem("Align Right");
SimpleAttributeSet attrset = new SimpleAttributeSet();
aL.addItemListener(new HandleItemEvent("LEFT",attrset));
aC.addItemListener(new HandleItemEvent("CENTER",attrset));
aR.addItemListener(new HandleItemEvent("RIGHT",attrset));
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(aL);
buttonGroup.add(aC);
buttonGroup.add(aR);
JCheckBoxMenuItem demo1 = new JCheckBoxMenuItem("demo1", true);
JCheckBoxMenuItem demo2 = new JCheckBoxMenuItem("demo2");
JCheckBoxMenuItem demo3 = new JCheckBoxMenuItem("demo3");
malign.add(aL);
malign.add(aC);
malign.add(aR);
malign.addSeparator();
malign.add(demo1);
malign.add(demo2);
malign.add(demo3);
return malign;
}//end of buildAlignMenu()
class HandleItemEvent implements ItemListener
{
String align;
SimpleAttributeSet attrset;
HandleItemEvent(String align, SimpleAttributeSet attrset)
{
this.align = align;
this.attrset = attrset;
}
public void itemStateChanged(ItemEvent e)
{
NewFrame currentFrame = (NewFrame)desktop.getSelectedFrame();
JTextPane pane = currentFrame.getTextPane();
StyledDocument docs = pane.getStyledDocument();
int offset = pane.getSelectionStart();
int end = pane.getSelectionEnd();
int selected_length = end - offset;
if(currentFrame == null) { return;}
if (align.equals("LEFT"))
StyleConstants.setAlignment(attrset, StyleConstants.ALIGN_LEFT);
if (align.equals("CENTER"))
StyleConstants.setAlignment(attrset, StyleConstants.ALIGN_CENTER);
if (align.equals("RIGHT"))
StyleConstants.setAlignment(attrset, StyleConstants.ALIGN_RIGHT);
docs.setParagraphAttributes(offset, selected_length, attrset, true);
}//end of itemStateChanged()
}//end of HandleItemEvent class
public void buildToolBar()
{
JToolBar toolBar = new JToolBar();
toolBar.setFloatable(true);
String[] newFunctions = {"New"};
String[] newPics = {"icons/new16.gif"};
buildToolButton(toolBar,newFunctions,newPics);
toolBar.addSeparator();
String[] editFunctions = {"Cut","Copy","Paste"};
String[] editPics = {"icons/cut16.gif","icons/copy16.gif","icons/paste16.gif"};
bu