滚动窗格不起作用

时间:2012-06-24 20:48:17

标签: java swing jscrollpane jtextarea

我试图将大量数据(成员资格记录)输出到JTextArea,然后将其转储到scrollPane中。这是代码。

package Ginfo;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.io.*;


public class ReviewAll extends JFrame implements ActionListener {
    Container RA;
    JPanel ReviewAll;
    JTextArea output;
    JButton back, quit;

    Message m;
    ConnectInfo c;
    ObjectOutputStream oout;
    ObjectInputStream oin;

    public ReviewAll(ObjectOutputStream oout2, ObjectInputStream oin2,
            Message m2, ConnectInfo a) {
        super("Full results");
        m = m2;
        c = a;
        oout = oout2;
        oin = oin2;
        try {
            oout.reset();
        } catch (IOException e) {

        }
        RA = getContentPane();
        BuildGUI();
        RA.add(ReviewAll);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);

        try {
            m.type = Message.REVIEWALL;
            oout.writeObject(m);
            while (!m.response.equals("End of list")) {
                m = (Message) oin.readObject();
                output.append("Member: " + m.main + "\n");
                output.append("Invited by: " + m.invitedBy + " on " + m.when
                        + "\n");
                output.append("Recommended by: " + m.HDTH + "\n\n");
            }
            m.response = "";

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    private void BuildGUI() {
        ReviewAll = new JPanel();
        output = new JTextArea();
        output.setPreferredSize(new Dimension(400, 400));
        output.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(output);
        scrollPane
                .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        back = new JButton("Close window");
        quit = new JButton("Exit Program");

        ReviewAll.add(scrollPane, BorderLayout.NORTH);
        ReviewAll.add(back, BorderLayout.SOUTH);
        ReviewAll.add(quit, BorderLayout.SOUTH);

        back.addActionListener(this);
        quit.addActionListener(this);
    }

    public void actionPerformed(ActionEvent re) {
        if (re.getSource() == back) {
            new WhatToDo(oout, oin, m, c);
            dispose();
        } else if (re.getSource() == quit) {
            if (oout != null) {
                try {
                    oout.flush();
                    oout.close();
                    oin.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            System.exit(0);
        }
    }
}

当我这样做的时候,我只看到前5条记录(足以填满我的JTextArea)并且无法向下滚动以查看其余部分。为了记录,当发送的记录保存在list.size()并且我知道它正常工作时,我将服务器代码上的m.response =“End of List”转移到客户端(如下所列)。我知道这是卷轴的一个问题。谢谢你的帮助。

1 个答案:

答案 0 :(得分:5)

  

我知道这是卷轴的一个问题。

问题不在于滚动条,而在于您如何设置JTextArea的大小或preferredSize,这会阻止它正确扩展。而是设置它的列和行,因为这将告诉JScrollPane它应在视口中查看多少行和列。

即改变这个:

ReviewAll = new JPanel();
output = new JTextArea();
output.setPreferredSize(new Dimension (400,400));

到此:

ReviewAll = new JPanel();

// ROW_COUNT & COL_COUNT are int constants defined elsewhere
output = new JTextArea(ROW_COUNT, COL_COUNT); // using whatever numbers work best.

// don't set the text area's preferredSize:
// output.setPreferredSize(new Dimension (400,400));

当然最好避免使用魔术数字,而是使用常量来计算行和列数。