1.5 主窗體設(shè)計(jì)
主窗體界面也是該系統(tǒng)的歡迎界面。應(yīng)用程序的主窗體必須設(shè)計(jì)層次清晰的系統(tǒng)菜單和工具欄,其中系統(tǒng)菜單包含系統(tǒng)中所有功能的菜單項(xiàng),而工具欄主要提供常用功能的快捷訪問(wèn)按鈕。企業(yè)進(jìn)銷存管理系統(tǒng)采用導(dǎo)航面板,綜合了系統(tǒng)菜單和工具欄的優(yōu)點(diǎn),而且其界面更加美觀,操作更快捷。主窗體的運(yùn)行效果如圖1.20所示。
1.5.1 創(chuàng)建主窗體
創(chuàng)建主窗體的步驟如下:
(1)創(chuàng)建JXCFrame類,在類中創(chuàng)建并初始化窗體對(duì)象,為窗體添加桌面面板,并設(shè)置背景圖片。關(guān)鍵代碼如下:
例程01 代碼位置:光盤\TM\01\JXCManager\src\com\lzw\JXCFrame.java
private JDesktopPane desktopPane;
private JFrame frame;
private JLabel backLabel;
private Preferences preferences;
//創(chuàng)建窗體的Map類型集合對(duì)象
private Map<String, JInternalFrame> ifs = new HashMap<String, JInternalFrame>();
public JXCFrame() {
frame = new JFrame("企業(yè)進(jìn)銷存管理系統(tǒng)"); //創(chuàng)建窗體對(duì)象
frame.addComponentListener(new FrameListener()); //添加窗體事件監(jiān)聽(tīng)器
frame.getContentPane().setLayout(new BorderLayout()); //設(shè)置布局管理器
frame.setBounds(100, 100, 800, 600); //設(shè)置窗體位置和大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設(shè)置窗體默認(rèn)的關(guān)閉方式
backLabel = new JLabel(); //背景標(biāo)簽
backLabel.setVerticalAlignment(SwingConstants.TOP); //設(shè)置背景標(biāo)簽垂直對(duì)齊方式
backLabel.setHorizontalAlignment(SwingConstants.CENTER); //設(shè)置背景標(biāo)簽水平對(duì)齊方式
updateBackImage(); //調(diào)用初始化背景標(biāo)簽的方法
desktopPane = new JDesktopPane(); //創(chuàng)建桌面面板
desktopPane.add(backLabel, new Integer(Integer.MIN_VALUE)); //將背景標(biāo)簽添加到桌面面板中
frame.getContentPane().add(desktopPane); //添加桌面面板到窗體中
JTabbedPane navigationPanel = createNavigationPanel(); //創(chuàng)建導(dǎo)航面板
frame.getContentPane().add(navigationPanel, BorderLayout.NORTH); //添加導(dǎo)航面板到窗體中
frame.setVisible(true); //顯示窗體
}
(2)編寫updateBackImage()方法,在該方法中初始化背景標(biāo)簽,背景標(biāo)簽使用HTML超文本語(yǔ)言設(shè)置了主窗體的背景圖片,該圖片將隨主窗體的大小自動(dòng)縮放。關(guān)鍵代碼如下:
例程02 代碼位置:光盤\TM\01\JXCManager\src\com\lzw\JXCFrame.java
private void updateBackImage() {
if (backLabel != null) {
int backw = JXCFrame.this.frame.getWidth();
int backh = frame.getHeight();
backLabel.setSize(backw, backh); //初始化背景標(biāo)簽的大小
backLabel.setText("<html><body><image width='" + backw
+ "' height='" + (backh - 110) + "' src="
+ JXCFrame.this.getClass().getResource("welcome.jpg")
+ "'></img></body></html>"); //設(shè)置背景標(biāo)簽的圖像
}
}
(3)在類的靜態(tài)代碼段中設(shè)置進(jìn)銷存管理系統(tǒng)的外觀樣式。Swing支持跨平臺(tái)特性,它可以在不同的操作系統(tǒng)中保持一致的外觀風(fēng)格,但是本系統(tǒng)使用UIManager類的setLookAndFeel()方法設(shè)置程序界面使用本地外觀,這樣可以使程序更像本地應(yīng)用程序。關(guān)鍵代碼如下:
例程03 代碼位置:光盤\TM\01\JXCManager\src\com\lzw\JXCFrame.java
static {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
}
(4)編寫主窗體的main()入口方法,在該方法中創(chuàng)建登錄窗體對(duì)象,登錄窗體會(huì)驗(yàn)證登錄信息,并顯示主窗體界面。關(guān)鍵代碼如下:
例程04 代碼位置:光盤\TM\01\JXCManager\src\com\lzw\JXCFrame.java
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Login();
}
});
}
1.5.2 創(chuàng)建導(dǎo)航面板
創(chuàng)建導(dǎo)航面板的步驟如下:
(1)在JXCFrame類中編寫createNavigationPanel()方法,在該方法中創(chuàng)建JTabbedPane選項(xiàng)卡面板對(duì)象;為突出選項(xiàng)卡的立體效果,設(shè)置其使用BevelBorder邊框效果;然后依次創(chuàng)建“基礎(chǔ)信息管理”、“庫(kù)存管理”、“銷售管理”、“查詢統(tǒng)計(jì)”、“進(jìn)貨管理”和“系統(tǒng)管理”選項(xiàng)卡。關(guān)鍵代碼如下:
例程05 代碼位置:光盤\TM\01\JXCManager\src\com\lzw\JXCFrame.java
private JTabbedPane createNavigationPanel() { //創(chuàng)建導(dǎo)航面板的方法
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setFocusable(false);
tabbedPane.setBackground(new Color(211, 230, 192));
tabbedPane.setBorder(new BevelBorder(BevelBorder.RAISED));
JPanel baseManagePanel = new JPanel(); //“基礎(chǔ)信息管理”面板
baseManagePanel.setBackground(new Color(215, 223, 194));
baseManagePanel.setLayout(new BoxLayout(baseManagePanel, BoxLayout.X_AXIS));
baseManagePanel.add(createFrameButton("客戶信息管理", "KeHuGuanLi"));
baseManagePanel.add(createFrameButton("商品信息管理", "ShangPinGuanLi"));
baseManagePanel.add(createFrameButton("供應(yīng)商信息管理", "GysGuanLi"));
JPanel depotManagePanel = new JPanel(); //“庫(kù)存管理”面板
depotManagePanel.setBackground(new Color(215, 223, 194));
depotManagePanel.setLayout(new BoxLayout(depotManagePanel, BoxLayout.X_AXIS));
depotManagePanel.add(createFrameButton("庫(kù)存盤點(diǎn)", "KuCunPanDian"));
depotManagePanel.add(createFrameButton("價(jià)格調(diào)整", "JiaGeTiaoZheng"));
JPanel sellManagePanel = new JPanel(); //“銷售管理”面板
sellManagePanel.setBackground(new Color(215, 223, 194));
sellManagePanel.setLayout(new BoxLayout(sellManagePanel, BoxLayout.X_AXIS));
sellManagePanel.add(createFrameButton("銷售單", "XiaoShouDan"));
sellManagePanel.add(createFrameButton("銷售退貨", "XiaoShouTuiHuo"));
JPanel searchStatisticPanel = new JPanel(); //“查詢統(tǒng)計(jì)”面板
searchStatisticPanel.setBounds(0, 0, 600, 41);
searchStatisticPanel.setName("searchStatisticPanel");
searchStatisticPanel.setBackground(new Color(215, 223, 194));
searchStatisticPanel.setLayout(new BoxLayout(searchStatisticPanel, BoxLayout.X_AXIS));
searchStatisticPanel.add(createFrameButton("客戶信息查詢", "KeHuChaXun"));
searchStatisticPanel.add(createFrameButton("商品信息查詢", "ShangPinChaXun"));
searchStatisticPanel.add(createFrameButton("供應(yīng)商信息查詢","GongYingShangChaXun"));
searchStatisticPanel.add(createFrameButton("銷售信息查詢", "XiaoShouChaXun"));
searchStatisticPanel.add(createFrameButton("銷售退貨查詢","XiaoShouTuiHuoChaXun"));
searchStatisticPanel.add(createFrameButton("入庫(kù)查詢", "RuKuChaXun"));
searchStatisticPanel.add(createFrameButton("入庫(kù)退貨查詢", "RuKuTuiHuoChaXun"));
searchStatisticPanel.add(createFrameButton("銷售排行", "XiaoShouPaiHang"));
JPanel stockManagePanel = new JPanel(); //“進(jìn)貨管理”面板
stockManagePanel.setBackground(new Color(215, 223, 194));
stockManagePanel.setLayout(new BoxLayout(stockManagePanel, BoxLayout.X_AXIS));
stockManagePanel.add(createFrameButton("進(jìn)貨單", "JinHuoDan"));
stockManagePanel.add(createFrameButton("進(jìn)貨退貨", "JinHuoTuiHuo"));
JPanel sysManagePanel = new JPanel(); //“系統(tǒng)管理”面板
sysManagePanel.setBackground(new Color(215, 223, 194));
sysManagePanel.setLayout(new BoxLayout(sysManagePanel, BoxLayout.X_AXIS));
sysManagePanel.add(createFrameButton("操作員管理", "CzyGL"));
sysManagePanel.add(createFrameButton("更改密碼", "GengGaiMiMa"));
sysManagePanel.add(createFrameButton("權(quán)限管理", "QuanManager"));
//將所有面板添加到導(dǎo)航面板中
tabbedPane.addTab(" 基礎(chǔ)信息管理 ", null, baseManagePanel, "基礎(chǔ)信息管理");
tabbedPane.addTab(" 進(jìn)貨管理 ", null, stockManagePanel, "進(jìn)貨管理");
tabbedPane.addTab(" 銷售管理 ", null, sellManagePanel, "銷售管理");
tabbedPane.addTab(" 查詢統(tǒng)計(jì) ", null, searchStatisticPanel, "查詢統(tǒng)計(jì)");
tabbedPane.addTab(" 庫(kù)存管理 ", null, depotManagePanel, "庫(kù)存管理");
tabbedPane.addTab(" 系統(tǒng)管理 ", null, sysManagePanel, "系統(tǒng)管理");
return tabbedPane;
}