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

在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条回答 默认 最新

  • u011606457
    _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]

    调整一下参数就好

    点赞 评论
  • u011606457
    _1_1_7_ 2011-10-13 16:48

    上次好像有个类似的问题,写过一个代码,如下:
    [code="java"]
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Toolkit;

    import javax.swing.*;

    import bluechip.swing.UIUtils;

    public class JTextAreaTest {

    private static JComponent getView() {
        JTextArea jTextArea = new JTextArea();
        jTextArea.setBounds(100, 70, 1300, 700);
        jTextArea.setLineWrap(true);
        return jTextArea;
    }
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        JScrollPane scroll = new JScrollPane(getView(), JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scroll.setPreferredSize(new Dimension(400, 200));
        JPanel rowView = new JPanel();
    

    // rowView.setBackground(Color.white);
    rowView.setPreferredSize(new Dimension(30, 200));
    scroll.setRowHeaderView(rowView);
    JPanel columeView = new JPanel();
    // columeView.setBackground(Color.white);
    columeView.setPreferredSize(new Dimension(400, 30));
    scroll.setColumnHeaderView(columeView);
    JFrame frame = new JFrame();
    frame.add(scroll);

        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
    

    }[/code]

    点赞 评论

相关推荐