/*
 * News.java
 *
 * Created on __DATE__, __TIME__
 */package test;import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;import javax.swing.JFrame;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;import com.sun.syndication.io.FeedException;import bean.Center;
import bean.Close;
import bean.Linknet;
import bean.MyJButton;
import bean.MyJPanel;import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;/**
 * 
 * @author __USER__
 */
public class News extends javax.swing.JFrame implements HyperlinkListener { /**
 * 
 */
private static final long serialVersionUID = 1L; /** Creates new form News */
public News() {
initComponents();
} /**
 * 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.
 */
// GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() { this.setUndecorated(true);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.setResizable(false);
// this.setUndecorated(true);
// this.getRootPane().setWindowDecorationStyle(
// JRootPane.INFORMATION_DIALOG);
Image image = this.getToolkit().createImage(
this.getClass().getResource("image/liaotian.jpg"));
this.setIconImage(image);
jPanel1 = new MyJPanel();
((MyJPanel) jPanel1).setImageFilename("image/loginingback.jpg"); close = new MyJButton();
((MyJButton) close).setImageFilename("image/close3.jpg");
Minimize = new MyJButton();
((MyJButton) Minimize).setImageFilename("image/minimize1.jpg");
jTabbedPane1 = new javax.swing.JTabbedPane();
jScrollPane1 = new javax.swing.JScrollPane();
news = new javax.swing.JEditorPane("text/html","");
jScrollPane2 = new javax.swing.JScrollPane();
Military = new javax.swing.JEditorPane("text/html","");
jScrollPane3 = new javax.swing.JScrollPane();
fun = new javax.swing.JEditorPane("text/html","");
jScrollPane4 = new javax.swing.JScrollPane();
car = new javax.swing.JEditorPane("text/html","");
jScrollPane5 = new javax.swing.JScrollPane();
games = new javax.swing.JEditorPane("text/html","");
this.news.addHyperlinkListener(this);
this.Military.addHyperlinkListener(this);
this.fun.addHyperlinkListener(this);
this.car.addHyperlinkListener(this);
this.games.addHyperlinkListener(this); setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE); jScrollPane1.setViewportView(news); jTabbedPane1.addTab("国际", jScrollPane1); jScrollPane2.setViewportView(Military); jTabbedPane1.addTab("体育", jScrollPane2); jScrollPane3.setViewportView(fun); jTabbedPane1.addTab("科技", jScrollPane3); jScrollPane4.setViewportView(car); jTabbedPane1.addTab("社会", jScrollPane4); jScrollPane5.setViewportView(games); jTabbedPane1.addTab("播客", jScrollPane5); close.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
closeActionPerformed(evt);
}
}); Minimize.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MinimizeActionPerformed(evt);
}
}); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout
.setHorizontalGroup(jPanel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
jPanel1Layout
.createSequentialGroup()
.addContainerGap()
.addGroup(
jPanel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
jPanel1Layout
.createSequentialGroup()
.addComponent(
jTabbedPane1,
javax.swing.GroupLayout.DEFAULT_SIZE,
506,
Short.MAX_VALUE)
.addContainerGap())
.addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout
.createSequentialGroup()
.addComponent(
Minimize)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(
close)))));
jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
jPanel1Layout.createSequentialGroup().addGroup(
jPanel1Layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING,
false).addComponent(close,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE).addComponent(Minimize,
javax.swing.GroupLayout.DEFAULT_SIZE, 17,
Short.MAX_VALUE)).addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTabbedPane1,
javax.swing.GroupLayout.DEFAULT_SIZE, 308,
Short.MAX_VALUE).addContainerGap())); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addComponent(
jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
layout.setVerticalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addComponent(
jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
//this.setLocation(Center.createRight(250)[0],Center.createRight(250)[1]
// );
this.setLocation(Center.createCenter(this.getSize().width,
this.getSize().height).getX(), Center.createCenter(
this.getSize().width, this.getSize().height).getY());
pack();
this.setVisible(true);
try {
this.write1();
} catch (Exception ee) { }
}// </editor-fold> // GEN-END:initComponents private void MinimizeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.setExtendedState(MainFrame.ICONIFIED);
} private void closeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.dispose();
Close.close();
}

