在CardLayout滚动的JPanel?(Scrollable JPanel in a CardL

2019-10-19 18:07发布

我一直在寻找一种方式来垂直滚动条添加到JPanel ,反过来被添加到CardLayout面板。 我抬起头,所有的担心与实施滚动的帖子在这里JPanel ,但我无法弄清楚如何与这个特定的实现了它CardLayout 。 Oracle不给,我可以用任何一个例子。

也许我不使用的正确配置JWindow或即时通讯使用的任何其他组件。

我已经把我下面的程序的精简版本,我想实现一个垂直滚动条。

我的问题是如何实现JPanelScrollable类的代码的底部,以便它能够成为滚动?

import javax.swing.*;

import java.awt.*;

import java.net.URL ;


public class Program2 extends JFrame
{
    public Program2()
    {
        super("Flash CC");
        Container2 container = new Container2();
        setSize(700, 800);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setFocusable( true ) ;
        add(container);

        setVisible(true) ; 
    }


    public static void main(String[] args)
    {
        Program2 prog = new Program2();
    }
}

class Container2 extends JPanel
{
    private CardLayout          cardLayout   = new CardLayout() ;
    private JPanel1             jPanelFirst  = new JPanel1() ;
    private JPanel2             jPanelSecond = new JPanel2() ;
    private JPanelScrollable    jPanelThird  = new JPanelScrollable() ;

    //Constructor
    Container2()
    {
        this.setLayout( cardLayout )   ;
        this.setFocusable( true ) ;

        JScrollPane scrollFrame = new JScrollPane(jPanelThird);

        this.add( jPanelFirst,  "first card" )  ;
        this.add( jPanelSecond, "second card" ) ;
        this.add( scrollFrame , "third card" ) ;

        cardLayout.show( this, "third card" ) ;
    }
}

class JPanel1 extends JPanel
{

}

class JPanel2 extends JPanel
{

}

class JPanelScrollable extends JPanel
{
    // here many, many, many elemnts will go 
    // and a vertical scroll barr is needed to   view'm all.
    JPanelScrollable()
    {
        this.setOpaque( true ) ;
        this.setLayout( null ) ;
        for(int i=0; i<30; i++)
        {
            JButton b = new JButton("Button" + i) ;
            b.setBounds(0, (i * 100), 100, 50) ;
            this.add(b) ;
        }

    }
}

Answer 1:

面板添加到一个JScrollPane

JScrollPane scrollPane = new JScrollPane(jPanelThird);

滚动窗格添加到CardLayout

this.add(scrollPane, "third card");

请参阅如何使用滚动窗格了解更多详情

更新了工作示例

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestCardLayout {

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

    private int index = 0;

    public TestCardLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                final CardLayout cl = new CardLayout();
                final JPanel cardPane = new JPanel(cl);
                cardPane.add(new JLabel("Hello"), "1");
                cardPane.add(new JScrollPane(createForm()), "2");

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(cardPane);
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                JButton next = new JButton("Next");
                next.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        index++;
                        if (index > 1) {
                            index = 0;
                        }
                        if (index == 0) {
                            cl.show(cardPane, "1");
                        } else { 
                            cl.show(cardPane, "2");
                        }
                    }
                });
                frame.add(next, BorderLayout.SOUTH);
            }

        });
    }

    public JPanel createForm() {
        JPanel form = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        for (int index = 0; index < 100; index++) {
            form.add(new JTextField(10), gbc);
        }
        return form;
    }
}


Answer 2:

所述Container.add(...)方法接受的任何组分。 一个CardLayout并不局限于面板。

面板添加到一个JScrollPane和滚动面板添加到卡上的布局。



Answer 3:

如果你正在使用Eclipse,可以考虑使用的WindowBuilder 。 它是免费的,让你做你需要通过拖放和改变某些性质是什么。



文章来源: Scrollable JPanel in a CardLayout?