本文介绍了JFrame中的开关面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个JPanels和1个JFrame,当我单击一个按钮时,我试图在面板之间切换.我不想使用CardLayout,因为我想要不同的面板,并且使用CardLayouts我可以为两个按钮使用相同的按钮.我的代码是:

I have 2 JPanels and 1 JFrame, and I'm trying to switch between panels when I click a button..I don't want to use CardLayout, because I want different panels and with CardLayouts I just can have the same button for both.My code is:

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;

}

推荐答案

下面是一个帮助您的小例子:

Here is one small example for your help :

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

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

这篇关于JFrame中的开关面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 06:01