📄 mainframe.java
字号:
package cn.edu.csu.oo.gui.project.view.frame;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.tree.DefaultMutableTreeNode;
import cn.edu.csu.oo.gui.project.view.panel.StudentFindPanel;
import cn.edu.csu.oo.gui.project.view.panel.StudentModifyPanel;
import cn.edu.csu.oo.gui.project.view.panel.StudentRegistPanel;
import cn.edu.csu.oo.gui.project.view.viewaction.BtnActionOper;
import cn.edu.csu.oo.gui.project.view.viewaction.TreeAction;
public class MainFrame extends JFrame {
/**
* 定义菜单栏
*/
private JMenuBar menuBar;
private JTree tree;
/**
* 定义工具栏
*/
private JToolBar toolBar;
private JSplitPane splitPane;
private ActionListener actionListener;
public JTabbedPane tab;
public MainFrame() {
super("教务管理系统主界面");
// 加载菜单到主界面
this.setJMenuBar(buildMenuBar());
this.add(buildToolBar(), "North");
this.add(buildSplitPane());
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize((int) screenSize.getWidth(), (int) screenSize.getHeight());
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int ch = JOptionPane.showConfirmDialog(MainFrame.this, "你确定要退出程序吗",
"关闭窗体", JOptionPane.YES_NO_OPTION);
if (ch == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});
// 主窗体居中显示
CenterContainer.centerContainer(this);
}
/**
* 构造一个splitpane,将容器剩下部分风格承左右两个面板panel
*
* @return
*/
public JSplitPane buildSplitPane() {
splitPane = new JSplitPane();
// 设置左边面板上加载的组件(scrollpane)
splitPane.setLeftComponent(buildJScrollPane());
// 设置右边面板上加载的组件(tabbedpane)
splitPane.setRightComponent(buildTabbedPane());
return splitPane;
}
public JScrollPane buildJScrollPane() {
JScrollPane scrollPane = new JScrollPane(buildJTree());
return scrollPane;
}
public JTree buildJTree() {
if (tree == null) {
//树的根节点
DefaultMutableTreeNode root = new DefaultMutableTreeNode("教务信息管理");
//学生管理根节点
DefaultMutableTreeNode studentRoot = new DefaultMutableTreeNode("学生信息管理");
DefaultMutableTreeNode studentRegist = new DefaultMutableTreeNode(
"学生信息注册");
DefaultMutableTreeNode studentFind = new DefaultMutableTreeNode("学生信息查询");
DefaultMutableTreeNode studentModify = new DefaultMutableTreeNode(
"学生信息维护");
//老师管理根节点
DefaultMutableTreeNode teacherRoot = new DefaultMutableTreeNode("老师信息管理");
DefaultMutableTreeNode teacherRegist = new DefaultMutableTreeNode(
"老师信息注册");
DefaultMutableTreeNode teacherFind = new DefaultMutableTreeNode("老师信息查询");
DefaultMutableTreeNode teacherModify = new DefaultMutableTreeNode(
"老师信息维护");
studentRoot.add(studentRegist);
studentRoot.add(studentFind);
studentRoot.add(studentModify);
teacherRoot.add(teacherRegist);
teacherRoot.add(teacherFind);
teacherRoot.add(teacherModify);
root.add(studentRoot);
root.add(teacherRoot);
//根据根节点构造jtree对象
tree = new JTree(root);
}
//给树添加事件处理功能
tree.addTreeSelectionListener(new TreeAction(this));
return tree;
}
/**
* 构造tabbedpane对象
*
* @return
*/
public JTabbedPane buildTabbedPane() {
if (tab == null) {
tab = new JTabbedPane();
// 将学生注册面板添加到tabbedpane中
tab.addTab("学生信息注册", new StudentRegistPanel());
tab.addTab("学生信息查询", new StudentFindPanel());
tab.addTab("学生信息维护", new StudentModifyPanel());
}
return tab;
}
public JPanel buildPane(String name) {
JPanel panel = new JPanel();
return panel;
}
/**
* 根据传入的菜单项名和快捷键构造相应的学生菜单项
*
* @param name
* @param mnemonic
* @return
*/
private JMenuItem buildStudentMenuItem(String name, char mnemonic) {
JMenuItem menuItem = new JMenuItem(name);
// 设置快捷键字符
menuItem.setMnemonic(mnemonic);
// 设置快捷键组合操作方式(CTRL+SHIFT+字符)
menuItem.setAccelerator(KeyStroke.getKeyStroke(mnemonic,
java.awt.Event.CTRL_MASK | java.awt.Event.SHIFT_MASK, false));
actionListener = new BtnActionOper(this);
menuItem.addActionListener(actionListener);
return menuItem;
}
/**
* 根据传入的菜单项名和快捷键构造相应的老师菜单项
*
* @param name
* @param mnemonic
* @return
*/
private JMenuItem buildTeacherMenuItem(String name, char mnemonic) {
JMenuItem menuItem = new JMenuItem(name);
// 设置快捷键字符
menuItem.setMnemonic(mnemonic);
// 设置快捷键组合操作方式(ALT+SHIFT+字符)
menuItem.setAccelerator(KeyStroke.getKeyStroke(mnemonic,
java.awt.Event.ALT_MASK | java.awt.Event.SHIFT_MASK, false));
return menuItem;
}
/**
* 根据传入的菜单名和快捷键构造学生信息操作菜单
*
* @param name
* @param mnemonic
* @return
*/
private JMenu buildStudentMenu(String name) {
JMenu menu = new JMenu(name);
menu.add(buildStudentMenuItem("学生信息注册", 'N'));
// 添加分割条
menu.addSeparator();
menu.add(buildStudentMenuItem("学生信息查询", 'F'));
menu.addSeparator();
menu.add(buildStudentMenuItem("学生信息维护", 'M'));
return menu;
}
/**
* 根据传入的菜单名和快捷键构造老师信息操作菜单
*
* @param name
* @param mnemonic
* @return
*/
private JMenu buildTeacherMenu(String name) {
JMenu menu = new JMenu(name);
menu.add(buildTeacherMenuItem("老师信息注册", 'N'));
// 添加分割条
menu.addSeparator();
menu.add(buildTeacherMenuItem("老师信息查询", 'F'));
menu.addSeparator();
menu.add(buildTeacherMenuItem("老师信息维护", 'M'));
return menu;
}
/**
* 构造菜单栏
*
* @return
*/
public JMenuBar buildMenuBar() {
menuBar = new JMenuBar();
menuBar.add(buildStudentMenu("学生信息管理"));
menuBar.add(buildTeacherMenu("老师信息管理"));
return menuBar;
}
public JToolBar buildToolBar() {
toolBar = new JToolBar();
toolBar.add(buildToolBtn("学生信息注册", "icons/justify24.gif"));
toolBar.add(buildToolBtn("学生信息查询", "icons/import24.gif"));
toolBar.add(buildToolBtn("学生信息维护", "icons/export24.gif"));
toolBar.add(buildToolBtn("老师信息注册", "icons/left24.gif"));
toolBar.add(buildToolBtn("老师信息查询", "icons/paste24.gif"));
toolBar.add(buildToolBtn("老师信息维护", "icons/right24.gif"));
toolBar.add(buildToolBtn("退出系统", "icons/exit24.gif"));
return toolBar;
}
/**
* 构造按钮
*
* @param name
* @param icons
* @return
*/
public JButton buildToolBtn(String name, String icons) {
JButton btn = new JButton(name);
btn.setIcon(new ImageIcon(icons));
actionListener = new BtnActionOper(this);
btn.setToolTipText(name);
btn.addActionListener(actionListener);
return btn;
}
/**
* @param args
*/
public static void main(String[] args) {
try {
// UIManager.setLookAndFeel("com.nilo.plaf.nimrod.NimRODLookAndFeel");
UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e) {
}
new MainFrame();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -