📄 mainframe.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* StoneForest应用的主框架
*/
public class MainFrame extends JFrame {
/**
* tabbed pane组件
*/
protected JTabbedPane tabbedPane;
/**
* 音乐CD panel
*/
protected MusicPanel musicPanel;
/**
* 默认构造方法
*/
public MainFrame() {
setTitle("欢迎使用StoneForest应用! ");
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("网络...");
openMenu.add(networkMenuItem);
JMenuItem webMenuItem = new JMenuItem("互联网...");
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("关于");
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');
//设定快捷键
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));
}
/**
* 设定和选择外观
*
*/
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);
}
/**
* "退出"事件处理内部类.
*/
class ExitActionListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
exit();
}
}
/**
* "关闭窗口"事件处理内部类.
*/
class WindowCloser extends WindowAdapter {
/**
* let's call our exit() method defined above
*/
public void windowClosing(WindowEvent e) {
exit();
}
}
/**
* "外观"选择监听类
*
*/
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();
}
}
}
/**
* "关于"菜单监听类
*/
class AboutActionListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String msg = "StoneForest 超值享受!";
JOptionPane.showMessageDialog(MainFrame.this, msg);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -