Java Swing Scroll-able JPanel,框架上有多个面板

时间:2017-02-27 13:27:03

标签: java swing jpanel jscrollpane

我有一个JFrame,它有两个JPanel。首先是标题,然后在它下面是主要内容面板。我想让我的第二个内容面板可以滚动,但即使滚动条出现在面板上,它似乎也没有扩展到它的真实高度。在我的第二个叫做p2的JPanel中,我添加了20个小的JPanel,它们会导致它在高度上扩展,但它不会这样做。以下是我的代码:

public class DisplayItems3{

    public static void main(String[] args) {

        JFrame frame = new JFrame();

        final int FRAME_WIDTH  = 1000;
        final int FRAME_HEIGHT = 1000;

        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        frame.setTitle("Home Library");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());

        //Top Panel
        JPanel p1 = new JPanel();
        p1.setLayout(new FlowLayout());
        p1.setBackground(Color.LIGHT_GRAY);
        p1.setPreferredSize(new Dimension(950, 100));

        JLabel l1 = new JLabel("All Library Items");
        l1.setForeground(Color.BLACK);
        l1.setPreferredSize(new Dimension(900, 50));
        l1.setFont(l1.getFont().deriveFont(30.0f));

        p1.add(l1);

        //Content Panel
        JPanel p2 = new JPanel();
        p2.setLayout(new FlowLayout());
        p2.setBackground(Color.LIGHT_GRAY);
        p2.setPreferredSize(new Dimension(950, 800));
        p2.setAutoscrolls(true);

        JScrollPane scrollPane = new JScrollPane(p2);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setBounds(0, 0, 950, 800);

        JPanel contentPane = new JPanel(null);
        contentPane.setPreferredSize(new Dimension(950, 800));
        contentPane.add(scrollPane);

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

            JPanel sp1 = new JPanel();
            sp1.setLayout(new FlowLayout());
            sp1.setBackground(Color.WHITE);
            sp1.setPreferredSize(new Dimension(900, 180));

            p2.add(sp1);    

        }


        //contentPane.add(p2);
        frame.add(p1);
        //frame.add(p2);
        //frame.setContentPane(contentPane);
        frame.add(contentPane);

        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);

    }

}

enter image description here

使用JList更新

它不起作用!

public class JListTest {

    public static void main(String[] args) {

        JFrame frame = new JFrame();

        final int FRAME_WIDTH  = 1000;
        final int FRAME_HEIGHT = 1000;

        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        frame.setTitle("Home Library");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());

        //Top Panel
        JPanel p1 = new JPanel();
        p1.setLayout(new FlowLayout());
        p1.setBackground(Color.LIGHT_GRAY);
        p1.setPreferredSize(new Dimension(950, 100));

        JLabel l1 = new JLabel("All Library Items");
        l1.setForeground(Color.BLACK);
        l1.setPreferredSize(new Dimension(900, 50));
        l1.setFont(l1.getFont().deriveFont(30.0f));

        p1.add(l1);

        JList ll1 = new JList();
        ll1.setLayout(new FlowLayout());
        ll1.setBackground(Color.LIGHT_GRAY);
        ll1.setPreferredSize(new Dimension(950, 800));
        ll1.setAutoscrolls(true);

        JScrollPane scrollPane = new JScrollPane(ll1);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setBounds(0, 0, 950, 800);

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

            JPanel sp1 = new JPanel();
            sp1.setLayout(new FlowLayout());
            sp1.setBackground(Color.WHITE);
            sp1.setPreferredSize(new Dimension(900, 180));

            ll1.add(sp1);    

        }


        //contentPane.add(p2);
        frame.add(p1);
        frame.add(scrollPane);
        //frame.setContentPane(contentPane);
        //frame.add(contentPane);

        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);

    }

}

解决问题:工作代码

public class scrollTest2 {

    public static void main(String[] args)
    {
        JFrame frame = new JFrame();

        final int FRAME_WIDTH  = 1000;
        final int FRAME_HEIGHT = 1000;

        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        frame.setTitle("Home Library");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());

        JLabel m1 = new JLabel("safsd");

        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setAutoscrolls(true);
        frame.add(panel,BorderLayout.NORTH);

        JScrollPane scrollPane = new JScrollPane(panel);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setBounds(50, 30, 800, 800);

        JPanel contentPane = new JPanel(null);
        contentPane.setPreferredSize(new Dimension(900, 900));
        contentPane.add(scrollPane);

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

            JPanel sp1 = new JPanel();
            sp1.setLayout(new FlowLayout());
            sp1.setBackground(Color.WHITE);
            sp1.setPreferredSize(new Dimension(900, 180));

            JPanel ssp1 = new JPanel();
            ssp1.setLayout(new FlowLayout());
            ssp1.setBackground(Color.WHITE);
            ssp1.setPreferredSize(new Dimension(500, 170));

            JPanel ssp2 = new JPanel();
            ssp2.setLayout(new FlowLayout());
            ssp2.setBackground(Color.WHITE);
            ssp2.setPreferredSize(new Dimension(350, 170));

            JLabel l3 = new JLabel("Title: ");
            l3.setForeground(Color.BLACK);
            l3.setPreferredSize(new Dimension(100, 20));
            JTextField t1 = new JTextField("Electronic Basics");
            t1.setPreferredSize(new Dimension(320, 20));

            JLabel l4 = new JLabel("Type: ");
            l4.setForeground(Color.BLACK);
            l4.setPreferredSize(new Dimension(100, 20));
            JTextField t2 = new JTextField("Book");
            t2.setPreferredSize(new Dimension(320, 20));

            JLabel l5 = new JLabel("Authors: ");
            l5.setForeground(Color.BLACK);
            l5.setPreferredSize(new Dimension(100, 20));
            JTextField t3 = new JTextField("Bob the Builder");
            t3.setPreferredSize(new Dimension(320, 20));

            JLabel l6 = new JLabel("Publisher: ");
            l6.setForeground(Color.BLACK);
            l6.setPreferredSize(new Dimension(100, 20));
            JTextField t4 = new JTextField("ABC Company");
            t4.setPreferredSize(new Dimension(320, 20));

            JLabel l7 = new JLabel("Location: ");
            l7.setForeground(Color.BLACK);
            l7.setPreferredSize(new Dimension(100, 20));
            JTextField t5 = new JTextField("Shelf 1 Row 3");
            t5.setPreferredSize(new Dimension(320, 20));

            JLabel l8 = new JLabel("Status: ");
            l8.setForeground(Color.BLACK);
            l8.setPreferredSize(new Dimension(100, 20));
            JTextField t6 = new JTextField("Available");
            t6.setPreferredSize(new Dimension(320, 20));

            JButton btnLoanHistory = new JButton("Loan History");
            btnLoanHistory.setPreferredSize(new Dimension(300, 20));
            JButton btnLoanItem = new JButton("Loan Item");
            btnLoanItem.setPreferredSize(new Dimension(300, 20));
            JButton btnProcessReturn = new JButton("Process Return");
            btnProcessReturn.setPreferredSize(new Dimension(300, 20));

            ssp1.add(l3);
            ssp1.add(t1);
            ssp1.add(l4);
            ssp1.add(t2);
            ssp1.add(l5);
            ssp1.add(t3);
            ssp1.add(l6);
            ssp1.add(t4);
            ssp1.add(l7);
            ssp1.add(t5);
            ssp1.add(l8);
            ssp1.add(t6);

            ssp2.add(btnLoanHistory);
            ssp2.add(btnLoanItem);
            ssp2.add(btnProcessReturn);

            sp1.add(ssp1);
            sp1.add(ssp2);
            panel.add(sp1); 

        }

        frame.add(m1, BorderLayout.NORTH);
        frame.add(contentPane, BorderLayout.CENTER);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }

}

1 个答案:

答案 0 :(得分:1)

将p1添加到contentPane中,而不是添加到框架中 上面的例子很好。 祝你有愉快的一天。

var myDropdown = document.getElementById('myDropdown');
var links = myDropdown.getElementsByTagName('a'); // get all links inside #myDropdown

function changeSRC(e) {
  var link = e.target.href;    // get href of clicked link
  var code = document.getElementsByTagName('code')[0];
  code.innerHTML = '&lt;iframe&gt;src=&quot;'+link+'&quot; scrolling=&quot;no&quot; seamless=&quot;seamless&quot;&gt;&lt;/iframe&gt;';
}

for( var i=0; i<links.length; i++) {
  links[i].addEventListener('click', changeSRC);    // call changeSRC function when any link is clicked
}