如何从循环中停止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(""); 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(); } }); } } 

很抱歉没有那么短的工作代码。 我已经修剪了一些。 那是我能做的最短的。

“如果用户位于面板的开头或面板的末端,是否有任何方法可以禁用按钮?”

根据如何使用CardLayout和CardLayout API ,没有直接的方法来做到这一点。

但是,您可以轻松实现此function,并使用当前卡号保留一个int变量,并再次检查其值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实现,默认情况下实现这个function真的很容易(除非我遗漏了一些东西):

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

不知道为什么他们没有提供这样有用的function。

查看Card Layout Actions以获得CardLayout的简单扩展。

它提供了“下一步”“上一步”操作,您可以使用这些操作创建按钮或菜单项,当您在卡片的结尾处开始时,操作将自动禁用。