繁体   English   中英

将JScrollPane添加到JPanel中的JTextArea

[英]Adding JScrollPane to a JTextArea in a JPanel

我试图将JScrollPane添加到我的大型JTextArea中,但是每当我包含JScrollPane代码时,我的整个JTextArea都会消失。

public myGUI() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 1174, 656);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    contentPane.setVisible(true);

    JTextArea textArea_1 = new JTextArea();
    textArea_1.setBounds(203, 5, 869, 440);
    textArea_1.setEditable(true);
    textArea_1.setVisible(true);
    contentPane.add(textArea_1);

    JScrollPane scroll = new JScrollPane (textArea_1);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    contentPane.add(scroll);

}

您的代码有几个问题:

  • 您正在尝试向多个容器(此处为contentPane JScrollPane)添加一个组件(此处为JTextArea_1,称为textArea_1)。 您不能在Swing中执行此操作,因为一个组件只能添加到一个组件中。 仅将其添加到JScrollPane而不是contentPane,然后将滚动窗格添加到GUI。
  • 您正在通过setBounds约束JTextArea的大小,这几乎可以保证JScrollPane无法正常工作,因为这样做会阻止JTextArea在容纳的文本多于显示的文本时扩展。 而是设置JScrollPane的rows和columns属性,而不是其边界。 这将是它应该在滚动窗格中显示的行数和列数
  • 您使用的是空布局,但未指定JScrollPane的大小,因此它默认为[0,0]的大小- 这就是JTextArea消失的原因 空布局要求完整说明所有组件的尺寸和位置。
  • 您正在使用空布局来设置GUI。 尽管null布局和setBounds()似乎是Swing新手喜欢创建复杂GUI的最简单和最佳方法,但您创建的Swing GUI越多,使用它们时将遇到的困难就越大。 当GUI调整大小时,它们不会重新调整组件的大小;它们是需要增强或维护的皇家女巫;放置在滚动窗格中时,它们会完全失败;在所有平台或与原始分辨率不同的屏幕分辨率下查看时,它们看起来都是令人毛骨悚然的。

例如:

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.*;

@SuppressWarnings("serial")
public class MyGuiPanel extends JPanel {
    // some row and column values for our JTextArea
    private static final int TXT_AREA_ROWS = 25;
    private static final int TXT_AREA_COLS = 80;

    // create the JTextArea, passing in the rows and columns values
    private JTextArea textArea = new JTextArea(TXT_AREA_ROWS, TXT_AREA_COLS);

    public MyGuiPanel() {
        // create the JScrollPane, adding our JTextArea
        JScrollPane scroll = new JScrollPane(textArea);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        // lets add some buttons to the bottom of the GUI just for fun
        JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 0));
        buttonPanel.add(new JButton("Save"));
        buttonPanel.add(new JButton("Open"));
        buttonPanel.add(new JButton("Delete"));
        buttonPanel.add(new JButton("Exit"));

        // let's add a title to the top:
        JLabel title = new JLabel("This is my Applications's Title", SwingConstants.CENTER);
        title.setFont(title.getFont().deriveFont(Font.BOLD, 24)); // and make
                                                                  // the text
                                                                  // *BIG*

        // use a BorderLayout for our GUI
        setLayout(new BorderLayout(5, 5));
        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        add(scroll, BorderLayout.CENTER); // add the scrollpane to the center
        add(buttonPanel, BorderLayout.PAGE_END); // the button panel to the
                                                 // bottom
        add(title, BorderLayout.PAGE_START); // and the title JLabel to the top
    }

    private static void createAndShowGui() {
        // create our GUI JPanel
        MyGuiPanel mainPanel = new MyGuiPanel();

        // create a JFrame to add it to
        JFrame frame = new JFrame("My GUI");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel); // add the GUI to the JFrame
        frame.pack(); // tell the layout managers to do their work
        frame.setLocationByPlatform(true);
        frame.setVisible(true); // display the GUI
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

另外,如果您的程序扩展了JFrame,请理解您这样做是在角落里画画,迫使您创建和显示JFrame,而通常需要更大的灵活性。 实际上,我会冒险地说,我创建的大多数Swing GUI代码都不会扩展JFrame,实际上,很少有人愿意这样做。 更常见的是,您的GUI类将面向创建JPanels,然后将其放置到JFrames或JDialogs或JTabbedPanes中,或者在需要时通过CardLayouts进行交换。 这将大大增加GUI编码的灵活性。

您的JTextArea不需要setBounds。 因为您使用的是null布局,并且JScrollPane没有边界,所以不会显示任何内容。 您的JTextArea也被添加到两个地方,这会引起一些问题。 我会推荐任何秋千布局经理 作为使用BorderLayout的示例,它是最简单的管理器之一:

public mygui() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    setSize(700, 500);
    setLayout(new BorderLayout());

    JTextArea textArea_1 = new JTextArea();
    textArea_1.setEditable(true);

    JScrollPane scroll = new JScrollPane(textArea_1);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    add(scroll, BorderLayout.CENTER);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM