0

这是我尝试过的代码,但滚动窗格未显示并且正在使用组布局。

import java.awt.BorderLayout;

public class JScrollPanel extends JFrame {

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                JScrollPanel frame = new JScrollPanel();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public JScrollPanel() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);

    **final JTextArea textArea = new JTextArea();

    JScrollPane scrollpanedreapta = new JScrollPane(textArea, 
        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
           JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    contentPane.add(scrollpanedreapta, null);**

    GroupLayout gl_contentPane = new GroupLayout(contentPane);
    gl_contentPane.setHorizontalGroup(
        gl_contentPane.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_contentPane.createSequentialGroup()
                .addGap(31)
                .addComponent(textArea, GroupLayout.PREFERRED_SIZE, 339, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(54, Short.MAX_VALUE))
    );
    gl_contentPane.setVerticalGroup(
        gl_contentPane.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_contentPane.createSequentialGroup()
                .addGap(31)
                .addComponent(textArea, GroupLayout.PREFERRED_SIZE, 172, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(49, Short.MAX_VALUE))
    );
    contentPane.setLayout(gl_contentPane);
}
}

当我启动代码时,它工作正常,当我溢出文本区域时,两个滚动窗格都没有显示。任何帮助家伙。

4

1 回答 1

3

您应该将 添加JScrollPane到您的布局组而不是JTextArea,即替换

.addComponent(textArea, ...)

.addComponent(scrollpanedreapta, ...)

两次。

于 2013-01-30T16:37:18.070 回答