Java – add jpanels of other classes to CardLayout
•
Java
I have three windows in three separate classes. I want to use CardLayout so that when you click the next button, the next window will appear How to add jpanels containing different elements to a CardLayout? This is the first window: (the only difference is the background - but it represents how I actually got it)
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(); } }); } }
Second window:
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); } } }
the last one:
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.EAST); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equalsIgnoreCase("next")) { // go to the next window // Window1.cardlayout.next(this); } } }
Solution
I made a small program. I hope the comments written in the program can guide you to know how to use 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 * 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 * has the LayoutManager set to cardlayout. * This card consists of Two JLabels and two JCheck@R_940_2419@ * with GridLayout. */ private ActionListener state; public Window3() { init(); } public void init() { setLayout(new GridLayout(2,2)); JLabel maleLabel = new JLabel("MALE",JLabel.CENTER); final JCheck@R_940_2419@ male@R_940_2419@ = new JCheck@R_940_2419@(); JLabel femaleLabel = new JLabel("FEMALE",JLabel.CENTER); final JCheck@R_940_2419@ female@R_940_2419@ = new JCheck@R_940_2419@(); state = new ActionListener() { public void actionPerformed(ActionEvent ae) { if (male@R_940_2419@ == (JCheck@R_940_2419@) ae.getSource()) { female@R_940_2419@.setSelected(false); JOptionPane.showMessageDialog(null,"Congrats you are a Male","Gender : ",JOptionPane.INFORMATION_MESSAGE); } else if (female@R_940_2419@ == (JCheck@R_940_2419@) ae.getSource()) { male@R_940_2419@.setSelected(false); JOptionPane.showMessageDialog(null,"Congrats you are a Female",JOptionPane.INFORMATION_MESSAGE); } } }; male@R_940_2419@.addActionListener(state); female@R_940_2419@.addActionListener(state); add(maleLabel); add(male@R_940_2419@); add(femaleLabel); add(female@R_940_2419@); } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码