滚动条未出现在 JScrollPane 中

发布于 2024-11-19 08:07:02 字数 1511 浏览 6 评论 0原文

我想制作一个带有两个高度很高的按钮和一侧滚动条的窗口。问题是没有出现滚动条。这是我的代码

public class Window {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    //namestanje teme
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedLookAndFeelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    JFrame frame = new JFrame("frame");
//  frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setSize(500,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel(null);

    JButton but1 = new JButton();
    JButton but2 = new JButton();

    panel.add(but1);
    panel.add(but2);

    but1.setSize(50, 505);
    but2.setSize(50, 505);

    but1.setLocation(0, 0);
    but2.setLocation(400, 400);

    but1.setText("1");
    but2.setText("2");


    JScrollPane scroll = new JScrollPane(panel);

    frame.add(scroll);
    frame.setVisible(true);         
}
}

注意:首先,按钮的宽度很大(通过使用“11111111111111111111111111111”之类的名称来命名它们)并且会出现滚动条。然后我想要大的高度,并且必须在面板中放置 null 。现在没有出现滚动条。

I want to make a window with two buttons with great heights and a scrollbar on a side. The problem is that no scrollbar appears. Here is my code

public class Window {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    //namestanje teme
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedLookAndFeelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    JFrame frame = new JFrame("frame");
//  frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setSize(500,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel(null);

    JButton but1 = new JButton();
    JButton but2 = new JButton();

    panel.add(but1);
    panel.add(but2);

    but1.setSize(50, 505);
    but2.setSize(50, 505);

    but1.setLocation(0, 0);
    but2.setLocation(400, 400);

    but1.setText("1");
    but2.setText("2");


    JScrollPane scroll = new JScrollPane(panel);

    frame.add(scroll);
    frame.setVisible(true);         
}
}

Note: At first, the buttons had large widths (did that by naming them with something like "11111111111111111111111111111") and a scrollbar would appear. Then I wanted large heights and had to put null in panel. Now no scrollbar appears.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爺獨霸怡葒院 2024-11-26 08:07:02

当添加到滚动窗格的组件的首选大小大于滚动窗格的大小时,就会出现滚动条。

布局管理器的工作是确定面板的首选尺寸。布局管理器的工作还包括确定添加到面板的组件的大小和位置。

摆脱空布局并使用布局管理器,滚动条将在需要时自动出现。

如果您希望组件从垂直角度以不同的方式显示,那么您需要使用不同的布局管理器。也许您可以使用具有垂直布局的 BoxLayout 。您可以使用:

panel.add( Box.createVerticalStrut(400) );

在两个组件之间添加垂直空间。

Scrollbars appear when the preferred size of the component added to the scollpane is greater than the size of the scrollpane.

It is the job of the layout manager to determine the preferred size of the panel. It is also the job of the layout manager to determine the size and location of the components added to the panel.

Get rid of the null layout and use a layout manager and scrollbars will appear when required automatically.

If you want components to be displayed differently from a vertical point of view, then you need to use a different layout manager. Maybe you can use a BoxLayout with a verticxal layout. You can use:

panel.add( Box.createVerticalStrut(400) );

to add vertical space between the two components.

无声情话 2024-11-26 08:07:02

要使滚动条始终出现,请使用:

yourScrollBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

...

import javax.swing.*;
import java.awt.*;
public class Window {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        //namestanje teme
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        JFrame frame = new JFrame("frame");
    //  frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setPreferredSize(new Dimension(100,95));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();

        JButton but1 = new JButton();
        JButton but2 = new JButton();

        panel.add(but1);
        panel.add(but2);

        but1.setSize(50, 505);
        but2.setSize(50, 505);

        but1.setLocation(0, 0);
        but2.setLocation(400, 400);

        but1.setText("1");
        but2.setText("2");


        JScrollPane scroll = new JScrollPane(panel);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        frame.setLayout(new FlowLayout());
        frame.pack();
        frame.add(scroll);
        frame.setVisible(true);         
    }

}

To make the scrollbar always appear, use:

yourScrollBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

...

import javax.swing.*;
import java.awt.*;
public class Window {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        //namestanje teme
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        JFrame frame = new JFrame("frame");
    //  frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setPreferredSize(new Dimension(100,95));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();

        JButton but1 = new JButton();
        JButton but2 = new JButton();

        panel.add(but1);
        panel.add(but2);

        but1.setSize(50, 505);
        but2.setSize(50, 505);

        but1.setLocation(0, 0);
        but2.setLocation(400, 400);

        but1.setText("1");
        but2.setText("2");


        JScrollPane scroll = new JScrollPane(panel);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        frame.setLayout(new FlowLayout());
        frame.pack();
        frame.add(scroll);
        frame.setVisible(true);         
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文