簡體   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