java-如何阻止cardlayout .next()和.previous()循环

我在这里有一个程序示例.我有3个面板,并且如果该面板在panel_1中,我想阻止用户按上一个按钮,并且还要在最后一个面板panel_3的末尾停止.如果用户位于面板的开始或面板的末尾,有什么方法喜欢禁用按钮?

package cardlayoutalignment;

  import java.awt.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import javax.swing.*;
  import javax.swing.border.BevelBorder;
  import javax.swing.border.Border;
  import javax.swing.border.EmptyBorder;

public class gridbaglayoutdemo {
JFrame Card = new JFrame();

FlowLayout flow = new FlowLayout(FlowLayout.RIGHT,2,2);
Border etch = BorderFactory.createEtchedBorder(Color.white,Color.gray);
Border margin = new EmptyBorder(10,10,10,10);

public static GridBagLayout grid = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
final static boolean shouldFill = true;

JPanel container = new JPanel();
JPanel divider = new JPanel();
JPanel bodypanel = new JPanel();
final JPanel buttonpanel = new JPanel();
JPanel panel_1 = new JPanel();
    JPanel panel_2 = new JPanel();
    JPanel panel_3 = new JPanel();
CardLayout cl = new CardLayout();

public gridbaglayoutdemo(){

    Card.setVisible(true);
    Card.setSize(605,333);
    Card.setTitle("");
    Card.setResizable(false);

    final Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();      
    int x=(int)((dimension.getWidth() - Card.getWidth())/2);
    int y=(int)((dimension.getHeight() - Card.getHeight())/2);

    Card.setLocation(x, y);
    Card.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


    bodypanel.setLayout(new BorderLayout());

    divider.setLayout(new BorderLayout());

    container.setLayout(cl);
    cl.show(container, "1");

    panel_1.setLayout(grid);

    JLabel label_1 = new JLabel("Enter 1:");
    label_1.setFont(new Font("Arial", Font.PLAIN, 18));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 0;
    c.gridy = 0;
    c.insets = new Insets(10,10,0,0);
    panel_1.add(label_1, c);

    JComboBox box_1 = new JComboBox();
    box_1.setPreferredSize(new Dimension(200,30));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 0;
    c.gridy = 1;
    c.insets = new Insets(10,10,0,0);
    panel_1.add(box_1,c);

    JLabel label = new JLabel("");
    label.setFont(new Font("Arial", Font.PLAIN, 18));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 1;
    c.gridx = 0;
    c.gridy = 2;
    c.insets = new Insets(10,0,0,0);
    panel_1.add(label, c);


    panel_2.setLayout(grid);

    JLabel label_2 = new JLabel("Enter 2:");
    label_2.setFont(new Font("Arial", Font.PLAIN, 18));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 0;
    c.gridy = 0;
    c.insets = new Insets(10,10,0,0);
    panel_2.add(label_2,c);

    JTextField text_2 = new JTextField();
    text_2.setPreferredSize(new Dimension(200,30));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 0;
    c.gridy = 1;
    c.insets = new Insets(10,10,0,0);
    panel_2.add(text_2,c);

    JLabel label_22 = new JLabel("");
    label_22.setFont(new Font("Arial", Font.PLAIN, 18));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 1;
    c.gridx = 0;
    c.gridy = 2;
    c.insets = new Insets(10,0,0,0);
    panel_2.add(label_22, c);


    panel_3.setLayout(grid);

    JLabel label_3 = new JLabel("Enter 3:");
    label_3.setFont(new Font("Arial", Font.PLAIN, 18));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 0;
    c.gridy = 0;
    c.insets = new Insets(10,10,0,0);
    panel_3.add(label_3,c);

    JTextField text_3 = new JTextField();
    text_3.setPreferredSize(new Dimension(200,30));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 0;
    c.gridy = 1;
    c.insets = new Insets(10,10,0,0);
    panel_3.add(text_3,c);

    JLabel label_33 = new JLabel("");
    label_33.setFont(new Font("Arial", Font.PLAIN, 18));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 1;
    c.gridx = 0;
    c.gridy = 2;
    c.insets = new Insets(10,0,0,0);
    panel_3.add(label_33, c);

    buttonpanel.setLayout(new FlowLayout(SwingConstants.RIGHT));
    buttonpanel.setBorder(new EmptyBorder(0,10,0,0));

    JButton btnBack = new JButton("< Back");
    btnBack.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        cl.previous(container);
        buttonpanel.repaint();
        }   
    });
    btnBack.setFont(new Font("Arial", Font.PLAIN, 20));
    btnBack.setFocusable(false);
    btnBack.setFocusTraversalKeysEnabled(false);
    btnBack.setFocusPainted(false);
    btnBack.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    btnBack.setPreferredSize(new Dimension(110, 40));
    btnBack.setBackground(new Color(224,223,227));
    buttonpanel.add(btnBack);

    JButton btnNext = new JButton("Next >");
    btnNext.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            cl.next(container);
            buttonpanel.repaint();
        }   
    });
    btnNext.setFont(new Font("Arial", Font.PLAIN, 20));
    btnNext.setFocusable(false);
    btnNext.setFocusTraversalKeysEnabled(false);
    btnNext.setFocusPainted(false);
    btnNext.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    btnNext.setPreferredSize(new Dimension(110, 40));
    btnNext.setBackground(new Color(224,223,227));
    buttonpanel.add(btnNext);

    final JButton btnCancel = new JButton("Cancel");
    btnCancel.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Window dialog = SwingUtilities.windowForComponent( btnCancel );
            dialog.dispose(); 
        }   
    });
    btnCancel.setFont(new Font("Arial", Font.PLAIN, 20));
    btnCancel.setFocusable(false);
    btnCancel.setFocusTraversalKeysEnabled(false);
    btnCancel.setFocusPainted(false);
    btnCancel.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    btnCancel.setPreferredSize(new Dimension(110, 40));
    btnCancel.setBackground(new Color(224,223,227));
    buttonpanel.add(btnCancel);

    JPanel numberpanel = new JPanel();
    numberpanel.setPreferredSize(new Dimension(221,0));
    numberpanel.setBorder(new EmptyBorder(10,0,0,10));
    numberpanel.setBorder(BorderFactory.createEtchedBorder(Color.white,Color.gray));
    numberpanel.setLayout(flow);

    JButton button_7 = new JButton("7");
    button_7.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }   
    });
    button_7.setFont(new Font("Arial", Font.PLAIN, 30));
    button_7.setFocusable(false);
    button_7.setFocusTraversalKeysEnabled(false);
    button_7.setFocusPainted(false);
    button_7.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    button_7.setPreferredSize(new Dimension(70, 70));
    button_7.setBackground(new Color(224,223,227));
    numberpanel.add(button_7);

    JButton button_8 = new JButton("8");
    button_8.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }   
    });
    button_8.setFont(new Font("Arial", Font.PLAIN, 30));
    button_8.setFocusable(false);
    button_8.setFocusTraversalKeysEnabled(false);
    button_8.setFocusPainted(false);
    button_8.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    button_8.setPreferredSize(new Dimension(70, 70));
    button_8.setBackground(new Color(224,223,227));
    numberpanel.add(button_8);

    JButton button_9 = new JButton("9");
    button_9.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }   
    });
    button_9.setFont(new Font("Arial", Font.PLAIN, 30));
    button_9.setFocusable(false);
    button_9.setFocusTraversalKeysEnabled(false);
    button_9.setFocusPainted(false);
    button_9.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    button_9.setPreferredSize(new Dimension(70, 70));
    button_9.setBackground(new Color(224,223,227));
    numberpanel.add(button_9);



    Card.add(bodypanel);
        bodypanel.add(divider, BorderLayout.WEST);
            divider.add(container, BorderLayout.NORTH);     
                container.add(panel_1, "1");
                container.add(panel_2, "2");
                container.add(panel_3, "3");
                //container.add(panel_4, "4");
                //container.add(p5.panel_5, "5");
                //container.add(p6.panel_6, "6");
            divider.add(buttonpanel, BorderLayout.SOUTH);
        bodypanel.add(numberpanel, BorderLayout.EAST);
  }

   public static void main(String[] args){
     //Use the event dispatch thread for Swing components
     EventQueue.invokeLater(new Runnable()
     {
    @Override
     public void run()
     {
     new gridbaglayoutdemo();         
     }
     });

     }
 }

