准備就以用戶主界面作為例子,記錄JAVA Swing編寫界面的主要方法。
主界面大致如這個樣子,先說說左邊的導航面板制作:

中間使用一個JSplitPane將屏幕一份為二,左邊為導航面板,右邊是實際操作界面。JSplitPane有點坑的地方就是要在界面setVisible(true)生效之后才能生效。也就是說要這么寫:
private JSplitPane splitPane = new JSplitPane(); public userMainFrm (User curUser) //構造函數 { //分屏 this.getContentPane().add(splitPane, java.awt.BorderLayout.CENTER); splitPane.add(TutorJp, JSplitPane.LEFT); //放置一個底層容器TutorJP //該干嘛干嘛…… //設置JFrame最大化,要放在setVisible之后才能刷新,否則只執行一次 this.setExtendedState(Frame.MAXIMIZED_BOTH); fillBookType();//初始化下拉框 setVisible(true); splitPane.setDividerLocation(0.25);//設定分割面板的左右比例(這時候就生效了,如果放在setVisible(true)這據之前就不會有效果。 splitPane.setEnabled(false); //設置分隔條禁止拖動 }
左邊當然要先加一個底層容器Jpanel,在其上面分別放上四個JinternalFrame划分成四塊。每一個JinternalFrame都去掉標題欄和邊框(由於分辨率問題,只能去掉它),然后每一個JInternalFrame放置一個帶標題的Jpanel.故而其實可以不要JinternalFrame,就像最后一個退出按鈕ExitJPL所在的容器一樣。
TutorJp.setLayout(gl_TutorJp); //setBounds(100, 100, 762, 1120); //創立三個內窗口 JInternalFrame SearchJIF = new JInternalFrame("\u56FE\u4E66\u67E5\u8BE2\u4E0E\u501F\u9605"); SearchJIF.getContentPane().setFont(new Font("宋體", Font.PLAIN, 35)); BasicInternalFrameUI ui = (BasicInternalFrameUI)SearchJIF.getUI(); ui.setNorthPane(null); SearchJIF.setBorder(BorderFactory.createEmptyBorder()); SearchJIF.setVisible(true);
JInternalFrame ID_JIF = new JInternalFrame("\u4E2A\u4EBA\u4FE1\u606F\u8BBE\u7F6E"); //去邊框和標題欄 BasicInternalFrameUI ui_1 = (BasicInternalFrameUI)ID_JIF.getUI(); ui_1.setNorthPane(null); ID_JIF.setBorder(BorderFactory.createEmptyBorder()); ID_JIF.getContentPane().setFont(new Font("宋體", Font.PLAIN, 35)); ID_JIF.setVisible(true);
JInternalFrame RecomJIF = new JInternalFrame("\u63A8\u8350\u4FE1\u606F"); RecomJIF.getContentPane().setFont(new Font("宋體", Font.PLAIN, 35)); //去邊框和標題欄 BasicInternalFrameUI RecomUI = (BasicInternalFrameUI)RecomJIF.getUI(); RecomUI.setNorthPane(null); RecomJIF.setBorder(BorderFactory.createEmptyBorder()); JPanel RecommendJP = new JPanel(); RecommendJP.setBorder(new TitledBorder(null, "\u63A8\u8350\u4FE1\u606F", TitledBorder.LEADING, TitledBorder.TOP, new Font("宋體", Font.PLAIN, 35), null));
JPanel ExitJPL = new JPanel();
//使用帶標題的邊框 ExitJPL.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u5B89\u5168\u9000\u51FA\u7CFB\u7EDF", TitledBorder.LEADING, TitledBorder.TOP, new Font("宋體", Font.PLAIN, 35), Color.DARK_GRAY));
當然這個帶標題的邊框代碼是由WindowBuider生成的,在里面選擇相應屬性就好了。在左邊的屬性里面若是點擊了相應的Jpanel,有一個border選項。
接下來是這次記錄的重點——如何插入圖片。
不管是Jbutton里面的圖片還是用一個JLabel容納圖片都一樣。那三張標題欄下面的圖片是這樣添加的:
//標簽 JLabel IDJLB = new JLabel("",SwingConstants.CENTER); IDJLB.setFont(new Font("宋體", Font.PLAIN, 35)); JLabel RecomJLb = new JLabel("",SwingConstants.CENTER); RecomJLb.setBounds(254, 41, 256, 73); RecomJLb.setFont(new Font("宋體", Font.PLAIN, 35)); RecommendJP.add(RecomJLb); JLabel Daily_Op = new JLabel("",SwingConstants.CENTER); Daily_Op.setBounds(247, 43, 264, 91); Daily_Op.setFont(new Font("宋體", Font.PLAIN, 35)); //圖片准備 int width = 300,height = 100; //這是圖片和JLable的寬度和高度 ImageIcon icon = new ImageIcon(userMainFrm.class.getResource("/res/dayly_op.png")); ImageIcon ID_icon = new ImageIcon(userMainFrm.class.getResource("/res/ID_Info.png")); // ImageIcon Recom_icon = new ImageIcon(userMainFrm.class.getResource("/res/Recommend.png"));// //改變圖片大小 icon.setImage(icon.getImage().getScaledInstance(width,height,Image.SCALE_DEFAULT));//80和100為大小 可以自由設置 ID_icon.setImage(ID_icon.getImage().getScaledInstance(width,height,Image.SCALE_DEFAULT)); Recom_icon.setImage(Recom_icon.getImage().getScaledInstance(width,height,Image.SCALE_DEFAULT)); //添加圖片 Daily_Op.setIcon(icon); IDJLB.setIcon(ID_icon); RecomJLb.setIcon(Recom_icon);
要注意的是圖片路徑一定要使用相對路徑:
ImageIcon icon = new ImageIcon(userMainFrm.class.getResource("/res/dayly_op.png"));
否則在debug時候可能並不會出現問題,但是一旦打包成可執行jar包,就呵呵了 ,找不到圖片。。。
而且可能即使使用了相對路徑之后,打包成可執行jar包后報出一個空指針錯誤,這個時候可能是圖片的數據流轉換成二進制流后出錯了,我就從新換了一次圖片就行 了。
如果是按鈕Jbutton 加入圖片直接使用WindowBuilder好了,生成如下代碼:
JButton BorrowButton = new JButton("\u56FE\u4E66\u501F\u9605"); BorrowButton.setIcon(new ImageIcon(userMainFrm.class.getResource("/manager/image/return_book.png")));
還有另一件像說的,就是關於按鈕的相應函數。這個時候用WindowBuilder,右擊相應的按鈕,選擇actionPerformed,選擇第一個就好了。出現如下代碼:
BorrowButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { borrowActionPerformed(evt);//自己的響應函數 } });
關於那個歡迎那行字,就采取極簡單的獲取登錄時候的時間,判斷上下午。要是嚴謹一點應該搞一個監聽,一旦時間由上午變成下午,要刷新。由於時間關系,這些都不是數據庫項目的重點,后不管了。
//歡迎標簽 DateUtil.getdateWithMinute(date); String welcomeString; if(date.getHour()<12) { welcomeString="上午好,"+this.presentUser.getUserName(); } else { welcomeString="下午好,"+this.presentUser.getUserName(); } JLabel WwlcomeJL = new JLabel(welcomeString); WwlcomeJL.setFont(new Font("華文行楷", Font.PLAIN, 40));
置於獲取時間函數,之后再說。。。。
