java中的水平和垂直滚动条

时间:2012-10-22 14:32:27

标签: java swing grouplayout jscrollbar

我有一个文档比较程序,它使用XWPFDocument APACHE POI将.docx文件显示到文本区域。我需要将一个滚动条附加到textarea,因为我创建了具有固定高度和宽度的textarea。因此,在文本区域中无法看到超过文本的高度和宽度的文件。我研究了一些代码并提出了这些代码。

    JTextArea textAreaFile1 = new JTextArea();
    textAreaFile1.setEditable(false);
    JScrollPane scroll = new JScrollPane ( textAreaFile1 );
    scroll.setViewportView(textAreaFile1);
    scroll.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );

我不知道如何将其渲染到我的群组布局中。我在创建GUI时使用了窗口构建器,因为代码是自动生成的,我不知道如何将其添加到自动生成的代码中。这是布局的代码。

GroupLayout groupLayout = new GroupLayout(frmOverallResults.getContentPane());
    groupLayout.setHorizontalGroup(
        groupLayout.createParallelGroup(Alignment.LEADING)
            .addGroup(groupLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                    .addGroup(groupLayout.createSequentialGroup()
                        .addComponent(textAreaFile1, GroupLayout.PREFERRED_SIZE, 371, GroupLayout.PREFERRED_SIZE)
                        .addGap(18))
                    .addGroup(groupLayout.createSequentialGroup()
                        .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                            .addGroup(groupLayout.createSequentialGroup()
                                .addComponent(lblNumberOfWords)
                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                .addComponent(wordsFile1)
                                .addPreferredGap(ComponentPlacement.RELATED, 20, Short.MAX_VALUE))
                            .addGroup(groupLayout.createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
                                    .addComponent(lblNumberOfWords_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(lblNumberOfSentences, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 122, Short.MAX_VALUE))
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                    .addComponent(charactersFile1)
                                    .addComponent(paragraphsFile1)))
                            .addGroup(groupLayout.createSequentialGroup()
                                .addComponent(Fil, GroupLayout.PREFERRED_SIZE, 91, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(ComponentPlacement.RELATED, 97, Short.MAX_VALUE)))
                        .addPreferredGap(ComponentPlacement.RELATED)))
                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                    .addGroup(groupLayout.createSequentialGroup()
                        .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
                            .addComponent(textAreaFile2, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 395, GroupLayout.PREFERRED_SIZE)
                            .addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                    .addComponent(label_3, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(label_4, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                    .addComponent(paragraphsFile2, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(charactersFile2, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE)))
                            .addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
                                .addComponent(label, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                .addComponent(wordsFile2, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap())
                    .addGroup(groupLayout.createSequentialGroup()
                        .addComponent(lblDocument, GroupLayout.PREFERRED_SIZE, 91, GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
            .addGroup(groupLayout.createSequentialGroup()
                .addGap(310)
                .addComponent(btnNewButton)
                .addContainerGap(356, Short.MAX_VALUE))
    );
    groupLayout.setVerticalGroup(
        groupLayout.createParallelGroup(Alignment.LEADING)
            .addGroup(groupLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                    .addGroup(groupLayout.createSequentialGroup()
                        .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                            .addComponent(lblNumberOfWords)
                            .addComponent(wordsFile1))
                        .addPreferredGap(ComponentPlacement.RELATED)
                        .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                            .addComponent(lblNumberOfSentences)
                            .addComponent(charactersFile1))
                        .addPreferredGap(ComponentPlacement.RELATED)
                        .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                            .addComponent(lblNumberOfWords_1)
                            .addComponent(paragraphsFile1))
                        .addGap(18)
                        .addComponent(Fil))
                    .addGroup(groupLayout.createSequentialGroup()
                        .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                            .addComponent(label)
                            .addComponent(wordsFile2))
                        .addGap(6)
                        .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                            .addComponent(label_3)
                            .addComponent(charactersFile2))
                        .addGap(6)
                        .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                            .addComponent(label_4)
                            .addComponent(paragraphsFile2))
                        .addGap(18)
                        .addComponent(lblDocument)))
                .addPreferredGap(ComponentPlacement.RELATED)
                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(textAreaFile2, GroupLayout.PREFERRED_SIZE, 448, GroupLayout.PREFERRED_SIZE)
                    .addComponent(textAreaFile1, GroupLayout.PREFERRED_SIZE, 448, GroupLayout.PREFERRED_SIZE))
                .addGap(18)
                .addComponent(btnNewButton)
                .addGap(24))
    );

我知道我应该没有使用窗口构建器并手动创建它。有什么想法吗?

0 个答案:

没有答案