我想使用Eclipse在JLabel上使用hmtl标签显示带有图像的文本。图像没有显示。有人会帮我解决问题吗?预先感谢。
图像文件路径:C:\ Users \ useName \ Documents \ Java Project \ Samples \ Reader \ src \ Reader \ QuestionMark.gif 有代码来设置JLabel的文本:
lblDate.setText("<html><table><tr><td style='width:10pt;'></td><td>Page 2 02/27/2015 10:40:13 AM</td></tr><tr><td style='width:10pt;'><img src='/src/Reader/QuestionMark.gif'></img></td><td>*Completed</td></tr></table></html>");
我的项目代码:
package pdfReader;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class TestHTMLLabel extends JFrame implements ActionListener {
private GridBagConstraints c;
private JLabel lblDate=null;
public TestHTMLLabel() {
// TODO Auto-generated constructor stub
setTitle("Test HTMLLabel example");
setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set flow layout for the frame
this.getContentPane().setLayout(new FlowLayout());
JPanel pl=new JPanel( new GridBagLayout());
c = new GridBagConstraints();
c.gridx=0;
c.gridy=2;
c.gridwidth=4; //left hand size
c.ipady=140; //make this component tall
c.anchor=GridBagConstraints.WEST;
pl.add(setPageDateLabel(), c);
c.ipady=0;
c.gridx=0;
c.gridy=3;
c.anchor=GridBagConstraints.CENTER;
pl.add(setContent(), c);
this.add(pl);
this.setVisible(true);
}
private JScrollPane setContent(){
JScrollPane scroll=new JScrollPane();
JTextArea t=new JTextArea(6,20);
t.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus quis scelerisque arcu. Aliquam eleifend, ex vel ullamcorper dignissim, leo nulla aliquet turpis, vel accumsan ex risus sed neque. Aliquam ornare ac nunc id posuere. Vivamus at tortor risus. Praesent quam arcu, varius sed porttitor sodales, sagittis id justo. Cras dictum sollicitudin sem, vitae feugiat eros. Aenean dictum lorem eros, vitae suscipit purus eleifend sit amet. Phasellus sodales metus quam, quis semper nunc vehicula et. Donec sollicitudin lacinia ipsum, ut hendrerit mauris tristique a. In fringilla, tellus elementum lacinia feugiat, lectus dolor tincidunt libero, sit amet blandit tellus sem in quam. Suspendisse sagittis ac enim in volutpat. Integer vel lectus quis sapien sollicitudin vehicula. Quisque id ligula tempus, molestie leo eget, sagittis dolor. Pellentesque tempor enim non elit condimentum commodo. In facilisis felis sed dapibus malesuada. Sed varius volutpat mi, id cursus neque consequat sed. ");
t.setLineWrap(true);
t.setWrapStyleWord(true);
t.setEditable(false);
scroll.getViewport().add(t);
return scroll;
}
private JLabel setPageDateLabel(){
JLabel lblDate=new JLabel( );
lblDate.setText("<html><table><tr><td style='width:10pt;'></td><td>Page 2 02/27/2015 10:40:13 AM</td></tr><tr><td style='width:10pt;'><img src='/src/Reader/QuestionMark.gif'></img></td><td>*Completed</td></tr></table></html>");
//lblDate.setBorder(BorderFactory.createLineBorder(Color.red));
return lblDate;
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
// createAndShowGUI();
new TestHTMLLabel();
}
});
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)