jscrollpane滚动条不会出现?

jaxagkaj  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(433)
package me.an.ugm;

import java.awt.EventQueue;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;

public class Application
{
    private JFrame frame;

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    Application window = new Application();
                    window.frame.setVisible(true);
                } catch (Exception e)
                {
                    e.printStackTrace();
                }
            }

        });
    }

    public Application()
    {
        initialize();
    }

    private void initialize()
    {
        frame = new JFrame();
        frame.setTitle("Application");
        frame.setBounds(100, 100, 401, 450);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        //frame.setResizable(false);
        frame.setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setBounds(0, 0, 296, 710);
        panel.setLayout(null);

        JScrollPane scrollPane = new JScrollPane(panel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setBounds(0, 0, 296, 399);
        frame.getContentPane().add(scrollPane);

        for (int i = 0, j = 10; i < 20; i++, j += 35)
        {
            JButton button1 = new JButton();
            button1.setBounds(10, j, 25, 25);
            panel.add(button1);

            JComboBox<String> selectorBox = new JComboBox<>();
            selectorBox.setBounds(40, j, 200, 25);
            panel.add(selectorBox);

            JButton button2 = new JButton();
            button2.setBounds(245, j, 25, 25);
            panel.add(button2);
        }
    }
}

我不知道为什么滚动条不会出现。jpanel比jscrollpane大,所以我认为它应该出现。另外,当我尝试使用setpreferredsize作为滚动窗格而不是setbounds或setsize时,什么也没有显示。这个程序的最终目标是在右边关闭一个按钮,以添加另一组按钮(循环中的按钮),用于选择另一项。我希望程序以10行按钮开始,但我将其设置为20以测试滚动条。是因为没有一个布局的问题还是我把滚动窗格搞砸了?

r8uurelv

r8uurelv1#

问题是没有使用正确的swing布局。
这是我创建的gui。

我在按钮和组合框中添加了文本,这样gui看起来更真实。我对关注gui的外观和感觉进行了注解。我更改了类的名称,因为我为堆栈溢出编写的所有代码都有一个测试包。
我用一个单独的方法创建了按钮jpanel。我用了网格布局。这允许我创建20行3个swing组件。这也让我可以把组件隔开一点。
我用另一种方法创建了scroll jpanel。关键是使用滚动jpanel的borderlayout。我把滚动面板做成按钮的一半大小,这样它就可以滚动了。你可以随意调整这个计算。
下面是完整的可运行代码。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class JButtonScrollGUI {
    private JFrame frame;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
//                  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    new JButtonScrollGUI();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        });
    }

    private String[] greekAlphabet;

    public JButtonScrollGUI() {
        this.greekAlphabet = new String[] { "alpha", "beta", "gamma", "epsilon", "zeta" };
        initialize();
    }

    private void initialize() {
        frame = new JFrame();
        frame.setTitle("Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(createScrollPanel(), BorderLayout.CENTER);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel createScrollPanel() {
        JPanel panel = new JPanel(new BorderLayout());

        JPanel innerPanel = createButtonPanel();
        Dimension d = innerPanel.getPreferredSize();
        d.width += 50;
        d.height /= 2;
        panel.setPreferredSize(d);

        JScrollPane scrollPane = new JScrollPane(innerPanel);

        panel.add(scrollPane, BorderLayout.CENTER);
        return panel;
    }

    private JPanel createButtonPanel() {
        JPanel panel = new JPanel(new GridLayout(0, 3, 10, 10));
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        for (int i = 0; i < 20; i++) {
            JButton button1 = new JButton("Previous " + i);
            panel.add(button1);

            JComboBox<String> selectorBox = new JComboBox<>(greekAlphabet);
            panel.add(selectorBox);

            JButton button2 = new JButton("Next " + i);
            button2.setPreferredSize(button1.getPreferredSize());
            panel.add(button2);
        }

        return panel;
    }

}

相关问题