如何在jscrollpane中自动添加滚动条? [英] How to add scrollbar automatic in jscrollpane?

查看:61
本文介绍了如何在jscrollpane中自动添加滚动条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试编写这样的 GUI.每次创建新按钮并放置在特定位置时单击按钮,但在jscrollpane中添加一些按钮后,滚动条未激活,因此我无法看到所有创建的按钮.

I try to program a GUI like this. When a button clicked every time a new button is created and placed at specific position but after adding some buttons in jscrollpane, scrollbar not activated, so I unable to see all created buttons.

我的代码在这里:

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

public class Test{

    private JFrame frame;
    private JPanel panel1,panel2;
    private JScrollPane pane;
    private JButton button;
    int i = 1, y = 10;

    public Test()
    {
        panel2 = new JPanel(null);
        panel2.setBounds(0,0,280,300);

        button = new JButton("Add Button");
        button.setBounds(90,10,120,30);

        pane = new JScrollPane();
        pane.setBounds(10,50,280,300);

        panel1 = new JPanel(null);
        panel1.setPreferredSize(new Dimension(300,400));
        panel1.setBackground(Color.WHITE);

        frame = new JFrame("Test");
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel1);
        frame.pack();

        panel1.add(pane);
        panel1.add(button);

        pane.add(panel2);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel2.add(new JButton("Button "+i)).setBounds(80,y,120,30);
                i += 1;
                y += 35;
            }
        });
    }
    public static void main(String[] args) {
        new Test();
    }
}

推荐答案

不要使用空布局.不要使用 setBounds().

Don't use a null layout. Don't use setBounds().

只有当面板的首选大小大于滚动窗格的大小时,滚动条才会自动出现.

The scrollbars will only appear automatically when the preferred size of the panel is greater that the size of the scroll pane.

布局经理的工作是:

  1. 设置组件的位置
  2. 设置组件的大小
  3. 计算面板的首选尺寸.

因此,解决方案是在面板上使用适当的布局管理器.

So the solution is to use the appropriate layout manager on your panel.

例如,您可以使用 BoxLayout:

So for example you can use a BoxLayout:

//panel2 = new JPanel(null);
panel2 = new JPanel();
panel2.setLayout( new BoxLayout(panel2, BoxLayout.Y_AXIS) );

然后,当您将组件添加到可见框架时,您需要 revalidate() 面板以调用布局管理器:

And then when you add components to a visible frame you need to revalidate() the panel to invoke the layout manager:

//panel2.add(new JButton("Button "+i)).setBounds(80,y,120,30);
panel2.add(new JButton("Button "+i));
panel2.revalidate();

不需要panel1.只需将组件添加到框架中即可:

There is no need for panel1. Just add the components to the frame:

//panel1.add(pane);
//panel1.add(button);
frame.add(button, BorderLayout.PAGE_START);
frame.add(pane, BorderLayout.CENTER);

但还有其他问题:

pane = new JScrollPane();

您实际上需要将面板添加到滚动窗格中.所以代码应该是:

You actually need to add the panel to the scroll pane. So the code should be:

pane = new JScrollPane(panel2);

由于一个组件只能有一个父级,所以需要删除:

Since a component can only have a single parent, you need to remove:

pane.add(panel2);

因为 panel2 已添加到滚动窗格中.

Since the panel2 has been added to the scroll pane.

    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(panel1);
    frame.pack();

上面的逻辑是错误的.

您应该只在所有组件都添加到框架后调用 pack() 和 setVisible( true ).

You should only invoked pack() and setVisible( true ) AFTER all the component have been added to the frame.

所以发布的大部分代码都是错误的.

So most of the code posted is wrong.

首先阅读 Swing 教程中关于布局管理器<的部分/a>.下载可用的演示代码并了解如何更好地构建代码.修改您的特定示例的代码.

Start by reading the section from the Swing turtorial on Layout Managers. Download the working demo code and learn how to better structure your code. The modify the code for your specific example.

这篇关于如何在jscrollpane中自动添加滚动条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