如何使用JScrollPane为我的JTextArea创建滚动条?

时间:2016-05-09 12:37:05

标签: java jscrollpane scrollpane

程序正确编译,但未创建文本区域的滚动窗格。我真的不知道为什么会这样。我定义了JscrollPane,甚至用scrollPane = new JScrollPane

实现了它

这是我的代码:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
public class GuiProject5 extends JFrame
{
private static final int WIDTH = 400;
private static final int HEIGHT = 300;
private JPanel lowerPanel;
private JLabel widthL, areaL;
private JTextArea areaTA;
private JTextArea ta;
private JScrollPane scrollPane;
private JTextField lengthTF;
private JCheckBox gergsC;
private JButton exitB;

//Button handlers:
private ExitButtonHandler ebHandler;

public GuiProject5()
{

    areaL = new JLabel("Label: ", SwingConstants.RIGHT);

    lengthTF = new JTextField("TextField");

    lowerPanel = new JPanel();

    areaTA = new JTextArea("TextArea", 6, 8);

    ta = new JTextArea("stuff", 6, 8);

    scrollPane = new JScrollPane();

    gergsC = new JCheckBox("Checkbox");

    //Specify handlers for each button and add (register)     ActionListeners to each button.

    exitB = new JButton("Button");
    ebHandler = new ExitButtonHandler();
    exitB.addActionListener(ebHandler);

    setTitle("My Gui in java");
    Container pane = getContentPane();
    pane.setLayout(new GridLayout(4, 2));

    //Add things to the pane in the order you want them to appear (left to right, top to bottom)

    pane.add(exitB);
    pane.add(lengthTF);
    pane.add(lowerPanel);
    ta = new JTextArea("Stuff", 6, 8);
    ta.setLineWrap(true);
    ta.setWrapStyleWord(true);
    lowerPanel.add(new JScrollPane(scrollPane));
    pane.add(new JScrolscrollpane);
    pane.add(gergsC);
    pane.add(areaL);



    setSize(WIDTH, HEIGHT);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}


public class ExitButtonHandler implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        System.exit(0);
    }
}

public static void main(String[] args)
{
    GuiProject5 rectObj = new GuiProject5();
}

}

1 个答案:

答案 0 :(得分:0)

您应该初始化var isValidKey = /^(?:sample[1-5]|definition)$/; var count = Object.keys(obj).filter(k => isValidKey.test(k) && obj[k] != null).length ,如

JScrollPane

并将其添加到面板中。

jsp=new JScrollPane(textAreaObj);

是一个容器类。你应该在JScrollPane 中定义你想要滚动条的组件 并在布局中添加该jsp对象