繁体   English   中英

如何将JScrollPane添加到此JTextArea?

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

(我是新来的Java BTW)我有这个未完成的计划,我想一个添加jscrollpanejtextarea 但是我无法正常工作。 我试着做

add(new JScollPane(area));

这没有用。 我试过了:

panel.add(new JScrollPane(area));

而且那也不起作用。 我也尝试过:

area.add(pane);

和:

area.add(new JScrollPane());

但没有任何效果。 救命!!! 看一看

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);

}

这只是我的构造函数,而代码中没有其他涉及jscrollpane

您添加了两次area 第一个使用JScrollPane,第二个不使用JScrollPane。 您应该删除第二部分。

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

另外,请确保正确布局area 默认情况下, JPanelFlowLayout ,但对于JTextArea它应该位于具有CENTER对齐方式的BorderLayout上。

您可以尝试添加

scroll.setViewportView(area);

暂无
暂无

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

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