JScrollPane不适用于我的JPanel [英] JScrollPane doesn't work for my JPanel

查看:150
本文介绍了JScrollPane不适用于我的JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我必须说我已经检查了这些问题并且没有找到我的答案:

first of all I must say that I have checked these questions and didn't find my answer :

1 2 3 4 5 6 7

以及其他许多问题喜欢这样

and many other questions like so

我也检查了这些教程和示例:

also I have checked these tutorials and examples:

1 9 10 11

和许多其他网站。但我无法解决我的问题。

and many other sites. but I couldn't fix my problem.


这是我的代码的简单类型:

and this is the simple kind of my code:

public class Question extends JFrame {
public Question() {
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    setLayout(new BorderLayout());
    setSize(d.width, d.height);
    setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    panel.setPreferredSize(new Dimension(d.width, d.height));
    panel.setBorder(BorderFactory.createLineBorder(Color.red));
    panel.setLayout(new BoxLayout(panel, 1));
    for (int i = 0; i < 100; i++) {
        panel.add(new JButton("kjdh"));
    }
    JScrollPane scrollPane = new JScrollPane(panel);
    scrollPane.setPreferredSize(new Dimension(500, 500));
    getContentPane().add(scrollPane);
    getContentPane().add(panel);
    setVisible(true);

}

public static void main(String[] args) {
    new Question();
} 
}

JScrollPane 没有出现。我测试过很多东西。我已经改变了将面板 scrollPane 添加到我的框架但它不起作用。任何人都可以帮助我吗?

but the JScrollPane doesn't appear. I have tested many things. I have changed the way adding panel and scrollPane to my frame but it didn't work. can any one help me plz?

推荐答案


  1. 不要在面板上设置首选大小。请参阅我是否应避免在Java Swing中使用setPreferred / Maximum / MinimumSize方法?出于原因。

  2. 仅将 滚动窗格添加到内容窗格。

  1. Don't set a preferred size on the panel. See Should I avoid the use of setPreferred/Maximum/MinimumSize methods in Java Swing? for the reasons why.
  2. Add only the scroll pane to the content pane.

  1. 使用默认布局( BorderLayout )的内容窗格将默认将组件放入 CENTER 约束(如果没有提供), CENTER 区域只能接受单个组件。

  2. 除此之外该面板已经添加到滚动窗格中,它已经显示在其中,并且只能出现在一个容器中。

  1. A content pane using the default layout (BorderLayout) will default to putting the component in the CENTER constraint if none is supplied, and the CENTER area can only accept a single component.
  2. Besides that, the panel has already been added to the scroll pane, it will already appear inside it, and can only appear in a single container.


  • 不要扩展框架,只使用一个实例。

  • 不要 setSize ,但 setExtendedState

  • 应在EDT上构建和更新GUI。

  • 更好的关闭操作是 DISPOSE_ON_CLOSE

  • Don't extend frame, just use an instance of one.
  • Don't setSize, but setExtendedState.
  • GUIs should be constructed and updated on the EDT.
  • A better close operation is DISPOSE_ON_CLOSE.






  • import java.awt.*;
    import javax.swing.*;
    
    public class Question {
    
        public Question() {
            JFrame f = new JFrame();
            f.setLayout(new BorderLayout());
            f.setResizable(false);
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            JPanel panel = new JPanel();
            panel.setBorder(BorderFactory.createLineBorder(Color.red));
            panel.setLayout(new BoxLayout(panel, 1));
            for (int i = 0; i < 100; i++) {
                panel.add(new JButton("kjdh"));
            }
            JScrollPane scrollPane = new JScrollPane(panel);
            f.getContentPane().add(scrollPane);
            f.pack();
            f.setExtendedState(JFrame.MAXIMIZED_BOTH);
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    new Question();
                }
            };
            SwingUtilities.invokeLater(r);
        }
    }
    

    这篇关于JScrollPane不适用于我的JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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