科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件JAVA:RSS与网格包布局

JAVA:RSS与网格包布局

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

在上一篇关于网格包布局的介绍文章中,我们留下了一个JAVA框架,它能够达到我们的目标但是rss功能没有实现,本文我们将完成该框架。

作者:开发者在线 来源:开发者在线 2007年8月2日

关键字:

  • 评论
  • 分享微博
  • 分享邮件

如果你想继续改进该接口,下面列出了一些快速清理接口的全部代码以帮助你快速起步。

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.table.*;

import javax.swing.event.*;

import java.util.Vector;

//Rome imports

import java.net.*;

import java.io.InputStreamReader;

import com.sun.syndication.feed.module.*;

import com.sun.syndication.feed.synd.*;

import com.sun.syndication.io.*;

import org.jdom.Document;

public class OldGridBagWindow extends JFrame {

 

private JButton searchBtn;

private JComboBox modeCombo;

private JLabel tagLbl;

private JLabel tagModeLbl;

private JLabel previewLbl;

private JTable resTable;

private JTextField tagTxt;

public OldGridBagWindow() {

Container contentPane = getContentPane();

GridBagLayout gridbag = new GridBagLayout();

contentPane.setLayout(gridbag);

GridBagConstraints c = new GridBagConstraints();

//setting a default constraint value

c.fill = GridBagConstraints.HORIZONTAL;

tagLbl = new JLabel("Tags");

tagLbl.setHorizontalAlignment(SwingConstants.RIGHT);

c.gridx = 0; //x grid position

c.gridy = 0; //y grid position

c.insets = new Insets(5,5,5,15);

gridbag.setConstraints(tagLbl, c); //associate the label with a constraint object

contentPane.add(tagLbl); //add it to content pane

tagModeLbl = new JLabel("Tag Mode");

tagModeLbl.setHorizontalAlignment(SwingConstants.RIGHT);

c.gridx = 0;

c.gridy = 1;

gridbag.setConstraints(tagModeLbl, c);

contentPane.add(tagModeLbl);

tagTxt = new JTextField("plinth");

c.gridx = 1;

c.gridy = 0;

c.gridwidth = 2;

c.insets = new Insets(0,0,0,0);

gridbag.setConstraints(tagTxt, c);

contentPane.add(tagTxt);

String[] options = {"all", "any"};

modeCombo = new JComboBox(options);

c.gridx = 1;

c.gridy = 1;

c.gridwidth = 1;

gridbag.setConstraints(modeCombo, c);

contentPane.add(modeCombo);

searchBtn = new JButton("Search");

searchBtn.addActionListener(new GridBagAL());

c.gridx = 1;

c.gridy = 2;

gridbag.setConstraints(searchBtn, c);

contentPane.add(searchBtn);

resTable = new JTable();

resTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

ListSelectionModel rowSM = resTable.getSelectionModel();

rowSM.addListSelectionListener(new ListSelectionListener() {

public void valueChanged(ListSelectionEvent e) {

//Ignore extra messages.

if (e.getValueIsAdjusting()) return;

 

ListSelectionModel lsm = (ListSelectionModel)e.getSource();

if (lsm.isSelectionEmpty()) {

//No rows are selected

} else {

int selectedRow = lsm.getMinSelectionIndex();

FlickrTableEntryfte=(FlickrTableEntry) resTable.getValueAt(selectedRow, 0);

previewLbl.setText(fte.getTitle());

try{

previewLbl.setIcon(newImageIcon(new URL(fte.getPicture())));

} catch (Exception ex){

ex.printStackTrace();

System.out.println("ERROR: "+ex.getMessage());

}

}

}

});

JScrollPane scrollPane = new JScrollPane(resTable);

scrollPane.setMinimumSize(new Dimension(450, 110));

c.gridx = 0;

c.gridy = 3;

c.gridwidth = 3;

c.fill = GridBagConstraints.BOTH;

gridbag.setConstraints(scrollPane, c);

contentPane.add(scrollPane);

previewLbl = new JLabel("Preview goes here");

previewLbl.setHorizontalAlignment(SwingConstants.CENTER);

previewLbl.setVerticalAlignment(SwingConstants.TOP);

previewLbl.setVerticalTextPosition(SwingConstants.TOP);

previewLbl.setHorizontalTextPosition(SwingConstants.CENTER);

previewLbl.setOpaque(true);

JScrollPane tmpPane = new JScrollPane(previewLbl);

tmpPane.setPreferredSize(new Dimension(450, 600));

c.gridx = 0;

c.gridy = 4;

c.weightx = 1;

c.weighty = 1;

c.fill = GridBagConstraints.BOTH;

gridbag.setConstraints(tmpPane, c);

contentPane.add(tmpPane);

 

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

}

 

public static void main(String args[]) {

OldGridBagWindow window = new OldGridBagWindow();

 

window.setTitle("GridBagWindow");

window.pack();

window.setVisible(true);

}

class GridBagAL implements ActionListener{

public void actionPerformed(ActionEvent e) {

try{

URLfeedUrl=new URL("http://api.flickr.com/services/feeds/photos_public.gne?tags="+URLEncoder.encode(tagTxt.getText(),"UTF-8") +"&tagmode="+modeCombo.getSelectedItem().toString()+"&format=rss_200_enc");

SyndFeedInput input = new SyndFeedInput();

SyndFeed feed = input.build(new XmlReader(feedUrl));

Vector tableData = new Vector();

for(Object ent: feed.getEntries()){

SyndEntryImpl sei = (SyndEntryImpl) ent;

for(Object enc_ent: sei.getEnclosures()){

SyndEnclosureImpl senci = (SyndEnclosureImpl) enc_ent;

Vector vectmp = new Vector();

vectmp.add(new FlickrTableEntry(sei.getTitle(), senci.getUrl()));

tableData.add(vectmp);

}

}

Vector colNames = new Vector();

colNames.add("Pictures");

resTable.setModel(new DefaultTableModel(tableData, colNames));

}

catch (Exception ex){

ex.printStackTrace();

System.out.println("ERROR: "+ex.getMessage());

}

}

}

class FlickrTableEntry{

private String title, picture;

public FlickrTableEntry(String title, String picture){

this.title = title;

this.picture = picture;

}

public String getTitle(){return title;}

public String getPicture(){return picture;}

public String toString(){return title;}

}

}

责任编辑:德东

查看本文国际来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章