Swing逐页转换(MultiPages Widget)

时间:2013-01-01 14:59:55

标签: java swing layout layout-manager

我正在寻找一种策略,允许我创建一个Java Swing应用程序并使用按钮来更改JFrame上显示的组件。

我正在尝试使用JButtonActionListener来做这件事,但没有太多运气。

我无法使用JDialogCardLayout,因为我一次只能看到一个页面供用户按顺序进行交互。

2 个答案:

答案 0 :(得分:6)

这是一个完成工作的课程:

enter image description here

public class MultiPages extends JPanel implements ActionListener {

    MultiPages() {
        super(new BorderLayout());
        add(leftButton, BorderLayout.WEST);
        add(pages, BorderLayout.CENTER);
        add(rightButton, BorderLayout.EAST);
        leftButton.addActionListener(this);
        rightButton.addActionListener(this);
        animation.setInitialDelay(10);
        animation.setRepeats(false);
    }

    public void addPage(JComponent page) {
        page.setLocation(0, 0);
        pages.add(page);
        pages.setComponentZOrder(page, pages.getComponentCount() - 1);
        SwingUtilities.invokeLater(new Runnable() {
            @Override public void run() { doLayout(); }});
    }

    @Override
    public void doLayout() {
        Dimension size = getParent().getSize();
        size.width -= leftButton.getWidth() + rightButton.getWidth();
        pages.setSize(size);
        for (Component page : pages.getComponents()) {
            page.setSize(size);
        }
        super.doLayout();
    }

    private void scrollLeft() {
        direction = true;
        Component next = pages.getComponents()[pages.getComponentCount() - 1];
        pages.setComponentZOrder(next, 1);
        animation.start();
        leftButton.setEnabled(false);
        rightButton.setEnabled(false);
    }

    private void scrollRight() {
        direction = false;
        animation.start();
        leftButton.setEnabled(false);
        rightButton.setEnabled(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object src = e.getSource();
        if (src == leftButton) {
            scrollLeft();
        } else if (src == rightButton) {
            scrollRight();
        } else if (src == animation) {
            Component onTop = pages.getComponents()[0];
            onTop.setLocation(onTop.getX() + (direction ? +4 : -4), 0);
            if (Math.abs(onTop.getX()) < onTop.getWidth()) {
                animation.start();
            } else {
                if (direction) {
                    pages.setComponentZOrder(onTop, 1);
                } else {
                    pages.setComponentZOrder(onTop, pages.getComponentCount() - 1);
                }
                onTop.setLocation(0, 0);
                leftButton.setEnabled(true);
                rightButton.setEnabled(true);
            }
        }
    }
    private final JPanel pages = new JPanel(null);
    private final JButton leftButton = new JButton("<<");
    private final JButton rightButton = new JButton(">>");
    private final Timer animation = new Timer(0, this);
    private boolean direction;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Multi pages demo App");

                frame.setLayout(new BorderLayout());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                MultiPages multiPage = new MultiPages();
                multiPage.setPreferredSize(new Dimension(250, 100));

                JPanel page;
                page = new JPanel();
                page.setBackground(Color.CYAN);
                page.setLayout(new BorderLayout());
                page.add(new JLabel("First page", SwingConstants.CENTER), BorderLayout.CENTER);
                multiPage.addPage(page);
                page = new JPanel();
                page.setBackground(Color.YELLOW);
                page.setLayout(new BorderLayout());
                page.add(new JLabel("Second page", SwingConstants.CENTER), BorderLayout.CENTER);
                multiPage.addPage(page);
                page = new JPanel();
                page.setBackground(Color.GREEN);
                page.setLayout(new BorderLayout());
                page.add(new JLabel("Third page", SwingConstants.CENTER), BorderLayout.CENTER);
                multiPage.addPage(page);
                page = new JPanel();
                page.setBackground(Color.RED);
                page.setLayout(new BorderLayout());
                page.add(new JLabel("Fourth page", SwingConstants.CENTER), BorderLayout.CENTER);
                multiPage.addPage(page);

                frame.add(multiPage, BorderLayout.CENTER);

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

对于CardLayout的专业人员来说,这里是没有动画的相同事情的实现,因为CardLayout没有公开它的内部地图。

你可以看到它更简单,代码更少,错误更少......

public class MultiPagesCardLayout extends JPanel implements ActionListener {

   MultiPagesCardLayout() {
      super( new BorderLayout());
      add( _leftBtn , BorderLayout.WEST   );
      add( _pages   , BorderLayout.CENTER );
      add( _rightBtn, BorderLayout.EAST   );
      _leftBtn .addActionListener( this );
      _rightBtn.addActionListener( this );
      _animation.setInitialDelay( 10 );
      _animation.setRepeats( false );
   }

   public void addPage( Component page ) {
      _pages.add( page, "" + ( _pages.getComponentCount() - 1 ));
   }

   @Override
   public void actionPerformed( ActionEvent e ) {
      Object src = e.getSource();
      if( src == _leftBtn ) {
         ((CardLayout)_pages.getLayout()).previous( _pages );
      }
      else if( src == _rightBtn ) {
         ((CardLayout)_pages.getLayout()).next( _pages );
      }
   }

   private final JPanel  _pages     = new JPanel( new CardLayout());
   private final JButton _leftBtn   = new JButton( "<<" );
   private final JButton _rightBtn  = new JButton( ">>" );
   private final Timer   _animation = new Timer( 0, this );

   public static void main( String[] args ) {
      SwingUtilities.invokeLater( new Runnable() {
         @Override public void run() {
            JFrame frame = new JFrame( "Multi pages demo App" );
            frame.setLayout( new BorderLayout());
            frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            MultiPagesCardLayout multiPage = new MultiPagesCardLayout();
            multiPage.setPreferredSize( new Dimension( 250, 100 ));
            JPanel page;
            page = new JPanel();
            page.setBackground( Color.CYAN );
            page.setLayout( new BorderLayout());
            page.add( new JLabel( "First page", SwingConstants.CENTER ), BorderLayout.CENTER );
            multiPage.addPage( page );
            page = new JPanel();
            page.setBackground( Color.YELLOW );
            page.setLayout( new BorderLayout());
            page.add( new JLabel( "Second page", SwingConstants.CENTER ), BorderLayout.CENTER );
            multiPage.addPage( page );
            page = new JPanel();
            page.setBackground( Color.GREEN );
            page.setLayout( new BorderLayout());
            page.add( new JLabel( "Third page", SwingConstants.CENTER ), BorderLayout.CENTER );
            multiPage.addPage( page );
            page = new JPanel();
            page.setBackground( Color.RED );
            page.setLayout( new BorderLayout());
            page.add( new JLabel( "Fourth page", SwingConstants.CENTER ), BorderLayout.CENTER );
            multiPage.addPage( page );
            frame.add( multiPage, BorderLayout.CENTER );
            frame.pack();
            frame.setLocationRelativeTo( null );
            frame.setVisible( true );
         }});
   }
}

答案 1 :(得分:1)

这是接近它的一种方法:

关键是Jpanels。将它们视为主jframe内的迷你jframe。以下是这些面板的一些信息:

http://docs.oracle.com/javase/6/docs/api/javax/swing/JPanel.html

单击按钮时,每个按钮基本上都会切换显示的面板。将编码放在动作监听器中。

此方法优于使用多个JFrame。