简体   繁体   English

CardLayout带有更改卡片的按钮

[英]CardLayout with buttons that change the cards

I am trying to write a short program that has a home page with title and 4 buttons, 3 of which will leave the home screen and go to a new page where you can input information accordingly. 我正在尝试编写一个简短的程序,该程序具有一个带有标题和4个按钮的主页,其中三个按钮将退出主屏幕并转到一个新页面,您可以在其中输入相应的信息。 I started by using a drag and drop editor but have found through forums i should be using cardlayout instead i have looked over the oracle tutorial but i want to use buttons to change cards not a combobox. 我从使用拖放编辑器开始,但是通过论坛发现我应该使用cardlayout,而我查看了oracle教程,但是我想使用按钮来更改卡而不是组合框。 Is that possible. 那可能吗。

here is the code from the tutorial what part would i need to change to make a button the action button that changes screens. 这是教程中的代码,我需要更改哪个部分才能使按钮成为更改屏幕的操作按钮。

thanks, 谢谢,

package layout;

/*
 * CardLayoutDemo.java
 *
 */
import java.awt.*;
  import java.awt.event.*;
import javax.swing.*;

public class CardLayoutDemo implements ItemListener {
    JPanel cards; //a panel that uses CardLayout
    final static String BUTTONPANEL = "Card with JButtons";
    final static String TEXTPANEL = "Card with JTextField";

public void addComponentToPane(Container pane) {
    //Put the JComboBox in a JPanel to get a nicer look.
    JPanel comboBoxPane = new JPanel(); //use FlowLayout
    String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL };
    JComboBox cb = new JComboBox(comboBoxItems);
    cb.setEditable(false);
    cb.addItemListener(this);
    comboBoxPane.add(cb);

    //Create the "cards".
    JPanel card1 = new JPanel();
    card1.add(new JButton("Button 1"));
    card1.add(new JButton("Button 2"));
    card1.add(new JButton("Button 3"));

    JPanel card2 = new JPanel();
    card2.add(new JTextField("TextField", 20));

    //Create the panel that contains the "cards".
    cards = new JPanel(new CardLayout());
    cards.add(card1, BUTTONPANEL);
    cards.add(card2, TEXTPANEL);

    pane.add(comboBoxPane, BorderLayout.PAGE_START);
    pane.add(cards, BorderLayout.CENTER);
}

public void itemStateChanged(ItemEvent evt) {
    CardLayout cl = (CardLayout)(cards.getLayout());
    cl.show(cards, (String)evt.getItem());
}

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event dispatch thread.
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("CardLayoutDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    CardLayoutDemo demo = new CardLayoutDemo();
    demo.addComponentToPane(frame.getContentPane());

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    /* Use an appropriate Look and Feel */
    try {
        //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    } catch (UnsupportedLookAndFeelException ex) {
        ex.printStackTrace();
    } catch (IllegalAccessException ex) {
        ex.printStackTrace();
    } catch (InstantiationException ex) {
        ex.printStackTrace();
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
    }
    /* Turn off metal's use of bold fonts */
    UIManager.put("swing.boldMetal", Boolean.FALSE);

    //Schedule a job for the event dispatch thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}
}

but i want to use buttons to change cards not a combobox. 但我想使用按钮来更改卡而不是组合框。 Is that possible. 那可能吗。

The tutorial uses an ItemListener for the combo box. 本教程使用ItemListener作为组合框。

If you want to use buttons, then you would use an ActionListener. 如果要使用按钮,则可以使用ActionListener。

The code in the ActionListener would basically be the same. ActionListener的代码基本上是相同的。 That is you need to identify the card you want to change to. 那就是您需要识别要更换的卡。

cl.show(cards, BUTTONPANEL); // for example

Although since the button will probably display the name of the panel you want to switch to you could do something like: 尽管由于该按钮可能会显示面板名称,所以您可以执行以下操作:

cl.show(cards, event.getActionCommand());

The action command of the ActionEvent which is generated when you click on the button contains the text that is displayed on the button. 单击按钮时生成的ActionEvent的action命令包含按钮上显示的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM