薇薇2013 2011-10-13 15:53
浏览 328
已采纳

在JTextArea区加上JScrollPane滚动条怎么做?

代码如下,我想实现的是在JTextArea加上JScrollPane,我用
JScrollPane scrollPane = new JScrollPane(getJtextArea());
add(scrollPane)实现不了,请问有好的方法吗?谢谢!

[code="java"]
package com.dr.swt.xuechengxitong;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

@SuppressWarnings("serial")
public class Teste3 extends JFrame{
private static JTextArea jTextArea;
private JButton jButton1;
private JButton jButton2;
private JButton jButton3;

public Teste3(){    
    GridBagLayout gridbag = new GridBagLayout();
    this.setLayout(gridbag);
    gridbag.setConstraints(getJtextArea(), new GridBagConstraints(0,0,3,2,1.0,1.0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(100,100,30,100),0,0));
    gridbag.setConstraints(getJButton1(), new GridBagConstraints(0,2,1,1,1.0,0.0,GridBagConstraints.SOUTH,GridBagConstraints.HORIZONTAL,new Insets(30,200,100,100),0,0));
    gridbag.setConstraints(getJButton2(), new GridBagConstraints(1,2,1,1,1.0,0.0,GridBagConstraints.SOUTH,GridBagConstraints.HORIZONTAL,new Insets(30,100,100,100),0,0));
    gridbag.setConstraints(getJButton3(), new GridBagConstraints(2,2,1,1,1.0,0.0,GridBagConstraints.SOUTH,GridBagConstraints.HORIZONTAL,new Insets(30,100,100,200),0,0));
    this.add(getJtextArea());
    this.add(getJButton1());
    this.add(getJButton2());
    this.add(getJButton3());
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();    
    int screenWidth = (int) screenSize.getWidth(); 
    int screenHight = (int) screenSize.getHeight();
    this.setSize(screenWidth, screenHight);
    this.setVisible(true);
}

private JTextArea getJtextArea(){
    if(jTextArea == null){
        jTextArea = new JTextArea();        
    }
    return jTextArea;
}

private JButton getJButton1(){
    if(jButton1 == null){
        jButton1 = new JButton("jButton1");
    }
    return jButton1;
}

private JButton getJButton2(){
    if(jButton2 == null){
        jButton2 = new JButton("jButton2");
    }
    return jButton2;
}

private JButton getJButton3(){
    if(jButton3 == null){
        jButton3 = new JButton("jButton3");
    }
    return jButton3;
}

public static void main(String args[]){
    new Teste3();
}

}
[/code]

  • 写回答

2条回答 默认 最新

  • _1_1_7_ 2011-10-13 18:47
    关注

    [code="java"]
    GridBagLayout gridbag = new GridBagLayout();
    this.setLayout(gridbag);
    JScrollPane scroll = new JScrollPane(getJtextArea(), JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    gridbag.setConstraints(scroll, new GridBagConstraints(0, 0, 3, 2, 1.0, 1.0,
    GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(10, 10, 100, 100),
    0, 0));
    gridbag.setConstraints(getJButton1(), new GridBagConstraints(0, 2, 1, 1, 1.0, 0.0,
    GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, new Insets(30, 200, 100,
    100), 0, 0));
    gridbag.setConstraints(getJButton2(), new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0,
    GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, new Insets(30, 100, 100,
    100), 0, 0));
    gridbag.setConstraints(getJButton3(), new GridBagConstraints(2, 2, 1, 1, 1.0, 0.0,
    GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, new Insets(30, 100, 100,
    200), 0, 0));

        this.add(scroll);
        this.add(getJButton1());
        this.add(getJButton2());
        this.add(getJButton3());
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int screenWidth = (int) screenSize.getWidth();
        int screenHight = (int) screenSize.getHeight();
        this.setSize(screenWidth, screenHight);
        this.setVisible(true);
    

    [/code]

    调整一下参数就好

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题