java的一些课程设计题目_Java课程设计
Java課程設計
1. 題目及要求
基于學校的搜索引擎
負責部分:Java GUI設計
2.界面調查
1)調查界面:百度
2)思考:
根據我的調查,我認為我需要完成三個界面的設計:
第一個是調查主界面,里面有一個集美大學的logo,一個搜索框用文本字段,因為需要在里面寫入搜索內容,一個搜索按鈕用button,這個按鈕完成的功能就是輸入搜索內容后,點擊搜索按鈕,可以跳轉到下一個界面,并且返回結果。
第二個界面是搜索結果界面:需要的是一個再次搜索框,用文本字段;一個再次搜索按鈕,用button;一個面板JPanel,用來盛放我搜索到的結果;在界面的最底下還有三個按鈕,一個文本框,分別是:上一頁,下一頁,跳轉,和相應頁面表示,當數據量過大需要分頁時,就是用來實現頁面的跳轉的。
第三個就是結果展示界面:一個jLabel,展示標題;一個文本區域展示內容;一個按鈕,點擊能夠跳轉到原網頁瀏覽。
3.我的代碼
1.EsGuiSearch.java
package edu.net.itsearch.gui;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import crawler.SearchResultEntry;
import edu.net.itsearch.elasticsearch.EsClient;
import io.searchbox.client.JestClient;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
/**
*
* @author xingkyh
*/
public class EsGuiSearch {
private JestClient jestClient;
public EsGuiSearch() {
this.jestClient=EsClient.getJestClient();
}
/**
* 全文檢索
*
* @param queryString 搜索字符串
* @return 檢索結果
*/
public List fullTextSerch(String queryString) {
// 聲明一個搜索請求體
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.queryStringQuery(queryString));
searchSourceBuilder.query(boolQueryBuilder);
// 設置分頁
searchSourceBuilder.from(0);
searchSourceBuilder.size(800);
// 構建Search對象
Search search = new Search.Builder(searchSourceBuilder.toString()).addIndex(EsClient.indexName)
.addType(EsClient.typeName).build();
SearchResult searchResult = null;
try {
searchResult = jestClient.execute(search);
} catch (IOException e) {
e.printStackTrace();
}
List list=new ArrayList();
List> hits = searchResult.getHits(SearchResultEntry.class);
for (SearchResult.Hit hit : hits) {
list.add(hit.source);
}
return list;
}
public void close() throws IOException {
EsClient.closeJestClient();
}
}
2.SearchMainPage.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package edu.net.itsearch.gui;
import java.util.List;
import javax.swing.JOptionPane;
import crawler.SearchResultEntry;
/**
*
* @author 格格
*/
public class SearchMainPage extends javax.swing.JFrame {
/**
* Creates new form searchMainPage
*/
public SearchMainPage() {
initComponents();
esGuiSearch=new EsGuiSearch();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
private void initComponents() {
searchBox = new javax.swing.JTextField();
searchButton = new javax.swing.JButton();
picture = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
searchButton.setText("搜索");
searchButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
searchButtonActionPerformed(evt);
}
});
picture.setIcon(new javax.swing.ImageIcon(getClass().getResource("jmu.png")));
picture.setText("jLabel2");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(68, 68, 68)
.addComponent(searchBox, javax.swing.GroupLayout.PREFERRED_SIZE, 516, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(searchButton, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(picture, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(256, 256, 256))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(46, 46, 46)
.addComponent(picture)
.addGap(36, 36, 36)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(searchBox, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(searchButton, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(140, Short.MAX_VALUE))
);
pack();
}//
private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {
String queryString=searchBox.getText();
List list = esGuiSearch.fullTextSerch(queryString);
if(list.isEmpty()) {
JOptionPane.showMessageDialog(null, "未搜索到相關內容!");
}else {
SearchResult searchResult = new SearchResult(list);
searchResult.setVisible(true);
dispose();
}
}
private EsGuiSearch esGuiSearch;
private javax.swing.JLabel picture;
private javax.swing.JTextField searchBox;
private javax.swing.JButton searchButton;
// End of variables declaration
}
3.SearchResult.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package edu.net.itsearch.gui;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import crawler.SearchResultEntry;
/**
*
* @author 格格
*/
public class SearchResult extends javax.swing.JFrame {
/**
* Creates new form searchResult
*/
public SearchResult() {
initComponents();
}
public SearchResult(List list){
initComponents();
esGuiSearch=new EsGuiSearch();
resultList = getJpanelList(list);
resultNum = list.size();
pageNum = (resultList.size()+1)/2;
currentPage = 1;
displayResult();
}
private void displayResult(){
resultJpanel.removeAll();
resultJpanel.setLayout(new GridLayout(2, 1));
resultJpanel.add(resultList.get(currentPage*2-2));
if(currentPage+currentPage <= resultNum){
resultJpanel.add(resultList.get(currentPage*2-1));
}
resultJpanel.revalidate();
resultJpanel.repaint();
page.setText(currentPage+"/"+pageNum);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
private void initComponents() {
searchAgainButton = new javax.swing.JButton();
resultJpanel = new javax.swing.JPanel();
jumpLastPage = new javax.swing.JButton();
jumpNextPage = new javax.swing.JButton();
jumpChoosePage = new javax.swing.JButton();
searchAgainBox = new javax.swing.JTextField();
page = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
searchAgainButton.setText("搜索");
searchAgainButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
searchAgainButtonPerformed(evt);
}
});
javax.swing.GroupLayout resultJpanelLayout = new javax.swing.GroupLayout(resultJpanel);
resultJpanel.setLayout(resultJpanelLayout);
resultJpanelLayout.setHorizontalGroup(
resultJpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
resultJpanelLayout.setVerticalGroup(
resultJpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 276, Short.MAX_VALUE)
);
jumpLastPage.setText("上一頁");
jumpLastPage.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jumpLastPageActionPerformed(evt);
}
});
jumpNextPage.setText("下一頁");
jumpNextPage.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jumpNextPageActionPerformed(evt);
}
});
jumpChoosePage.setText("頁數跳轉");
jumpChoosePage.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jumpChoosePageActionPerformed(evt);
}
});
searchAgainBox.setText("");
page.setText("1");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(page, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jumpLastPage)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jumpNextPage)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jumpChoosePage)
.addGap(12, 12, 12))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(resultJpanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(searchAgainBox, javax.swing.GroupLayout.PREFERRED_SIZE, 584, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(searchAgainButton, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 26, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(searchAgainBox, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(searchAgainButton, javax.swing.GroupLayout.DEFAULT_SIZE, 48, Short.MAX_VALUE))
.addGap(27, 27, 27)
.addComponent(resultJpanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jumpLastPage)
.addComponent(jumpNextPage)
.addComponent(jumpChoosePage)
.addComponent(page, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
pack();
}//
private void searchAgainButtonPerformed(java.awt.event.ActionEvent evt) {
String queryString=searchAgainBox.getText();
List list=esGuiSearch.fullTextSerch(queryString);
if(list.isEmpty()) {
JOptionPane.showMessageDialog(null, "未搜索到相關內容!");
}else {
resultList = getJpanelList(list);
resultNum = list.size();
pageNum = (resultList.size()+1)/2;
currentPage = 1;
displayResult();
}
}
private void jumpLastPageActionPerformed(java.awt.event.ActionEvent evt) {
if(currentPage == 1){
JOptionPane.showMessageDialog(null, "當前已為第一頁,無法進入上一頁!");
}else{
currentPage--;
displayResult();
}
}
private void jumpNextPageActionPerformed(java.awt.event.ActionEvent evt) {
if(currentPage == pageNum){
JOptionPane.showMessageDialog(null, "當前已為最后一頁,無法進入下一頁!");
}else{
currentPage++;
displayResult();
}
}
private void jumpChoosePageActionPerformed(java.awt.event.ActionEvent evt) {
int jumpPage = Integer.valueOf(page.getText());
if(jumpPage >= 1 && jumpPage <= pageNum){
currentPage = jumpPage;
displayResult();
}else{
JOptionPane.showMessageDialog(null, "輸入頁數不合法,請輸入1-"+pageNum+"中的數字");
}
}
private List getJpanelList(List list) {
List resultList = new ArrayList<>();
for(SearchResultEntry e:list){
JPanel jPanel=new SearchLook(e);
resultList.add(jPanel);
}
return resultList;
}
private List resultList;
private int pageNum;
private int currentPage;
private int resultNum;
private EsGuiSearch esGuiSearch;
private javax.swing.JButton jumpChoosePage;
private javax.swing.JButton jumpLastPage;
private javax.swing.JButton jumpNextPage;
private javax.swing.JTextField page;
private javax.swing.JPanel resultJpanel;
private javax.swing.JTextField searchAgainBox;
private javax.swing.JButton searchAgainButton;
// End of variables declaration
}
4.SearchLook.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package edu.net.itsearch.gui;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;
import crawler.SearchResultEntry;
/**
*
* @author 格格
*/
public class SearchLook extends javax.swing.JPanel {
/**
* Creates new form SearchLook
*/
public SearchLook() {
initComponents();
}
public SearchLook(SearchResultEntry result) {
initComponents();
titleJlabel.setText(result.getTitle());
textArea.setText(result.getText());
url = result.getUrl();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
/** */
private void initComponents() {
titleJlabel = new javax.swing.JLabel();
jumpJbutton = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
textArea = new javax.swing.JTextArea();
titleJlabel.setText("jLabel1");
jumpJbutton.setText("跳轉");
jumpJbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(Desktop.isDesktopSupported()){
try {
URI uri=URI.create(url);
Desktop dp=Desktop.getDesktop();
if(dp.isSupported(Desktop.Action.BROWSE)){
dp.browse(uri);
}
} catch (Exception o) {
o.printStackTrace();
}
}
}
});
textArea.setColumns(20);
textArea.setRows(5);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
jScrollPane1.setViewportView(textArea);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(36, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(titleJlabel, javax.swing.GroupLayout.PREFERRED_SIZE, 522, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(30, 30, 30)
.addComponent(jumpJbutton)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 633, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(31, 31, 31))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(titleJlabel, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jumpJbutton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
.addContainerGap())
);
}//
private String url;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton jumpJbutton;
private javax.swing.JTextArea textArea;
private javax.swing.JLabel titleJlabel;
// End of variables declaration
}
關鍵代碼
4.運行結果截圖
1.
2.
3.
4.
5.
5.遇到的問題
1)在SearchLook.java類中,用來放文本和標題的容器不知道用JLabel還是Jframe,最終經過百度查詢資料,選擇的JLabel,原因如下:JFrame是一個頂層的框架類,好比一個窗戶的框子。也是一個容器類,這個框子可以嵌入幾個玻璃窗,就是說Jframe可以將標簽文本和按鈕安放并處理,而且能實現最小化/最大化、改變大小、移動等功能特性。而JPanel是一個容器類,相當于一大玻璃窗,可以放置文本框按鈕等非容器組件。在結果展示中,我只需要一個JLabel和一個文本區域和一個按鈕,所以我選擇JPanel。
2)第一次從net beans轉到eclipse上編寫時發現錯誤,后來經過檢查發現是因為照片文件的問題,獲取照片文件的相對路徑為當前包,第一開始我單獨放在別的包里,移到gui包后就可以正常運行了。
6.git提交記錄
7.我的感想
我本身的代碼基礎不是很扎實,所以課設中很多部分都無法完成,只能實現Gui界面的設計與部分代碼的編寫。通過這次Java課設,從隊友的指導,百度的搜索還有書本上的知識等等地方學到了好多關于Gui的知識,從容器插件還有監聽器等等。雖然功能簡單,但是我也遇到了不少的問題,比如說第一次寫監聽器是無法運行,原因就是我調用的類不對。還有就是跳轉到網頁時遇到了很多困難,研究了很久才解決。我設計的Gui界面有一些不足,但是我還是從中學到了很多,這次課設我受益匪淺。
8.團隊博客鏈接
總結
以上是生活随笔為你收集整理的java的一些课程设计题目_Java课程设计的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [js] 微信的JSSDK都有哪些内容?
- 下一篇: java 课程设计题目_Java课程设计