2014-04-01 43 views
1

我一直在寻找一种将垂直滚动条添加到JPanel的方法,该方法又被添加到CardLayout面板。我查阅了这里所有关于实现可滚动的JPanel的帖子,但我无法弄清楚如何使用这个特定的CardLayout来实现它。 Oracle没有给出我可以使用的例子。CardLayout中的可滚动JPanel?

也许我没有使用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) ; 
     } 

    } 
} 

回答

2

面板添加到JScrollPane中

JScrollPane scrollPane = new JScrollPane(jPanelThird); 

滚动窗格添加到CardLayout

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

更多细节

与工作示例更新见How to use scroll panes

Card1Card2

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; 
    } 
} 
+0

谢谢我正在尝试这个选项,我也看到了之前的链接无济于事。 – user9349193413

+0

它似乎没有工作。 – user9349193413

+0

对我来说工作正常,请记住,直到你将组件放大,因此它的尺寸大于可视空间,滚动条将不会显示 – MadProgrammer

2

Container.add(...)方法接受任何组件。 CardLayout不限于面板。

将面板添加到JScrollPane并将滚动窗格添加到卡布局。

+0

我现在试试吧,谢谢 – user9349193413

+0

@twimB,'我也实现了一些按钮,以防你想运行它。' - 不要使用空布局。只有当您添加到滚动窗格的组件的首选大小大于滚动窗格的大小时,才会显示滚动条。当你使用空布局时,面板的首选大小为0. – camickr

0

如果你使用Eclipse,可以考虑使用WindowBuilder。它是免费的,并允许您通过拖放和更改某些属性来执行所需的操作。