如何将 jscrollpane 添加到 jframe? [英] how to add jscrollpane to jframe?

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

问题描述

我有以下源代码...有人可以给我建议如何将 jscrollpane 添加到 jframe 上吗?我尝试了几次将它添加到 jframe 但没有任何进展.它甚至没有显示.

public class Form3 {JFrame jframe = new JFrame("Etiket print.");JPanel panel1 = new JPanel();JPanel panel2 = new JPanel();JPanel panel3 = new JPanel();JPanel panel4 = new JPanel();JScrollPane scrollFrame = new JScrollPane(panel2);颜色 myBlue1Color = 新颜色(168, 175, 247);颜色 myBlue2Color = 新颜色(139, 146, 255);公共 Form3(){jframe.setMinimumSize(new Dimension(1280, 1000));panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));panel2.setAutoscrolls(true);jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//---------------------------------标题panel1 = createSquareJPanel(Color.YELLOW, 600,200);panel3 = createSquareJPanel(Color.GREEN, 400,200);panel4 = createSquareJPanel(Color.white, 280,200);JPanel 容器 = new JPanel();JPanel container1 = new JPanel();JPanel container2 = new JPanel();container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));container1.setLayout(new BoxLayout(container1, BoxLayout.Y_AXIS));container2.setLayout(new BoxLayout(container2, BoxLayout.X_AXIS));容器 1.添加(面板 1);container2.add(container1);container2.add(panel3);container2.add(panel4);容器.添加(容器 2);容器.添加(面板2);{for (int i=0; i<25; i++){JPanel线束面板=新的JPanel();线束面板.setMinimumSize(新维度(1280, 70));线束面板.setMaximumSize(新维度(1280, 70));harnessPanel.setPreferredSize(new Dimension(1280, 70));如果(我%2== 0){线束面板.setBackground(myBlue1Color);}别的 {线束面板.setBackground(myBlue2Color);}panel2.add(harnessPanel);线束面板.repaint();线束面板.validate();}panel2.repaint();panel2.validate();}jframe.add(scrollFrame);jframe.add(容器);jframe.pack();jframe.setLocationRelativeTo(null);jframe.setVisible(true);}私人 JPanel createSquareJPanel(颜色颜色,int size1,int size2){JPanel tempPanel = new JPanel();tempPanel.setBackground(颜色);tempPanel.setMinimumSize(new Dimension(size1, size2));tempPanel.setMaximumSize(new Dimension(size1, size2));tempPanel.setPreferredSize(new Dimension(size1, size2));返回临时面板;}公共静态无效主(字符串参数[]){SwingUtilities.invokeLater(new Runnable() {@覆盖公共无效运行(){Form3 myF=new Form3();}});};}

我的应用图片:

实际状态:

解决方案

JFrame 默认使用 BorderLayout.它的默认位置(如果你没有指定)是 CENTER.

BorderLayout 将只允许一个组件占据其 5 个可用位置中的任何一个.

所以当你...

jframe.add(scrollFrame);jframe.add(容器);

它将 scrollFrame 添加到中心位置并在您添加 container 时有效地将其删除(实际上并没有删除它,但结果几乎相同).

尝试提供位置约束.例如...

jframe.add(scrollFrame);jframe.add(容器,BorderLayout.SOUTH);

有关详细信息,请参阅如何使用 BorderLayout>

I have following source code...Can someone please give me an advice how to add jscrollpane onto jframe? I tried several time to add it to jframe but without any progress. It is not even showing.

public class Form3 {
        JFrame jframe = new JFrame("Etiket print.");
        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();
        JPanel panel4 = new JPanel();
        JScrollPane scrollFrame = new JScrollPane(panel2);
        Color myBlue1Color = new Color(168, 175, 247);
        Color myBlue2Color = new Color(139, 146, 255);



           public Form3(){
                jframe.setMinimumSize(new Dimension(1280, 1000));
                panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
                panel2.setAutoscrolls(true);
                jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                //---------------------------------Header
                panel1 = createSquareJPanel(Color.YELLOW, 600,200);
                panel3 = createSquareJPanel(Color.GREEN, 400,200);
                panel4 = createSquareJPanel(Color.white, 280,200);
                JPanel container = new JPanel();
                JPanel container1 = new JPanel();
                JPanel container2 = new JPanel();
                container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
                container1.setLayout(new BoxLayout(container1, BoxLayout.Y_AXIS));
                container2.setLayout(new BoxLayout(container2, BoxLayout.X_AXIS));


                container1.add(panel1);
                container2.add(container1);
                container2.add(panel3);
                container2.add(panel4);
                container.add(container2);
                container.add(panel2);

                {

                    for (int i=0; i<25; i++){

                        JPanel harnessPanel= new JPanel();
                        harnessPanel.setMinimumSize(new Dimension(1280, 70));
                        harnessPanel.setMaximumSize(new Dimension(1280, 70));
                        harnessPanel.setPreferredSize(new Dimension(1280, 70));
                        if(i%2==0) {
                            harnessPanel.setBackground(myBlue1Color);
                        }
                        else {
                            harnessPanel.setBackground(myBlue2Color);
                        }
                        panel2.add(harnessPanel);

                        harnessPanel.repaint();
                        harnessPanel.validate();
                    }
                    panel2.repaint();
                    panel2.validate();
                }
                jframe.add(scrollFrame);
                jframe.add(container);

                jframe.pack();
                jframe.setLocationRelativeTo(null);
                jframe.setVisible(true);

            }

            private JPanel createSquareJPanel(Color color, int size1, int size2)
            {
                JPanel tempPanel = new JPanel();
                tempPanel.setBackground(color);
                tempPanel.setMinimumSize(new Dimension(size1, size2));
                tempPanel.setMaximumSize(new Dimension(size1, size2));
                tempPanel.setPreferredSize(new Dimension(size1, size2));
                return tempPanel;
            }

            public static void main (String args[]){
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        Form3 myF=new Form3();

                    }
                });
            };
        }

picture of my app:

actual state:

解决方案

JFrame uses a BorderLayout by default. It's default position (if you don't specify one) is CENTER.

BorderLayout will only allow one component to occupy any of it's 5 available positions.

So when you do...

jframe.add(scrollFrame);
jframe.add(container);

It adds the scrollFrame to the center position and effectively removes it when you add container (it doesn't actually remove it, but the result is just about the same).

Try supplying a position constraint. For example...

jframe.add(scrollFrame);
jframe.add(container, BorderLayout.SOUTH);

See How to use BorderLayout for more details

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

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