⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 searchscreen.java

📁 We intend to develop a wifi enabled p2p file sharing system on a linux platform using jxta and java.
💻 JAVA
字号:
package edu.uiuc.cs.cs327.linuxwifi.gui;

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.Vector;

import edu.uiuc.cs.cs327.linuxwifi.services.*;
import edu.uiuc.cs.cs327.linuxwifi.util.*;

    /**
     * Class that contains all information pertaining to the Search Screen
     */    

class SearchScreen extends JPanel implements ActionListener, FileInfoListener
{
	private static final boolean DEBUG = false;
	private ServicesApi _servicesApi;
	
	private JTextField _artist = new JTextField(30);
	private JTextField _song = new JTextField(30);
	private JTextField _genre = new JTextField(30);
	private JTextField _fileFormat = new JTextField(30);
	private JTextField _maxFileSize = new JTextField(30);
	
	private JList _responses = new JList();

    SearchScreen(ServicesApi servicesAPI ) {
        _servicesApi = servicesAPI;
        _servicesApi.addFileInfoListener(this);
        drawSearchScreen();
    }
    
	private void drawSearchScreen()
	{
		JPanel mainPanel = this;
		mainPanel.setLayout(new GridLayout(2,1));
		mainPanel.setSize(new Dimension(800,600));

		mainPanel.add(createSearchPanel());
		mainPanel.add(createResultsPanel());
	}
	
	private JPanel createSearchPanel()
	{
		JPanel searchPanel = new JPanel();
		searchPanel.setLayout(new BorderLayout());
		
		searchPanel.add(createSearchLabel(), BorderLayout.NORTH);
		searchPanel.add(createInputPanel(), BorderLayout.WEST);
		searchPanel.add(createSearchButton(), BorderLayout.SOUTH);
		
		return searchPanel;
	}
	
	private JLabel createSearchLabel()
	{
		JLabel searchLabel = new JLabel("SEARCH SCREEN", JLabel.CENTER);
		searchLabel.setFont(new Font(searchLabel.getFont().getFontName(), searchLabel.getFont().getStyle(), 20));
		
		return searchLabel;
	}
	
	private JPanel createInputPanel()
	{
		JPanel inputPanel = new JPanel();
		inputPanel.setLayout(new GridLayout(5, 2));
		
		addInputField(inputPanel, "Artist:", _artist);
		addInputField(inputPanel, "Song:", _song);
		addInputField(inputPanel, "Genre:", _genre);
		addInputField(inputPanel, "File Format:", _fileFormat);
		addInputField(inputPanel, "Max File Size:", _maxFileSize);
		
		return inputPanel;
	}
	
	private void addInputField(JPanel hostPanel, String labelText, JTextField textField)
	{
		JLabel label = new JLabel(labelText, JLabel.RIGHT);
		hostPanel.add(label);
		hostPanel.add(textField);
	}
	
	private JPanel createSearchButton()
	{
		JPanel buttonPanel = new JPanel();
		JButton submit = new JButton("Search");
                submit.setActionCommand("Search");
		//KABE Above
                submit.addActionListener(this);
		buttonPanel.add(submit);

                JButton downloadbutton = new JButton("Download");
                downloadbutton.setActionCommand("Download");
                downloadbutton.addActionListener(this);
                buttonPanel.add(downloadbutton);

		return buttonPanel;
	}
	
	private JPanel createResultsPanel()
	{
		JPanel resultsPanel = new JPanel();
		
		JScrollPane scrollPane = new JScrollPane(_responses);
		resultsPanel.add(scrollPane);
		
		attachResponseList();
		
		return resultsPanel;
	}

	public void actionPerformed(ActionEvent e)
	{
        if (e.getActionCommand().equals("Search")) {
        System.out.println("Search Caught");
            MusicProfile profile = new MusicProfile();
            profile.setArtist(_artist.getText());
            profile.setSong(_song.getText());
            profile.setGenre(_genre.getText());
            profile.setFileFormat(_fileFormat.getText());
            profile.setMaxFileSize(parseMaxFileSize());

            _servicesApi.searchPeers(profile);
        }
        else if(e.getActionCommand().equals("Download")){
        System.out.println("Download Caught");
        //Get Selected Item
        int sel = _responses.getSelectedIndex();
        System.out.println("Responses Selected: "+sel);
//        Downloader d = new Downloader();
        _servicesApi.startDownload( (FileInfo) _responses.getSelectedValue());
        

        //Add to Download Vector

        }
    }
	
	public void fileInfoReceived(FileInfoEvent e)
    {
		attachResponseList();
    }
    
    private void attachResponseList()
    {
		_responses.setListData(_servicesApi.getSearchResponses());
    }
	
	private int parseMaxFileSize()
	{
		int maxSize = 0;
		try
		{
			Integer.parseInt(_maxFileSize.getText());
		}
		catch (NumberFormatException nfe)
		{
		}
		return maxSize;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -