JScrollPane没有出现在我的JTextArea中

时间:2011-09-30 08:05:27

标签: scroll textarea scrollbar jscrollpane jtextarea

这个类是我程序中第一个选项卡CreatePanel的布局和实现。我正在尝试在我的JTextArea中实现JScrollPane。但是,滚动条永远不会出现,尽管程序编译正确。我已经全神贯注地寻找解决方案,并尝试了几种变化,但似乎没有任何工作。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class CreatePanel extends JPanel
{
    private Vector flightList;
    private SelectPanel sPanel;

    private JButton createButton;
    private JLabel airName, flightNumber, departCity, departDay, departTime, arriveCity, arriveDay, arriveTime, price;
    private JLabel notify, blank;
    private JTextField l1, l2, l3, l4, l5, l6, l7, l8, l9;
    private JTextArea flightInfo;
    private JScrollPane scroll;
    String air, num, dC, dD, dT, aC, aD, aT, prc;

    //Constructor initializes components and organize them using certain layouts
    public CreatePanel(Vector flightList, SelectPanel sPanel)
    {
        this.flightList = flightList;
        this.sPanel = sPanel;

        // organize components here
        // creates all labels and textfields for Flight Creation tab
        notify = new JLabel("");
        blank = new JLabel("");
        airName = new JLabel("Enter a name of Airlines");
        flightNumber = new JLabel("Enter a flight number");
        departCity = new JLabel("Enter a departure city");
        departDay = new JLabel("Enter a departure day");
        departTime = new JLabel("Enter a departure time");
        arriveCity = new JLabel("Enter an arrival city");
        arriveDay = new JLabel("Enter an arrival day");
        arriveTime = new JLabel("Enter an arrival time");
        price = new JLabel("Price");
        l1 = new JTextField("");
        l2 = new JTextField("");
        l3 = new JTextField("");
        l4 = new JTextField("");
        l5 = new JTextField("");
        l6 = new JTextField("");
        l7 = new JTextField("");
        l8 = new JTextField("");
        l9 = new JTextField("");
        createButton = new JButton("Create a flight");
        createButton.addActionListener(new ButtonListener());
        flightInfo = new JTextArea("No flight");
        flightInfo.setEditable(false);


        // the user-entered information panel
        JPanel panelUser = new JPanel();
        panelUser.setLayout(new GridLayout(10, 2));
        panelUser.add(notify);
        panelUser.add(blank);
        panelUser.add(airName);
        panelUser.add(l1);
        panelUser.add(flightNumber);
        panelUser.add(l2);
        panelUser.add(departCity);
        panelUser.add(l3);
        panelUser.add(departDay);
        panelUser.add(l4);
        panelUser.add(departTime);
        panelUser.add(l5);
        panelUser.add(arriveCity);
        panelUser.add(l6);
        panelUser.add(arriveDay);
        panelUser.add(l7);
        panelUser.add(arriveTime);
        panelUser.add(l8);
        panelUser.add(price);
        panelUser.add(l9);

        // panel for the button
        JPanel panelButton = new JPanel();
        panelButton.add(createButton);

        // the leftside panel of CreatePanel
        JPanel panelLeft = new JPanel();
        panelLeft.setLayout(new BorderLayout());
        panelLeft.add(panelUser, BorderLayout.CENTER);
        panelLeft.add(panelButton, BorderLayout.SOUTH);

        // the rightside panel of CreatePanel
        JPanel panelInfo = new JPanel();
        scroll = new JScrollPane(flightInfo);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scroll.setVisible(true);
        panelInfo.setLayout(new GridLayout(1, 1));
        panelInfo.add(flightInfo);

        // WHY WONT YOU WORK?


        // layout for CreatePanel
        setLayout(new GridLayout(1, 2));
        add(panelLeft);
        add(panelInfo);
    }
}

添加JScrollPane时我做错了什么?

2 个答案:

答案 0 :(得分:0)

您可以根据需要使用滚动条,因此仅当达到textarea的限制时才会显示该栏(溢出,文字多于大小:D)

编辑:此外,您必须将滚动添加到面板而不是航班信息

答案 1 :(得分:0)

您一直在尝试添加错误的Panel。 将panelInfo.add(flightInfo);替换为panelInfo.add(scroll),您应该会看到您的JScrollPane出现(您可能需要将滚动条策略设置为“始终”以演示此内容)。

虽然我很喜欢,但是当我发布问题时,我是否可以建议你尝试做一个最小的失败示例?抱歉,如果我听起来很迂腐,但它确实让其他人更容易帮助你。