如何在JPanel中显示文件中的文本?

时间:2011-03-26 09:38:50

标签: java jpanel

我在JPanel中显示文本时遇到了困难。问题是我是否必须使用JLabel来显示文本,还是有其他方法可以这样做?这似乎是大多数教程告诉我的,我只是不确定。

代码如下所示:

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());
          }

    }

非常感谢。

2 个答案:

答案 0 :(得分:1)

IMO,虽然你没有 来使用JLabel,但这是显示没有人应该编辑的文本的最合适的组件。关于你的问题,似乎你在阅读时只给strLine分配了一行,我认为如果你添加另一个变量来存储那个完整的String会更好:

String strLine;
StringBuilder sb = new StringBuilder();
//Read File Line By Line
while ((strLine = br.readLine()) != null)   {
      // Print the content on the console
      System.out.println (strLine);
      sb.append(strLine);
}

稍后再显示(我在内容面板中假设):

JPanel content = new JPanel(new FlowLayout());
JLabel info_from_file = new JLabel(sb.toString());
content.add(info_from_file);
content.setToolTipText(strLine);
content.setForeground(Color.red);
....

答案 1 :(得分:1)

我想如果你替换:

sb.append(strLine); 

使用:

sb.append(strLine+"/n"); 

你将为每个输入的文本行创建一个新行。这应该解决单行问题。