java的一些课程设计题目_Java课程设计
Java課程設(shè)計(jì)
1. 題目及要求
基于學(xué)校的搜索引擎
負(fù)責(zé)部分:Java GUI設(shè)計(jì)
2.界面調(diào)查
1)調(diào)查界面:百度
2)思考:
根據(jù)我的調(diào)查,我認(rèn)為我需要完成三個(gè)界面的設(shè)計(jì):
第一個(gè)是調(diào)查主界面,里面有一個(gè)集美大學(xué)的logo,一個(gè)搜索框用文本字段,因?yàn)樾枰诶锩鎸懭胨阉鲀?nèi)容,一個(gè)搜索按鈕用button,這個(gè)按鈕完成的功能就是輸入搜索內(nèi)容后,點(diǎn)擊搜索按鈕,可以跳轉(zhuǎn)到下一個(gè)界面,并且返回結(jié)果。
第二個(gè)界面是搜索結(jié)果界面:需要的是一個(gè)再次搜索框,用文本字段;一個(gè)再次搜索按鈕,用button;一個(gè)面板JPanel,用來盛放我搜索到的結(jié)果;在界面的最底下還有三個(gè)按鈕,一個(gè)文本框,分別是:上一頁,下一頁,跳轉(zhuǎn),和相應(yīng)頁面表示,當(dāng)數(shù)據(jù)量過大需要分頁時(shí),就是用來實(shí)現(xiàn)頁面的跳轉(zhuǎn)的。
第三個(gè)就是結(jié)果展示界面:一個(gè)jLabel,展示標(biāo)題;一個(gè)文本區(qū)域展示內(nèi)容;一個(gè)按鈕,點(diǎn)擊能夠跳轉(zhuǎn)到原網(wǎng)頁瀏覽。
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 檢索結(jié)果
*/
public List fullTextSerch(String queryString) {
// 聲明一個(gè)搜索請(qǐng)求體
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.queryStringQuery(queryString));
searchSourceBuilder.query(boolQueryBuilder);
// 設(shè)置分頁
searchSourceBuilder.from(0);
searchSourceBuilder.size(800);
// 構(gòu)建Search對(duì)象
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, "未搜索到相關(guān)內(nèi)容!");
}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("頁數(shù)跳轉(zhuǎn)");
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, "未搜索到相關(guān)內(nèi)容!");
}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, "當(dāng)前已為第一頁,無法進(jìn)入上一頁!");
}else{
currentPage--;
displayResult();
}
}
private void jumpNextPageActionPerformed(java.awt.event.ActionEvent evt) {
if(currentPage == pageNum){
JOptionPane.showMessageDialog(null, "當(dāng)前已為最后一頁,無法進(jìn)入下一頁!");
}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, "輸入頁數(shù)不合法,請(qǐng)輸入1-"+pageNum+"中的數(shù)字");
}
}
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("跳轉(zhuǎn)");
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
}
關(guān)鍵代碼
4.運(yùn)行結(jié)果截圖
1.
2.
3.
4.
5.
5.遇到的問題
1)在SearchLook.java類中,用來放文本和標(biāo)題的容器不知道用JLabel還是Jframe,最終經(jīng)過百度查詢資料,選擇的JLabel,原因如下:JFrame是一個(gè)頂層的框架類,好比一個(gè)窗戶的框子。也是一個(gè)容器類,這個(gè)框子可以嵌入幾個(gè)玻璃窗,就是說Jframe可以將標(biāo)簽文本和按鈕安放并處理,而且能實(shí)現(xiàn)最小化/最大化、改變大小、移動(dòng)等功能特性。而JPanel是一個(gè)容器類,相當(dāng)于一大玻璃窗,可以放置文本框按鈕等非容器組件。在結(jié)果展示中,我只需要一個(gè)JLabel和一個(gè)文本區(qū)域和一個(gè)按鈕,所以我選擇JPanel。
2)第一次從net beans轉(zhuǎn)到eclipse上編寫時(shí)發(fā)現(xiàn)錯(cuò)誤,后來經(jīng)過檢查發(fā)現(xiàn)是因?yàn)檎掌募膯栴},獲取照片文件的相對(duì)路徑為當(dāng)前包,第一開始我單獨(dú)放在別的包里,移到gui包后就可以正常運(yùn)行了。
6.git提交記錄
7.我的感想
我本身的代碼基礎(chǔ)不是很扎實(shí),所以課設(shè)中很多部分都無法完成,只能實(shí)現(xiàn)Gui界面的設(shè)計(jì)與部分代碼的編寫。通過這次Java課設(shè),從隊(duì)友的指導(dǎo),百度的搜索還有書本上的知識(shí)等等地方學(xué)到了好多關(guān)于Gui的知識(shí),從容器插件還有監(jiān)聽器等等。雖然功能簡單,但是我也遇到了不少的問題,比如說第一次寫監(jiān)聽器是無法運(yùn)行,原因就是我調(diào)用的類不對(duì)。還有就是跳轉(zhuǎn)到網(wǎng)頁時(shí)遇到了很多困難,研究了很久才解決。我設(shè)計(jì)的Gui界面有一些不足,但是我還是從中學(xué)到了很多,這次課設(shè)我受益匪淺。
8.團(tuán)隊(duì)博客鏈接
總結(jié)
以上是生活随笔為你收集整理的java的一些课程设计题目_Java课程设计的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [js] 微信的JSSDK都有哪些内容?
- 下一篇: [css] 你们团队中css的clas