如何阻止 cardlayout .next() 和 .previous() 循环 [英] How do I stop the cardlayout .next() and .previous() from looping

查看:37
本文介绍了如何阻止 cardlayout .next() 和 .previous() 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有一个程序示例.我有 3 个面板,如果面板在 panel_1 中,我想阻止用户按下上一个按钮,并且在最后一个面板 panel_3 的末尾停止.如果用户在面板的开头或结尾,有什么办法可以禁用按钮吗?

封装cardlayoutalignment;导入 java.awt.*;导入 java.awt.event.ActionEvent;导入 java.awt.event.ActionListener;导入 javax.swing.*;导入 javax.swing.border.BevelBorder;导入 javax.swing.border.Border;导入 javax.swing.border.EmptyBorder;公共类 gridbaglayoutdemo {JFrame Card = new JFrame();FlowLayout flow = new FlowLayout(FlowLayout.RIGHT,2,2);边框蚀刻 = BorderFactory.createEtchedBorder(Color.white,Color.gray);边框边距 = new EmptyBorder(10,10,10,10);公共静态 GridBagLayout 网格 = 新 GridBagLayout();GridBagConstraints c = new GridBagConstraints();最终静态布尔值 shouldFill = true;JPanel 容器 = new JPanel();JPanel 分隔符 = new JPanel();JPanel bodypanel = new JPanel();最终 JPanel 按钮面板 = 新 JPanel();JPanel panel_1 = new JPanel();JPanel panel_2 = new JPanel();JPanel panel_3 = new JPanel();CardLayout cl = new CardLayout();公共网格包布局演示(){Card.setVisible(true);Card.setSize(605,333);Card.setTitle("");Card.setResizable(false);最终工具包工具包 = Toolkit.getDefaultToolkit();维度维度 = 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(容器,1");panel_1.setLayout(grid);JLabel label_1 = new JLabel("请输入 1:");label_1.setFont(new Font("Arial", Font.PLAIN, 18));c.fill = GridBagConstraints.HORIZONTAL;c.权重x = 0.5;c.重量 = 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.权重x = 0.5;c.重量 = 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.权重x = 0.5;c.重量 = 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("请输入 2:");label_2.setFont(new Font("Arial", Font.PLAIN, 18));c.fill = GridBagConstraints.HORIZONTAL;c.权重x = 0.5;c.重量 = 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.权重x = 0.5;c.重量 = 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.权重x = 0.5;c.重量 = 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("请输入 3:");label_3.setFont(new Font("Arial", Font.PLAIN, 18));c.fill = GridBagConstraints.HORIZONTAL;c.权重x = 0.5;c.重量 = 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.权重x = 0.5;c.重量 = 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.权重x = 0.5;c.重量 = 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(容器);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(新颜色(224,223,227));buttonpanel.add(btnBack);JButton btnNext = new JButton("Next >");btnNext.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {cl.next(容器);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(新颜色(224,223,227));buttonpanel.add(btnNext);final JButton btnCancel = new JButton("Cancel");btnCancel.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {窗口对话框 = SwingUtilities.windowForComponent( btnCancel );对话框处理();}});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(新颜色(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(新颜色(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(新颜色(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(新颜色(224,223,227));numberpanel.add(button_9);Card.add(bodypanel);bodypanel.add(divider, BorderLayout.WEST);分隔线.添加(容器,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);}公共静态无效主(字符串 [] args){//为Swing组件使用事件调度线程EventQueue.invokeLater(new Runnable(){@覆盖公共无效运行(){新的 gridbaglayoutdemo();}});}}

抱歉没有这么短的工作代码.我已经修剪了一些.这是我能做的最短的版本.

解决方案

如果用户在面板的开头或结尾,有没有办法禁用按钮?"

根据如何使用 CardLayoutCardLayout API 没有直接的方法可以做到这一点.>

但是你可以很容易地实现这个特性,保持一个带有当前卡号的 int 变量,并检查它的值是否为 0(对于第一张卡)或容器的组件计数(对于最后一张卡).例如:

public class GridBagLayoutDemo {//注意代码约定!int currentCard = 0;动作 backAction, nextAction;...公共 GridBagLayoutDemo() {...backAction = new AbstractAction("< Back") {@覆盖public void actionPerformed(ActionEvent e) {当前卡--;GridBagLayoutDemo.this.evaluateActions();}};nextAction = new AbstractAction("Next >") {@覆盖public void actionPerformed(ActionEvent e) {当前卡++;GridBagLayoutDemo.this.evaluateActions();}};JButton btnBack = new JButton(backAction);...JButton btnNext = new JButton(nextAction);...}私人无效评估行动(){backAction.setEnabled(currentCard > 0);nextAction.setEnabled(currentCard < container.getComponentCount() - 1);}...}

<小时>

附录

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

公共类 CardLayout 实现了 LayoutManager2,可序列化{/** 这将创建一个 Vector 来存储关联的* 成对的组件及其名称.* @see java.util.Vector*/向量向量 = new Vector();/** CardLayout 当前显示的组件索引.*/int currentCard = 0;.../** 假设的实现*/公共布尔 isDisplayingFirstCard() {返回当前卡 == 0;}公共布尔 isDisplayingLastCard() {返回 currentCard == vector.size() - 1;}}

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

I have here a sample of a program here. I have 3 panels and I want to stop the user in pressing the previous button if the panel is in panel_1 and also stop at the end of panel_3 which is that last panel. 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?

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();         
     }
     });

     }
 }

Sorry for the not so short working code. I already trimmed some of it. That's the shortest ver I can do.

解决方案

"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?"

According to How to Use CardLayout and CardLayout API there's no direct way to do it.

But you can implement this feature easily keeping an int variable with current card number and checking its value againts 0 (for first card) or container's component count (for last card). For instance:

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);
    }
    ...
}


Addendum

Looking closer at CardLayout implementation, it would be really easy to have this feature implemented by default (unless I'm missing something):

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

Don't know why they didn't provide such useful feature.

这篇关于如何阻止 cardlayout .next() 和 .previous() 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