日期:2014-05-20  浏览次数:20660 次

关于控制JPanel中的按钮进行滑动
问题是这样的,首先在JFrame中有一个JPanel,JFrame大小是500*500,包含了一个JPanel,在JPanel中有十二个按钮,界面只能显示六个按钮,还有六个没有显示出来,要求的效果就是,在下面放入两个按钮,一个left,一个right,可以通过这两个按钮进行一个切换,类似翻页效果(就是一次六个的切换),请各位高手帮帮忙,或者有什么好的方法推荐一下,有Demo参考最好了。

------解决方案--------------------
Java code
final JPanel left = new JPanel(new GridLayout(3,2,5,5));
left.add(new JButton("1"));
left.add(new JButton("2"));
left.add(new JButton("3"));
left.add(new JButton("4"));
left.add(new JButton("5"));
left.add(new JButton("6"));
final JPanel right = new JPanel(new GridLayout(3,2,5,5));
right.add(new JButton("7"));
right.add(new JButton("8"));
right.add(new JButton("9"));
right.add(new JButton("10"));
right.add(new JButton("11"));
right.add(new JButton("12"));

final CardLayout layout = new CardLayout();
final JPanel pages = new JPanel(layout);
pages.add(left, "left");
pages.add(right, "right");

final JButton showLeft = new JButton("Left");
showLeft.addActionListener(new ActionListener(){
    @Override public void actionPerformed(ActionEvent e){
        layout.show(pages,"left");
    }
});
final JButton showRight = new JButton("Right");
showRight.addActionListener(new ActionListener(){
    @Override public void actionPerformed(ActionEvent e){
        layout.show(pages,"right");
    }
});

final JPanel buttonPanel = new JPanel();
buttonPanel.add(showLeft);
buttonPanel.add(showRight);

final JPanel main = new JPanel(new BorderLayout());
main.add(pages,BorderLayout.CENTER);
main.add(buttonPanel,BorderLayout.SOUTH);