簡體   English   中英

JPanel上的JScrollpane不起作用

[英]JScrollpane on JPanel doesn't work

我試圖通過jpanel在我的jframe上添加jscrollpane,但是它不起作用。 問題是如果我不添加滾動條並且要在框架上顯示的字符串太長,它將不會顯示並且將被切斷。但是,當我添加滾動窗格時,jframe不會顯示要在窗口上“繪制”字符串,我正在使用setText()。 這是我的代碼:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.net.*;
import javax.swing.border.LineBorder;

public class LabelFrame extends JFrame {
    private final JTextField urlString; 
    private final JButton loadButton;
    String content;
    JTextArea textArea = new JTextArea();

    public LabelFrame() { 
        super("WebStalker"); 
        setSize(600, 600);

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
         });
        setLayout(new FlowLayout());

        urlString = new JTextField("https:Search",30);
        loadButton = new JButton("Load");


        JPanel panel = new JPanel();
        JLabel label = new JLabel("URL");
        panel.add(label);
        panel.add(urlString);
        panel.add(loadButton);

        JScrollPane jp = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        panel.add(jp);
        this.add(panel);

        pack(); // JFrame στο ελάχιστο μέγεθος με όλα τα περιεχόμενα
        setLocationRelativeTo(null); //τοποθετεί στο κέντρο το παράθυρο

        TextFieldHandler tHandler = new TextFieldHandler();
        ButtonHandler bHandler = new ButtonHandler(); 

        urlString.addActionListener(tHandler);
        loadButton.addActionListener(bHandler);

        urlString.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent e){
                urlString.setText("");
            }
        });

    }

    private class TextFieldHandler implements ActionListener {
        @Override
         public void actionPerformed(ActionEvent event){ 
             try {
                 content = URLReaderFinal.Reading(event.getActionCommand());

                 textArea.setText(content);
                 textArea.setWrapStyleWord(true);
                 textArea.setLineWrap(true);
                 textArea.setOpaque(false);
                 textArea.setEditable(false);
                 textArea.setFocusable(false);
                 textArea.setBackgroung(UIManager.getColor("Label.background");                             
                 textArea.setFont(UIManager.getFont("Label.font"));
                 textArea.setBorder(UIManager.getBorder("Label.border"));
                 getContentPane().add(textArea, BorderLayout.CENTER);
          }
          catch(Exception e) {
              JOptionPane.showMessageDialog(null, "This url doesnt exist","Error", JOptionPane.ERROR_MESSAGE);
          }


         }
    }

     private class ButtonHandler implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent event) {
             try {
                 content = URLReaderFinal.Reading(urlString.getText());
                 textArea.setText(content);
                 textArea.setWrapStyleWord(true);
                 textArea.setLineWrap(true);
                 textArea.setOpaque(false);
                 textArea.setEditable(false);
                 textArea.setFocusable(false);
                 textArea.setBackground(UIManager.getColor("Label.background"));
                 textArea.setFont(UIManager.getFont("Label.font"));
                 textArea.setBorder(UIManager.getBorder("Label.border"));
                 getContentPane().add(textArea, BorderLayout.CENTER);
             } catch (Exception e) {
                 System.out.println("Unable to load page");
                 JOptionPane.showMessageDialog(null, "Unable to load page","Error", JOptionPane.ERROR_MESSAGE);
             }
     }
 }

}

在按鈕處理程序中,您正在將JTextArea重新添加到沒有JScrollPane的GUI中:

getContentPane().add(textArea, BorderLayout.CENTER);

這實際上是在需要時將其從JScrollPane中刪除的原因-請勿這樣做,因為這會使您完全混亂,並將JScrollPane從視圖中刪除。 而是將JTextArea保留在原處,不要嘗試將組件重新添加到JFrame的contentPane中,而只需根據需要向JTextArea添加文本。

另外,不要忘記給您的JTextArea列和行屬性,這可以通過使用帶有兩個int的構造函數輕松完成。 並將contentPane的布局保留為BorderLayout:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.net.*;
import javax.swing.border.LineBorder;

@SuppressWarnings("serial")
public class LabelFrame extends JFrame {
    private static final int ROWS = 30;
    private static final int COLS = 80;
    private final JTextField urlString;
    private final JButton loadButton;
    String content;
    JTextArea textArea = new JTextArea(ROWS, COLS);

    public LabelFrame() {
        super("WebStalker");
        // setSize(600, 600);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // setLayout(new FlowLayout()); // !! no
        textArea.setWrapStyleWord(true);
        textArea.setLineWrap(true);
        textArea.setOpaque(false);
        textArea.setEditable(false);
        textArea.setFocusable(false);
        textArea.setBackground(UIManager.getColor("Label.background"));
        textArea.setFont(UIManager.getFont("Label.font"));
        textArea.setBorder(UIManager.getBorder("Label.border"));

        urlString = new JTextField("https:Search", 30);
        loadButton = new JButton("Load");

        JPanel panel = new JPanel();
        JLabel label = new JLabel("URL");
        panel.add(label);
        panel.add(urlString);
        panel.add(loadButton);

        JScrollPane jp = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        // panel.add(jp);
        this.add(panel, BorderLayout.PAGE_START);
        add(jp);

        pack(); // JFrame στο ελάχιστο μέγεθος με όλα τα περιεχόμενα
        setLocationRelativeTo(null); // τοποθετεί στο κέντρο το παράθυρο

        TextFieldHandler tHandler = new TextFieldHandler();
        ButtonHandler bHandler = new ButtonHandler();

        urlString.addActionListener(tHandler);
        loadButton.addActionListener(bHandler);

        urlString.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                urlString.setText("");
            }
        });

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new LabelFrame().setVisible(true);
        });
    }

    private class TextFieldHandler implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent event) {
            try {
                content = URLReaderFinal.Reading(event.getActionCommand());

                textArea.setText(content);

                // !! getContentPane().add(textArea, BorderLayout.CENTER);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "This url doesnt exist", "Error", JOptionPane.ERROR_MESSAGE);
            }

        }
    }

    private class ButtonHandler implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent event) {
            try {
                content = URLReaderFinal.Reading(urlString.getText());
                textArea.setText(content);

                // getContentPane().add(textArea, BorderLayout.CENTER);
            } catch (Exception e) {
                System.out.println("Unable to load page");
                JOptionPane.showMessageDialog(null, "Unable to load page", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}

編輯:不要在JTextField上使用MouseListener。 也許您想改用FocusListener。

而是執行以下操作:

public class LabelFrame extends JFrame {
    private static final int ROWS = 30;
    private static final int COLS = 80;
    private static final String HTTPS_SEARCH = "https:Search";

    // .....

    public LabelFrame() {
        // ....

        urlString = new JTextField(HTTPS_SEARCH, 30);

        //.....

        urlString.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                JTextField textField = (JTextField) e.getComponent();
                String text = textField.getText();
                if (text.equals(HTTPS_SEARCH)) {
                    textField.setText("");
                }
            }
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM