我在JPanel中顯示文本時遇到困難。問題是我必須使用JLabel來顯示文本,還是有其他方法可以這樣做?這似乎是大多數教程告訴我的,我只是不確定。如何在JPanel中顯示文件中的文本?
的代碼看起來是這樣的:
public class WhatToDo extends JFrame{
public void aboutus(){
try{
//open the file
FileInputStream inMessage = new FileInputStream("aboutus.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(inMessage);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
Container con = getContentPane();
con.setLayout(null);
ImageIcon imh1 = new ImageIcon("img/aboutus.png");
setSize(imh1.getIconWidth(), imh1.getIconHeight());
JPanel panelBgImg = new JPanel()
{
public void paintComponent(Graphics g)
{
Image img = new ImageIcon("img/aboutus.png").getImage();
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
g.drawImage(img, 0, 0, null);
}
};
Container pane = getContentPane();
JPanel back = new JPanel(new FlowLayout());
back.add(new AboutUsBack(null));
back.setBounds(1170, 665, 83, 85);
back.setBackground(new Color(118, 122, 117, 0));
pane.add(back);
JPanel content = new JPanel(new FlowLayout());
content.setToolTipText(strLine);
content.setForeground(Color.red);
content.setBounds(570, 165, 583, 85);
content.setFont(new Font("Dialog", 1, 14));
pane.add(content);
con.add(panelBgImg);
panelBgImg.setBounds(0, 0, imh1.getIconWidth(), imh1.getIconHeight());
setUndecorated(true);
setResizable(true);
setVisible(true);
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
非常感謝提前。
我會在只讀模式下使用JTextPane。這非常簡單,關心流程和文字換行,如果需要,可以包含HTML或RTF格式和/或圖形。 – 9000 2011-03-26 10:15:07