协助在JTextArea上显示滚动条?

时间:2015-01-11 15:58:50

标签: java swing user-interface awt

基本上我不知道我的代码有什么问题,我试图将滚动条放到JTextAre上,但是JTextArea只是不断调整大小而不是滚动条付诸行动。我宣布" t1"作为私人JTextArea字段和" s1"作为私有JScrollPane字段。我也有一些GridBagConstraints," jp"是JPanel。这是我的代码:`

gbc.insets = new Insets(10,0,0,0);
    gbc.gridx=1;
    gbc.gridy=2;
    t1 = new JTextArea(5,15);
    s1 = new JScrollPane(t1, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    jp.add(s1,gbc);

1 个答案:

答案 0 :(得分:0)

这是一个可行的演示:

public class White extends JPanel {
    public White(){
        setBackground( Color.BLACK );
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10,0,0,0);
        gbc.gridx=1;
        gbc.gridy=2;
        JTextArea t1 = new JTextArea(5,15);
        JScrollPane s1 = new JScrollPane(t1, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        this.add(s1,gbc);
    }
    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(new Color(0xFF, 0xFF, 0xFF));
    }
    public static void main( String[] args ){
        JFrame frame = new JFrame();
        frame.add( new White() );
        frame.setTitle( "Hello World" );
        frame.setSize( 600, 400 );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setVisible( true );
    }
}