滚动条不起作用

时间:2012-01-08 11:31:48

标签: java swing jscrollpane jtextarea

嗨,这是我的滚动条的代码。但不幸的是它不起作用。怎么会这样?

text_area = new JTextArea();
text_area.setPreferredSize(new Dimension(250,150));
text_area.setLineWrap(true);
scrollpane = new JScrollPane(text_area); 

当我插入文本时,它比区域长,滚动条不会出现。

这是代码:

public AziendaGUI() {

    company = new Azienda();

    frame = new JFrame("Immobiliari s.p.a");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    view_list = new JButton("View Property");
    view_list.setActionCommand("view_list");
    view_list.addActionListener(this);

    save_list = new JButton("Save List");
    save_list.setActionCommand("save_list");
    save_list.addActionListener(this);

    text_area = new JTextArea();
    text_area.setPreferredSize(new Dimension(250,150));
    text_area.setLineWrap(true);
    scrollpane = new JScrollPane(text_area); //Non funziona la scroll bar

    grid = new GridBagLayout();
    pane = new JPanel(grid);

    /* Set Constraints view_list button */
    grid.setConstraints(view_list, new GridBagConstraints(0,0,1,1,0.0,0.0,GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(5,5,5,5),0,0));
    pane.add(view_list);

    /* Set Constraints save_list button */
    grid.setConstraints(save_list,new GridBagConstraints(1,0,1,1,0.0,0.0,GridBagConstraints.EAST,GridBagConstraints.NONE,new Insets(5,5,5,5),0,0));
    pane.add(save_list);

    /* Set Constraint text area */
    grid.setConstraints(scrollpane, new GridBagConstraints(0,1,2,1,0.0,0.0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(5,5,5,5),0,0));
    pane.add(scrollpane);

    frame.setLayout(new FlowLayout());
    frame.add(pane);

    frame.pack();
    frame.setVisible(true);
}

1 个答案:

答案 0 :(得分:2)

设置JScrollPane的首选大小。

设置JTextArea的首选大小。

来源: 我略微修改了源代码并且工作正常。