解决方案 »

  1.   


    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new News();
    }
    }); } public void write1() throws IllegalArgumentException, FeedException {
    java.lang.StringBuffer news = new java.lang.StringBuffer(),news1 = new java.lang.StringBuffer(),
    news2 = new java.lang.StringBuffer(),
    news3 = new java.lang.StringBuffer(),
    news4 = new java.lang.StringBuffer();
    java.util.Properties systemSettings = System.getProperties();
    systemSettings.put("http.proxyHost", "mywebcache.com");
    systemSettings.put("http.proxyPort", "8080");
    System.setProperties(systemSettings);
    // String urlStr = "http://172.16.158.16/news.xml";
    String urlStr = "http://rss.sina.com.cn/news/marquee/ddt.xml";
    // String urlStr = "http://www.hnyyzx.com/rssFeed.aspx";
    java.net.URLConnection feedUrl;
    try {
    feedUrl = new java.net.URL(urlStr).openConnection();
    feedUrl.setRequestProperty("User-Agent",
    "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
    com.sun.syndication.io.SyndFeedInput input = new com.sun.syndication.io.SyndFeedInput();
    com.sun.syndication.feed.synd.SyndFeed feed = input
    .build(new com.sun.syndication.io.XmlReader(feedUrl));
    List<?> list = feed.getEntries();
    for (int i = 0; i < list.size(); i++) {
    com.sun.syndication.feed.synd.SyndEntry entry = (com.sun.syndication.feed.synd.SyndEntry) list
    .get(i);
    if(entry.getTitle().trim().startsWith("[国际]"))
    news.append(" "+"<a href=\""+entry.getLink().trim()+"\">"+entry.getTitle().trim() +"</a> <br/>");
    else if(entry.getTitle().trim().startsWith("[体育]"))
    news1.append(" "+"<a href=\""+entry.getLink().trim()+"\">"+entry.getTitle().trim() +"</a> <br/>");
    else if(entry.getTitle().trim().startsWith("[科技]"))
    news2.append(" "+"<a href=\""+entry.getLink().trim()+"\">"+entry.getTitle().trim() +"</a> <br/>");
    else if(entry.getTitle().trim().startsWith("[社会]"))
    news3.append(" "+"<a href=\""+entry.getLink().trim()+"\">"+entry.getTitle().trim() +"</a> <br/>");
    else //if(entry.getTitle().startsWith("[播客]"))
    news4.append(" "+"<a href=\""+entry.getLink().trim()+"\">"+entry.getTitle().trim() +"</a> <br/>");
    }
    this.news.setText(news.toString());
    this.news.addMouseListener(new MouseAdapter(){
    public void mouseClicked(MouseEvent e){

    }

    });
    this.Military.setText(news1.toString());
    this.fun.setText(news2.toString());
    this.car.setText(news3.toString());
    this.games.setText(news4.toString());


    } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } }
     public void hyperlinkUpdate(HyperlinkEvent hyperlinkEvent) {
     HyperlinkEvent.EventType type = hyperlinkEvent.getEventType();
      URL url = hyperlinkEvent.getURL();
      System.out.println(url.toString());
             if (type == HyperlinkEvent.EventType.ACTIVATED) {
                   Linknet.open(url.toString().trim());
             }  }
      // GEN-BEGIN:variables
    // Variables declaration - do not modify
    private javax.swing.JEditorPane Military;
    private javax.swing.JButton Minimize;
    private javax.swing.JEditorPane car;
    private javax.swing.JButton close;
    private javax.swing.JEditorPane fun;
    private javax.swing.JEditorPane games;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JEditorPane news;
    // End of variables declaration//GEN-END:variables}
      

  2.   

    为什么我点超链接
    而不触发HyperlinkEvent事件
      

  3.   

    呵呵 自己解决了
    问题是没加setEditable(false);
      

  4.   

    LZ 没加setEditable(false); 
      

  5.   

    LZ 没加setEditable(false);