1

я написал код который берёт текст с пастебина raw в консоль он выводит его столбиком, но в JLabel нет( как сделать, чтобы в JLabel был столбиком? Сам код:

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JLabel;
 
public class Main
{
    public static void main(String[] args) throws MalformedURLException, IOException
    {
        JFrame f = new JFrame();
        f.setSize(200,200);
        f.setVisible(true);
        f.setLayout(null);
        JLabel l = new JLabel();
    
        l.setBounds(0,0,200, 200);
        
        f.add(l);
         InputStream in = null;
       in = (new URL("https://pastebin.com/raw/QPjyzJ5w").openStream());
 
        try (BufferedReader br = new BufferedReader(new InputStreamReader(in)))
        {
            String line;
            while ((line = br.readLine()) != null) {
            
               l.setText(line);
               System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

1 ответ 1

0

Вы можете использовать JTextArea вместо JLabel

Код:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class Main {

    public static void main(String[] args) throws MalformedURLException, IOException {
        JFrame f = new JFrame();
        f.setSize(200,200);
        f.setVisible(true);
        f.setLayout(null);
        
        JTextArea l = new JTextArea();
        l.setEditable(false);

        l.setBounds(0,0,200, 200);

        f.add(l);
        InputStream in = null;
        in = (new URL("https://pastebin.com/raw/QPjyzJ5w").openStream());

        try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
            String line;
            while ((line = br.readLine()) != null) {
                l.append(line + "\n");
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Результат:

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.