简体   繁体   English

调整/添加组件大小时,滚动窗格滚动条会显示整个Jframe

[英]Scrollpane scrollbars to entire Jframe when resizing/adding components

I edited my project using only swing components and a layout (not null).So now i want to add scrollbars to all frame not only at a pictrure.Resizing and moving the scrollbars and showing components under when you scroll down.The difficult is that the frame has many components and users can add pictures and admin can add labels or other components,so i don't know what to redraw when the frame is resized.Redraw everything i can't see for example.I paste some code to tell me where i add scrollpane or scrollbar 我只使用摆动组件和布局(不为null)编辑项目,所以现在我不仅要在pictrure处向所有帧添加滚动条,还需要调整和移动滚动条的大小并在向下滚动时显示组件。框架有很多组件,用户可以添加图片,管理员可以添加标签或其他组件,因此在调整框架大小时我不知道要重画什么。例如,重画所有我看不到的东西。我粘贴了一些代码来告诉我在哪里添加滚动窗格或滚动条

public class Test extends JFrame {

private JPanel contentPane;

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

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

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

            //Suppose that here we have many jlabels,jbuttons,jtextfields and other
}

 }

I red some other examples and the problem is that i don't have only a picture to redraw or circles but stuff added by users.Its an online application. 我再举一些其他例子,问题是我没有重画或画圈的图片,而是用户添加的东西。它是一个在线应用程序。

I don't need a small program with a scrollbar example but help on my code how to add it in the entire frame and work dynamically.Resizing and moving the scrollbars and showing components under when you scroll down 我不需要一个带有滚动条示例的小程序,但是可以帮助我的代码在整个框架中添加它并动态地工作。

Yes you do need a simple example. 是的,您确实需要一个简单的例子。 You should start with something that works and then modify it to meet your needs. 您应该从一些可行的方法开始,然后对其进行修改以满足您的需求。 You have many problems with your code: 您的代码有很多问题:

  1. Don't mix AWT and Swing components. 不要混合使用AWT和Swing组件。
  2. Don't use a null layout. 不要使用空布局。
  3. Don't randomly set the size of a component. 不要随意设置组件的大小。 Every Swing component is designed to have a preferred size at which is should be displayed. 每个Swing组件都设计为具有应显示的首选大小。

Read the Swing tutorial . 阅读Swing tutorial You will find plenty of examples that will show you the proper way to use Swing components and build the initial GUI on the EDT. 您会发现很多示例,这些示例将向您展示使用Swing组件并在EDT上构建初始GUI的正确方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM