Java版本:Mars.2 Release (4.5.2)
SWT下載的jar包:
http://download.eclipse.org/windowbuilder/WB/release/R201506241200-1/4.5/(不同版本的eclipse對應不同的版本,在此不再贅述install過程)
1.插入圖片
在要插入圖片的框架如Jpanel下編寫如下代碼
protected void paintComponent(Graphicsarg0) {
super.paintComponent(arg0);
Image bgImg = null;
try {
bgImg = ImageIO.read(new File("C:/Users/lenovo/Desktop/20140213111544852.jpg"));//這裏是圖片路徑
} catch (IOExceptione) {
e.printStackTrace();
}
arg0.drawImage(bgImg, 0, 0,null);
}
有圖片整個界面更加美觀
2.通過對UIManager的設置改變應用程序的外觀
public static void main(String[]args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());// 這裏使用系統外觀
} catch (Exceptione1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
利用swing.UIManager可以做出如xp風格的外觀,界面設計的話美觀很重要
3.固定窗體的大小
frame.setResizable(false);//false就是窗口大小不可改變,true就是能改變
這樣就能固定窗體的大小,不會出現來回拉動的窘迫
4.選擇文件窗口
JFileChooser jfc=new JFileChooser(); //選擇文件
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES ); jfc.showDialog(new JLabel(),"選擇");
File file=jfc.getSelectedFile();
textField.setText(file.getAbsolutePath());//文本框顯示文件路徑
效果如下:
5.保存文件窗口
JFileChooser jfc2 = new JFileChooser();//設置默認的保存文件名稱,注意如果這樣設置了之後,後面就無法取得用戶改變之後的文件存儲路徑了,因爲後面調用的 jFileChooser.getCurrentDirectory()獲取的路徑就是你設置的默認文件的路徑
jfc2.setSelectedFile(new File("詞頻統計結果"+".txt"));
int result =jfc2.showSaveDialog(null);
File file2=jfc2.getSelectedFile();
textField_1.setText(file2.getAbsolutePath());//文本框顯示文件路徑
效果如下:
6.一些小tips
設置字體
lblNewLabel.setFont(new Font("宋體", Font.PLAIN, 18));
ButtonGroup實現多選一
ButtonGroup filetype = new ButtonGroup();
filetype.add(rdbtnDoc);
filetype.add(rdbtnTxt);
filetype.add(rdbtnXls);
filetype.add(rdbtnXlsx);
效果如下:
暫時先寫這麼多,這個項目主要難在算法上,這個就是後話了。學習Java以來感覺很有動力,比較好理解,這也算Java有魅力的一面吧。我只是個初學者,分享自己的一點心得,歡迎點評。