?? mainframe.java
字號:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* StoneForest應(yīng)用的主框架
*/
public class MainFrame extends JFrame {
/**
* tabbed pane組件
*/
protected JTabbedPane tabbedPane;
/**
* 音樂CD panel
*/
protected MusicPanel musicPanel;
/**
* 默認構(gòu)造方法
*/
public MainFrame() {
setTitle("歡迎使用StoneForest應(yīng)用! ");
Container container = this.getContentPane();
container.setLayout(new BorderLayout());
tabbedPane = new JTabbedPane();
musicPanel = new MusicPanel(this);
tabbedPane.addTab("音樂", musicPanel);
container.add(BorderLayout.CENTER, tabbedPane);
JMenuBar myMenuBar = new JMenuBar();
JMenu fileMenu = new JMenu("文件");
JMenu openMenu = new JMenu("打開");
JMenuItem localMenuItem = new JMenuItem("本地硬盤...");
openMenu.add(localMenuItem);
JMenuItem networkMenuItem = new JMenuItem("網(wǎng)絡(luò)...");
openMenu.add(networkMenuItem);
JMenuItem webMenuItem = new JMenuItem("互聯(lián)網(wǎng)...");
openMenu.add(webMenuItem);
fileMenu.add(openMenu);
JMenuItem saveMenuItem = new JMenuItem("保存");
fileMenu.add(saveMenuItem);
JMenuItem exitMenuItem = new JMenuItem("退出");
fileMenu.add(exitMenuItem);
myMenuBar.add(fileMenu);
exitMenuItem.addActionListener(new ExitActionListener());
setupLookAndFeelMenu(myMenuBar);
JMenu helpMenu = new JMenu("幫助");
JMenuItem aboutMenuItem = new JMenuItem("關(guān)于");
helpMenu.add(aboutMenuItem);
myMenuBar.add(helpMenu);
aboutMenuItem.addActionListener(new AboutActionListener());
this.setJMenuBar(myMenuBar);
setSize(500, 400);
setLocation(100, 100);
this.addWindowListener(new WindowCloser());
fileMenu.setMnemonic('f');
exitMenuItem.setMnemonic('x');
helpMenu.setMnemonic('h');
aboutMenuItem.setMnemonic('a');
//設(shè)定快捷鍵
exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
aboutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
}
/**
* 設(shè)定和選擇外觀
*
*/
protected void setupLookAndFeelMenu(JMenuBar theMenuBar) {
UIManager.LookAndFeelInfo[] lookAndFeelInfo = UIManager.getInstalledLookAndFeels();
JMenu lookAndFeelMenu = new JMenu("選項");
JMenuItem anItem = null;
LookAndFeelListener myListener = new LookAndFeelListener();
try {
for (int i=0; i < lookAndFeelInfo.length; i++) {
anItem = new JMenuItem(lookAndFeelInfo[i].getName() + " 外觀");
anItem.setActionCommand(lookAndFeelInfo[i].getClassName());
anItem.addActionListener(myListener);
lookAndFeelMenu.add(anItem);
}
}
catch (Exception e) {
e.printStackTrace();
}
theMenuBar.add(lookAndFeelMenu);
}
/**
* 退出方法.
*/
public void exit() {
setVisible(false);
dispose();
System.exit(0);
}
/**
* "退出"事件處理內(nèi)部類.
*/
class ExitActionListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
exit();
}
}
/**
* "關(guān)閉窗口"事件處理內(nèi)部類.
*/
class WindowCloser extends WindowAdapter {
/**
* let's call our exit() method defined above
*/
public void windowClosing(WindowEvent e) {
exit();
}
}
/**
* "外觀"選擇監(jiān)聽類
*
*/
class LookAndFeelListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String className = event.getActionCommand();
try {
UIManager.setLookAndFeel(className);
SwingUtilities.updateComponentTreeUI(MainFrame.this);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* "關(guān)于"菜單監(jiān)聽類
*/
class AboutActionListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String msg = "StoneForest 超值享受!";
JOptionPane.showMessageDialog(MainFrame.this, msg);
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -