当前位置: 首页 > 面试题库 >

Java将其他类的JPanels添加到cardLayout

龚永新
2023-03-14
问题内容

我在3个单独的类中有3个窗口,我想使用cardLayout,以便当你单击next按钮时,将出现下一个窗口。如何将包含不同元素的JPanels添加到一个cardLayout?这是第一个窗口:(尽管唯一的区别是背景-但它代表了我实际得到它的想法)

public class Window1 extends JPanel implements ActionListener {

static CardLayout cardLayout = new CardLayout();

public Window1() {
   init();
}

private void init() {
    JPanel jp = new JPanel(new BorderLayout());
    JPanel jp2 = new Window2();
    //JPanel jp3 = new Window3();
    JLabel textLabel = new JLabel("Window1");
    jp.setBackground(Color.GREEN);
    jp.add(textLabel, BorderLayout.CENTER);
    JButton nextButton = new JButton("NEXT");
    nextButton.setActionCommand("next");
    nextButton.addActionListener(this);
    jp.add(nextButton, BorderLayout.EAST);
    setLayout(cardLayout);
    add(jp, "string");
    add(jp2, "string");
    //add(jp3, "string");
}

public void actionPerformed(ActionEvent e) {            
    if (e.getActionCommand().equalsIgnoreCase("next")) {
    // go to the next window
        cardLayout.next(this);
    }
}

private static void createAndShowGUI() {        
      JFrame frame = new JFrame("test");
      frame.getContentPane().setLayout(Window1.cardLayout);
      frame.getContentPane().add(new Window1(), "Center");
      frame.getContentPane().add(new Window2(), "Center");
      frame.getContentPane().add(new Window3(), "Center");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(550, 450);
      frame.setVisible(true); 
}


public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }           
    });
}
}

第二个窗口:

public class Window2 extends JPanel implements ActionListener {

//static CardLayout cardLayout = new CardLayout();

public Window2() {
   init();
}

private void init() {
    setLayout(new BorderLayout());
    JLabel textLabel = new JLabel("Window2");
    setBackground(Color.RED);
    add(textLabel, BorderLayout.CENTER);
    JButton nextButton = new JButton("NEXT");
    nextButton.setActionCommand("next");
    nextButton.addActionListener(this);
    add(nextButton, BorderLayout.EAST);
    //setLayout(cardLayout);
    //JPanel jp3 = new Window3();
    //add(jp3, "string");
}

public void actionPerformed(ActionEvent e) {            
    if (e.getActionCommand().equalsIgnoreCase("next")) {
    // go to the next window??
        System.out.println("window2");
        Window1.cardLayout.next(this);
    }
}

}

最后一个:

public class Window3 extends JPanel implements ActionListener {

public Window3() {
   init();
}

private void init() {
    setLayout(new BorderLayout());
    JLabel textLabel = new JLabel("Window3");
    setBackground(Color.BLUE);
    add(textLabel, BorderLayout.CENTER);
    JButton nextButton = new JButton("NEXT");
    nextButton.setActionCommand("next");
    nextButton.addActionListener(this);
    add(nextButton, BorderLayout.EAST);
}

public void actionPerformed(ActionEvent e) {            
    if (e.getActionCommand().equalsIgnoreCase("next")) {
    // go to the next window
    //  Window1.cardLayout.next(this);
    }
}

}

问题答案:

我做了一个小程序,希望程序中写的注释可以指导你了解如何使用CardLayout。

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

/* Here we are first declaring our class that will act as the
 * base for other panels or in other terms the base for CardLayout.
 */

public class CardLayoutTest
{
    private static final String CARD_JBUTTON =  "Card JButton";
    private static final String CARD_JTEXTFIELD = "Card JTextField";    
    private static final String CARD_JRADIOBUTTON = "Card JRadioButton";

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("Card Layout Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        // This JPanel is the base for CardLayout for other JPanels.
        final JPanel contentPane = new JPanel();
        contentPane.setLayout(new CardLayout(20, 20));

        /* Here we be making objects of the Window Series classes
         * so that, each one of them can be added to the JPanel 
         * having CardLayout. 
         */
        Window1 win1 = new Window1();
        contentPane.add(win1, CARD_JBUTTON);
        Window2 win2 = new Window2();
        contentPane.add(win2, CARD_JTEXTFIELD);
        Window3 win3 = new Window3();
        contentPane.add(win3, CARD_JRADIOBUTTON);

        /* We need two JButtons to go to the next Card
         * or come back to the previous Card, as and when
         * desired by the User.
         */
        JPanel buttonPanel = new JPanel(); 
        final JButton previousButton = new JButton("PREVIOUS");
        previousButton.setBackground(Color.BLACK);
        previousButton.setForeground(Color.WHITE);
        final JButton nextButton = new JButton("NEXT");
        nextButton.setBackground(Color.RED);
        nextButton.setForeground(Color.WHITE);
        buttonPanel.add(previousButton);
        buttonPanel.add(nextButton);

        /* Adding the ActionListeners to the JButton,
         * so that the user can see the next Card or
         * come back to the previous Card, as desired.
         */
        previousButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                CardLayout cardLayout = (CardLayout) contentPane.getLayout();
                cardLayout.previous(contentPane);
            }
        });
        nextButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                CardLayout cardLayout = (CardLayout) contentPane.getLayout();
                cardLayout.next(contentPane);   
            }
        });

        // Adding the contentPane (JPanel) and buttonPanel to JFrame.
        frame.add(contentPane, BorderLayout.CENTER);
        frame.add(buttonPanel, BorderLayout.PAGE_END);

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

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
} 

class Window1 extends JPanel
{
    /*
     * Here this is our first Card of CardLayout, which will
     * be added to the contentPane object of JPanel, which
     * has the LayoutManager set to CardLayout.
     * This card consists of Two JButtons.
     */  
    private ActionListener action;

    public Window1() 
    {
        init();
    }

    private void init() 
    {
        final JButton clickButton = new JButton("CLICK ME");
        final JButton dontClickButton = new JButton("DON\'T CLICK ME");     

        action = new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                if (ae.getSource() == clickButton)
                {
                    JOptionPane.showMessageDialog(null, "Hello there dude!"
                                                , "Right Button", JOptionPane.INFORMATION_MESSAGE);
                }
                else if (ae.getSource() == dontClickButton)
                {
                    JOptionPane.showMessageDialog(null, "I told you not to click me!"
                                                        , "Wrong Button", JOptionPane.PLAIN_MESSAGE);
                }
            }
        };

        clickButton.addActionListener(action);
        dontClickButton.addActionListener(action);

        add(clickButton);
        add(dontClickButton);
    }
}

class Window2 extends JPanel implements ActionListener 
{
    /*
     * Here this is our second Card of CardLayout, which will
     * be added to the contentPane object of JPanel, which
     * has the LayoutManager set to CardLayout.
     * This card consists of a JLabel and a  JTextField
     * with GridLayout.
     */  

    private JTextField textField;

    public Window2() 
    {
        init();
    }

    private void init() 
    {
        setLayout(new GridLayout(1, 2));
        JLabel userLabel = new JLabel("Your Name : ");
        textField = new JTextField();
        textField.addActionListener(this);

        add(userLabel);
        add(textField);
    }

    public void actionPerformed(ActionEvent e) 
    {            
        if (textField.getDocument().getLength() > 0)
            JOptionPane.showMessageDialog(null, "Your Name is : " + textField.getText()
                                                                            , "User\'s Name : ", JOptionPane.QUESTION_MESSAGE);
    }
}

class Window3 extends JPanel
{
    /*
     * Here this is our third Card of CardLayout, which will
     * be added to the contentPane object of JPanel, which
     * has the LayoutManager set to CardLayout.
     * This card consists of Two JLabels and two JCheckBox
     * with GridLayout.
     */  
    private ActionListener state;

    public Window3()
    {
        init();
    }

    public void init()
    {
        setLayout(new GridLayout(2, 2));
        JLabel maleLabel = new JLabel("MALE", JLabel.CENTER);
        final JCheckBox maleBox = new JCheckBox();
        JLabel femaleLabel = new JLabel("FEMALE", JLabel.CENTER);
        final JCheckBox femaleBox = new JCheckBox();

        state = new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                if (maleBox == (JCheckBox) ae.getSource())
                {
                    femaleBox.setSelected(false);
                    JOptionPane.showMessageDialog(null, "Congrats you are a Male"
                                                , "Gender : ", JOptionPane.INFORMATION_MESSAGE);                            
                }
                else if (femaleBox == (JCheckBox) ae.getSource())
                {
                    maleBox.setSelected(false);
                    JOptionPane.showMessageDialog(null, "Congrats you are a Female"
                                            , "Gender : ", JOptionPane.INFORMATION_MESSAGE);                        
                }
            }
        };

        maleBox.addActionListener(state);
        femaleBox.addActionListener(state);
        add(maleLabel);
        add(maleBox);
        add(femaleLabel);
        add(femaleBox);
    }
}


 类似资料:
  • 我正在尝试将一个字符串从topicRNG加载到changeXML。我以前在类之间加载过变量,但现在无法运行 首先,我有我的代码,我试图加载它。包装试验; 然后是我试图加载的代码, 当我从GenreDefiner加载int时,我把它设置成这样, 我尝试过“将公共静态字符串放入topicFinal”,但它给了我一个错误,当我将它放在“公共静态void main(String args[]){}”之外时

  • 我正在学习JSwing,我发现了GridBagLayout。 我试图创建一个简单的计算器,我添加了多个JPanel设置每个首选大小,但当我调整窗框大小时,面板也不会调整大小。然后我发现了Gridbag的布局。 但我得到的是:GridBagLayout的计算器错误 } 应该是这样的:正确的计算器 我试过: 要锚定。。。但它不起作用, 创建多个JPanel(一个带有GridLayout),但不起作用

  • 我有一个JPanel、窗口和一组JPanel和JLabel。我想在JFrame中添加5个JPanel,在每个JPanel中添加一个JLabel。每个JPanel将用于表示有关骰子的数据。 但是,当我运行代码时,只有最后一个JPanel出现在JFrame上,并带有文本“Dice 4”。我不明白为什么。 守则:

  • 问题内容: 如何控制转盘中包含哪些文件?似乎没有被使用。 更新 : 我错了从源tarball安装与安装轮子之间的区别。源代码发行版包含中指定的文件,但已安装的软件包仅包含python文件。无论是通过源分发版,egg还是wheel安装,都需要采取步骤来确定应安装的其他文件。即,其他软件包文件需要package_data,而软件包外部文件(例如命令行脚本或系统配置文件)需要data_files。 原始

  • 问题内容: 有没有办法在翡翠模板中内联? 想要执行此条件检查“内联”,如果fromEdit存在,则结果会将.in添加到div的末尾。 问题答案: 这有效: 在这里尝试。

  • 我正在学习java,我正在尝试从另一个类向我的框架中添加一个菜单栏(练习将代码分成多个类以更好地组织程序)。 以下是我的代码示例: 和menubar类: 我使用eclipse,在这行中: “add”带下划线,因此我无法编译代码。 我一直在寻找解决这个问题的方法,据我所知,这应该是可行的。 感谢您的帮助。 非常感谢。 乔什