一个简单的桌面应用程序框架示例(Java SE9)
1. 创建主窗体界面
package com.ys; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.KeyStroke; import ys.com.internalFrame.SyssetInternalFrame; public class MainFrm extends JFrame implements ActionListener{ JDesktopPane desktop; //声明系统主菜单 static final String SYS="系统"; static final String STORE="仓库管理"; static final String PURCHARCE="采购管理"; static final String BANFANG="板房管理"; //声明系统子菜单项 //1.系统子菜单项 static final String SYS_SET="系统设置"; static final String SYS_UMANAGER="用户管理"; static final String SYS_EXIT="退出系统"; //2.仓库子菜单项 static final String STORE_BUBAN="布板管理"; static final String STORE_IN="入库管理"; static final String STORE_OUT="出库管理"; static final String STORE_ORDER="物料订购单"; static final String STORE_RETURN="物料回货单"; public MainFrm() { super("YS_Main"); int inset=50; Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize(); setBounds(inset,inset,screenSize.width-inset*2,screenSize.height-inset*2); //创建图形用户界面 desktop=new JDesktopPane(); setContentPane(desktop); setJMenuBar(createMenuBar()); //设置拖放效果 desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE); } private static void createAndShowGUI() { javax.swing.JFrame.setDefaultLookAndFeelDecorated(true); MainFrm frame =new MainFrm(); //实例化主窗体界面 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); //显示窗体 } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } //创建主菜单项方法createMenu() public JMenu createMenu(String name,int c,JMenuBar menuBar) { JMenu menu=new JMenu(name); menu.setMnemonic(c); menuBar.add(menu); return menu; } //创建子菜单项方法createMenuItem() public void createMenuItem(String name,int keyCode,JMenu menu,boolean sep) { JMenuItem menuItem=new JMenuItem(name); menuItem.setMnemonic(keyCode); menuItem.setAccelerator(KeyStroke.getKeyStroke(keyCode,ActionEvent.ALT_MASK)); menuItem.setActionCommand(name); menuItem.addActionListener(this); menu.add(menuItem); if(sep) menu.addSeparator();//添加分割线 } //定义创建菜单栏的方法createMenuBar() protected JMenuBar createMenuBar() { JMenuBar menuBar=new JMenuBar(); //定义菜单栏工具对象并实例化 //添加菜单项,并初始化菜单项 JMenu menuSYS=createMenu(SYS,KeyEvent.VK_S,menuBar); JMenu menuSTORE=createMenu(STORE,KeyEvent.VK_C,menuBar); createMenu(PURCHARCE,KeyEvent.VK_P,menuBar); createMenu(BANFANG,KeyEvent.VK_B,menuBar); //添加子菜单项 //1.实例化系统子菜单项 createMenuItem(SYS_SET,KeyEvent.VK_0,menuSYS,false); createMenuItem(SYS_UMANAGER,KeyEvent.VK_1,menuSYS,false); createMenuItem(SYS_EXIT,KeyEvent.VK_X,menuSYS,false); //2.实例化仓库管理子菜单项 createMenuItem(STORE_BUBAN,KeyEvent.VK_B,menuSTORE,false); createMenuItem(STORE_IN,KeyEvent.VK_I,menuSTORE,false); createMenuItem(STORE_OUT,KeyEvent.VK_O,menuSTORE,true); createMenuItem(STORE_ORDER,KeyEvent.VK_D,menuSTORE,false); createMenuItem(STORE_RETURN,KeyEvent.VK_R,menuSTORE,false); return menuBar; } public void actionPerformed(ActionEvent e) { if(SYS_SET.equals(e.getActionCommand())) createSysSetFrame(); if(SYS_EXIT.equals(e.getActionCommand())) quit(); } protected void createSysSetFrame() {//打开用户设置窗体 SyssetInternalFrame frame = new SyssetInternalFrame(); frame.setVisible(true); //necessary as of 1.3 desktop.add(frame); try { frame.setSelected(true); } catch (java.beans.PropertyVetoException e) {} } protected void quit() { System.exit(0); //退出系统 } }
2. 建立一个带有笑脸的简单子窗体
package com.ys.internalFrame; import java.awt.Container; import javax.swing.JInternalFrame; public class SyssetInternalFrame extends JInternalFrame { static int openFrameCount = 0; static final int xOffset = 30, yOffset = 30; public SyssetInternalFrame() { super("系统设置",// + (++openFrameCount), true, //resizable true, //closable true, //maximizable true);//iconifiable //...Create the GUI and put it in the window... Container cp=getContentPane(); FaceBean faceBean=new FaceBean(); faceBean.setMouthWidth(60); faceBean.smile(); //faceBean.frown(); cp.add(faceBean); //...Then set the window size or call pack... setSize(500,300); //Set the window's location. setLocation(xOffset*openFrameCount, yOffset*openFrameCount); } }
3. 定义笑脸控件类
package com.ys.entity; import java.awt.*; import javax.swing.*; public class FaceBean extends JComponent { private int mMouthWidth = 90; //初始化嘴巴默认宽度 private boolean mSmile = true; //默认情况显示笑脸 @Override public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 脸 int w = getWidth(); int h = getHeight(); int pad = 12; int cw = w - pad * 2; int ch = h - pad * 2; g2.setColor(getBackground()); g2.fillArc(pad, pad, cw, ch, 0, 360); g2.setColor(getForeground()); g2.drawArc(pad, pad, cw, ch, 0, 360); // 嘴 int sw = cw / 2; int sh = ch / 2; if (mSmile == true) g2.drawArc(w / 2 - sw / 2, h / 2 - sh / 2, sw, sh, 270 - mMouthWidth / 2, mMouthWidth); else g2.drawArc(w / 2 - sw / 2, h / 2 + sh / 3, sw, sh, 90 - mMouthWidth / 2, mMouthWidth); // 眼睛 int er = 4; g2.fillArc(w / 2 - cw * 1 / 8 - er / 2, h / 2 - ch / 4 - er, er, er, 0, 360); g2.fillArc(w / 2 + cw * 1 / 8 - er / 2, h / 2 - ch / 4 - er, er, er, 0, 360); } public int getMouthWidth() { return mMouthWidth; } //设置嘴巴宽度的方法 public void setMouthWidth(int mw) { mMouthWidth = mw; repaint(); } //显示笑脸的方法 public void smile() { mSmile = true; repaint(); } //显示苦脸的方法 public void frown() { mSmile = false; repaint(); } }