抱歉,工作代码不够短.我已经修剪了一些.那是我能做的最短的版本.

最佳答案

“Is there any way to like disable the button if the user is at the start of the panel or at end of the panel?”

根据How to Use CardLayoutCardLayout API,没有直接方法可以做到.

但是您可以轻松实现此功能,使int变量与当前卡号保持一致,并再次检查其值ts 0(对于第一张卡)或容器的组件数(对于最后一张卡).例如:

public class GridBagLayoutDemo { // note code convention!

    int currentCard = 0;
    Action backAction, nextAction;
    ...

    public GridBagLayoutDemo() {
        ...
        backAction = new AbstractAction("< Back") {
            @Override
            public void actionPerformed(ActionEvent e) {
                currentCard--;
                GridBagLayoutDemo.this.evaluateActions();
            }
        };

        nextAction = new AbstractAction("Next >") {
            @Override
            public void actionPerformed(ActionEvent e) {
                currentCard++;
                GridBagLayoutDemo.this.evaluateActions();
            }
        };

        JButton btnBack = new JButton(backAction);
        ...
        JButton btnNext = new JButton(nextAction);
        ...
    }

    private void evaluateActions() {
        backAction.setEnabled(currentCard > 0);
        nextAction.setEnabled(currentCard < container.getComponentCount() - 1);
    }
    ...
}

附录

仔细研究CardLayout的实现,默认情况下实现此功能真的很容易(除非我遗漏了一些东西):

public class CardLayout implements LayoutManager2,
                                   Serializable {

    /*
     * This creates a Vector to store associated
     * pairs of components and their names.
     * @see java.util.Vector
     */
    Vector vector = new Vector();

    /*
     * Index of Component currently displayed by CardLayout.
     */
    int currentCard = 0;

    ...

    /* 
     * Hypothetical implementations
     */

    public boolean isDisplayingFirstCard() {
        return currentCard == 0;
    }

    public boolean isDisplayingLastCard() {
        return currentCard == vector.size() - 1;
    }
}

不知道为什么他们没有提供这种有用的功能.

转载注明原文:java-如何阻止cardlayout .next()和.previous()循环 - 代码日志