简体   繁体   English

协助在JTextArea上显示滚动条?

[英]Assistance displaying a scroll-bar onto a JTextArea?

Well basically I don't know what's wrong with my code, i'm trying to put a scrollbar onto a JTextAre but the JTextArea just keeps resizing instead of the scrollbar going into action.I have declared "t1" as a private JTextArea field and "s1" as a private JScrollPane field. 好吧,基本上我不知道我的代码有什么问题,我试图将滚动条放到JTextAre上,但JTextArea只是保持调整大小,而不是滚动条开始起作用。我已将“ t1”声明为私有JTextArea字段,并且“ s1”作为私有JScrollPane字段。 I also have some GridBagConstraints, "jp" is the JPanel. 我也有一些GridBagConstraints,“ jp”是JPanel。 Here's my code: ` 这是我的代码:

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

This is a demo as it works: 这是一个演示,因为它有效:

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

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

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