jscrollpane卷轴仅出现在底部

发布于 2025-02-06 00:23:38 字数 1744 浏览 3 评论 0原文

我有这个jframe具有jscrollpanejtextarea ,它可以正常工作,但是滚动按钮仅出现在底部,我想要它默认显示在顶部。我该如何更改?

public class frameMyPasswords extends JFrame {

    private JPanel contentPane;

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

    /**
     * Create the frame.
     */
    public frameMyPasswords() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        //scrollPane.getViewport().setViewPosition( new Point(0, 0) );
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.getVerticalScrollBar().setValue(0);
        scrollPane.setBounds(10, 45, 414, 194);
        contentPane.add(scrollPane);
        
        JTextArea txtField = new JTextArea();
        scrollPane.setViewportView(txtField);
        
        txtField.setLineWrap(true);
        txtField.setEditable(false);
        txtField.setToolTipText("");
        txtField.setText("LONG TEXT");
    }
}

I have this JFrame that has a JScrollPane and a JTextArea inside it, it works okay but the scroll button only appears at the bottom, I want it to show at the top as default. How can I change this?

public class frameMyPasswords extends JFrame {

    private JPanel contentPane;

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

    /**
     * Create the frame.
     */
    public frameMyPasswords() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        //scrollPane.getViewport().setViewPosition( new Point(0, 0) );
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.getVerticalScrollBar().setValue(0);
        scrollPane.setBounds(10, 45, 414, 194);
        contentPane.add(scrollPane);
        
        JTextArea txtField = new JTextArea();
        scrollPane.setViewportView(txtField);
        
        txtField.setLineWrap(true);
        txtField.setEditable(false);
        txtField.setToolTipText("");
        txtField.setText("LONG TEXT");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文