开发者

how to display text from file within JPanel?

开发者 https://www.devze.com 2023-02-20 11:28 出处:网络
I have difficulties in displaying the text in JPanel. The question is do I have to use JLabel to display the text, or there is any other way to do so?It seems like that\'s what most of the tutorials a

I have difficulties in displaying the text in JPanel. The question is do I have to use JLabel to display the text, or there is any other way to do so? It seems like that's what most of the tutorials are telling me, I'm just not sure.

The code looks like this:

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("Dial开发者_运维知识库og", 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());
          }

    }

Thanks so much in advance.


IMO, While you don't have to use JLabel, this is the most fitting component to display text that no one should edit. Regarding your problem, it seems that you assign strLine only one line when you read it, I think it would be better if you add another variable to store that full 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);
}

And display it later (I assume in content panel):

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);
....


I think if you replace:

sb.append(strLine); 

with:

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

you'll create a new line for every inputted line of text.That should fix the single line issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消