1

我有一个文档比较程序,它使用 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))
    );

我知道我不应该使用窗口生成器并手动创建它。有任何想法吗?

4

0 回答 0