4

我有 2 个 JPanel 和 1 个 JFrame,当我单击一个按钮时,我试图在面板之间切换。我不想使用 CardLayout,因为我想要不同的面板,而使用 CardLayouts,我只能为两者使用相同的按钮. 我的代码是:

package javaapplication2;

import javax.swing.JPanel;

public class NewJFrame extends javax.swing.JFrame {
/**
 * Creates new form NewJFrame
 */JPanel panel1 = new JPanel();
 JPanel panel2 = new JPanel();
public NewJFrame() {
    initComponents();
}

private void initComponents() {

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setBackground(new java.awt.Color(121, 183, 60));
    setSize(200, 300);
    setResizable(false);



    /**panel1**/
     jButton1 = new javax.swing.JButton();

    jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            jButton1MouseClicked(evt);
        }
    });
    jButton1.setText("jButton1");
     panel1.setBackground(new java.awt.Color(121, 183, 60));
    panel1.setMaximumSize(new java.awt.Dimension(100, 200));
    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(panel1);
    panel1.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(164, 164, 164)
            .addComponent(jButton1)
            .addContainerGap(172, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(115, 115, 115)
            .addComponent(jButton1)
            .addContainerGap(158, Short.MAX_VALUE))
    );

    /**panel2**/

       jButton2 = new javax.swing.JButton();

    jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            jButton2MouseClicked(evt);
        }
    });
    jButton2.setText("jButton2");
     panel2.setBackground(new java.awt.Color(101, 13, 61));
    panel2.setMaximumSize(new java.awt.Dimension(100, 200));
    javax.swing.GroupLayout layout2 = new javax.swing.GroupLayout(panel2);
    panel2.setLayout(layout2);
    layout2.setHorizontalGroup(
        layout2.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout2.createSequentialGroup()
            .addGap(164, 164, 164)
            .addComponent(jButton2)
            .addContainerGap(172, Short.MAX_VALUE))
    );
    layout2.setVerticalGroup(
        layout2.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout2.createSequentialGroup()
            .addGap(115, 115, 115)
            .addComponent(jButton2)
            .addContainerGap(158, Short.MAX_VALUE))
    );
   add(panel2);
    pack();
}


public void changePanel(){
    getContentPane().removeAll();
    add(panel1);
    invalidate();
    repaint();
}

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
    changePanel();
}
 private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
    changePanel();
}

private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;

}

4

1 回答 1

6

这是一个可以帮助您的小例子:

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

public class CardLayoutExample
{
    private JPanel contentPane;
    private MyPanel panel1;
    private MyPanel2 panel2;

    private void displayGUI()
    {
        JFrame frame = new JFrame("Card Layout Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setBorder(
            BorderFactory.createEmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new CardLayout());
        panel1 = new MyPanel(contentPane);
        panel2 = new MyPanel2(contentPane);
        contentPane.add(panel1, "Panel 1"); 
        contentPane.add(panel2, "Panel 2");
        frame.setContentPane(contentPane);
        frame.pack();   
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

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

class MyPanel extends JPanel 
{
    private JButton jcomp4;
    private JPanel contentPane;

    public MyPanel(JPanel panel) 
    {
        contentPane = panel;
        setOpaque(true);
        setBackground(Color.RED.darker().darker());
        //construct components
        jcomp4 = new JButton ("openNewWindow");
        jcomp4.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                CardLayout cardLayout = (CardLayout) contentPane.getLayout();
                cardLayout.next(contentPane);
            }
        });
        add(jcomp4);
    }

    @Override
    public Dimension getPreferredSize()
    {
        return (new Dimension(500, 500));
    }
}

class MyPanel2 extends JPanel 
{

    private JButton jcomp1;
    private JPanel contentPane;

    public MyPanel2(JPanel panel) 
    {   
        contentPane = panel;

        setOpaque(true);
        setBackground(Color.GREEN.darker().darker());

        //construct components
        jcomp1 = new JButton ("Back");
        jcomp1.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                CardLayout cardLayout = (CardLayout) contentPane.getLayout();
                cardLayout.next(contentPane);
            }
        });

        add(jcomp1);
    }

    @Override
    public Dimension getPreferredSize()
    {
        return (new Dimension(500, 500));
    }
}

最新编辑

展示JPanel您的选择,在里面CardLayout

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

public class CardLayoutExample
{
    private JPanel contentPane;
    private MyPanel panel1;
    private MyPanel2 panel2;
    private MyPanel2 panel3;
    private JComboBox choiceBox;
    private String[] choices = {
                                "Panel 1",
                                "Panel 2",
                                "Panel 3"
                               };

    private void displayGUI()
    {
        JFrame frame = new JFrame("Card Layout Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setBorder(
            BorderFactory.createEmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new CardLayout());

        choiceBox = new JComboBox(choices);        

        panel1 = new MyPanel(contentPane, this);
        panel2 = new MyPanel2(contentPane
                , Color.GREEN.darker().darker(), this);
        panel3 = new MyPanel2(contentPane
                , Color.DARK_GRAY, this);   

        contentPane.add(panel1, "Panel 1"); 
        contentPane.add(panel2, "Panel 2");
        contentPane.add(panel3, "Panel 3");         

        frame.getContentPane().add(choiceBox, BorderLayout.PAGE_START);
        frame.getContentPane().add(contentPane, BorderLayout.CENTER);       
        frame.pack();   
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public JComboBox getChoiceBox()
    {
        return choiceBox;
    }

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

class MyPanel extends JPanel 
{
    private JButton jcomp4;
    private JPanel contentPane;
    private JComboBox choiceBox;

    public MyPanel(JPanel panel, CardLayoutExample cle) 
    {
        choiceBox = cle.getChoiceBox();
        contentPane = panel;
        setOpaque(true);
        setBackground(Color.RED.darker().darker());
        //construct components
        jcomp4 = new JButton ("Show New Panel");
        jcomp4.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                String changeToPanel = (String) choiceBox.getSelectedItem();
                CardLayout cardLayout = (CardLayout) contentPane.getLayout();
                cardLayout.show(contentPane, changeToPanel);
            }
        });
        add(jcomp4);
    }

    @Override
    public Dimension getPreferredSize()
    {
        return (new Dimension(500, 500));
    }
}

class MyPanel2 extends JPanel 
{

    private JButton jcomp1;
    private JPanel contentPane;
    private Color backgroundColour;
    private JComboBox choiceBox;

    public MyPanel2(JPanel panel, Color c, CardLayoutExample cle) 
    {   
        contentPane = panel;
        backgroundColour = c;
        choiceBox = cle.getChoiceBox();

        setOpaque(true);
        setBackground(backgroundColour);

        //construct components
        jcomp1 = new JButton ("Show New Panel");
        jcomp1.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                String changeToPanel = (String) choiceBox.getSelectedItem();
                CardLayout cardLayout = (CardLayout) contentPane.getLayout();
                cardLayout.show(contentPane, changeToPanel);
            }
        });

        add(jcomp1);
    }

    @Override
    public Dimension getPreferredSize()
    {
        return (new Dimension(500, 500));
    }
}
于 2012-07-25T15:12:11.243 回答