薇薇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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog