简体   繁体   English

如何将JScrollPane添加到此JTextArea?

[英]how do i add a JScrollPane to this JTextArea?

(I'm new to java btw) i have this unfinished program that and I would like to add a jscrollpane to the jtextarea . (我是新来的Java BTW)我有这个未完成的计划,我想一个添加jscrollpanejtextarea However I can't get it to work. 但是我无法正常工作。 I've tried doing 我试着做

add(new JScollPane(area));

which didn't work. 这没有用。 I tried: 我试过了:

panel.add(new JScrollPane(area));

and that didnt't work either. 而且那也不起作用。 I also tried: 我也尝试过:

area.add(pane);

and: 和:

area.add(new JScrollPane());

but nothing works. 但没有任何效果。 HELP!!! 救命!!! take a look 看一看

public class Constructor extends JFrame {
private static final long serialVersionUID = 123456789L;

    JScrollPane scroll;
    public JPanel panel;
    static JTextField field;
    static JTextArea area;
    String displayCommand, commandIs;

public Constructor() {

    panel = new JPanel();
    panel.setLayout(getLayout());
    panel.setSize(600, 450);

    scroll = new JScrollPane(area);
    scroll.setBounds(0, 0, 10, 395);


    field = new JTextField();
    field.setSize(595, 25);
    field.setLocation(0, 400);
    field.addActionListener(
            new ActionListener(){
            public void actionPerformed(ActionEvent event){
                showCommand(event.getActionCommand());
                field.setText("");
                }
            }
        );
    area = new JTextArea();
    area.setSize(595,395);
    area.setLocation(0,0);
    area.setEditable(false);
    area.setFont(new Font("Lucida Console", Font.BOLD, 13));
    area.setText("Welcome to JConsole! This Application is for executing commands."
            + "\nType help for more info");

    panel.add(new JScrollPane(area));
    panel.add(area);
    panel.add(field);
    add(panel);

}

This is just my constructor bu nothing else in the code involves the jscrollpane . 这只是我的构造函数,而代码中没有其他涉及jscrollpane

You added area two times. 您添加了两次area First one with JScrollPane and second one without JScrollPane. 第一个使用JScrollPane,第二个不使用JScrollPane。 You should remove second part. 您应该删除第二部分。

panel.add(new JScrollPane(area));
//panel.add(area); Remove this part.

Also make sure proper layout for area . 另外,请确保正确布局area By default JPanel is FlowLayout , but for JTextArea it should be on BorderLayout with CENTER alignment. 默认情况下, JPanelFlowLayout ,但对于JTextArea它应该位于具有CENTER对齐方式的BorderLayout上。

您可以尝试添加

scroll.setViewportView(area);

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

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