简体   繁体   English

如何在NetBeans中查看来自同一JFrame的包含许多子jPanel的另一个jPanel(Java Swing)

[英]How to view another jPanel containing many sub-jPanels from same JFrame in netbeans (Java Swing)

I want to show another jPanel from a button event action. 我想从按钮事件动作中显示另一个jPanel。 eg 例如

private void jButtonMouseClicked(MouseEvent e)
{
    getContentPane().removeAll();
    update(getGraphics());
    //code to show another jPanel containing different sub-panels
}

When I use CardLayout , I am able to use only one panel at a time, isn't there a way to add multiple panels in one frame and then after an event switch to another set of multiple panels within same frame? 当我使用CardLayout ,一次只能使用一个面板,难道没有一种方法可以在一个框架中添加多个面板,然后在事件切换到同一框架中的另一组多个面板之后,又如何呢?

Exactly, you can show only one JPanel everytime with CardLayout but that doesn't prevent you to show multiple JPanel s when using it... 确实,每次使用CardLayout只能显示一个JPanel ,但这并不能阻止您在使用它时显示多个JPanel

You need to make the card (the JPanel that is shown in the current view) to show multiple JPanel s. 你需要做的card (在JPanel显示多是在当前视图中显示) JPanel秒。

For example: 例如:

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class CardLayoutWithMultiplePanes {
    private JFrame frame;
    private JPanel pane;
    private JPanel cardsPane;
    private JPanel[] cards;
    private CardLayout cl;
    private JButton nextButton;
    private JButton previousButton;
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new CardLayoutWithMultiplePanes()::createAndShowGui);
    }

    private void createAndShowGui() {
        frame = new JFrame(getClass().getSimpleName());
        pane = new JPanel();
        pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));

        previousButton = new JButton("Previous");
        nextButton = new JButton("Next");

        cl = new CardLayout();
        cardsPane = new JPanel(cl);
        cards = new JPanel[2];

        for (int i = 0; i < cards.length; i++) {
            cards[i] = new JPanel();
            cards[i].setLayout(new GridLayout(2, 1));
            cards[i].add(new CustomPane((i + 1) % 2 == 0 ? Color.BLUE : Color.RED));
            cards[i].add(new CustomPane((i + 1) % 2 == 0 ? Color.GREEN : Color.MAGENTA));

            cardsPane.add(cards[i]);
        }

        Box box = Box.createHorizontalBox();
        box.add(previousButton);
        box.add(Box.createHorizontalGlue());
        box.add(nextButton);

        previousButton.addActionListener(listener);
        nextButton.addActionListener(listener);

        pane.add(cardsPane);
        pane.add(box);

        frame.add(pane);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private ActionListener listener = e -> {
        if (e.getSource().equals(previousButton)) {
            cl.previous(cardsPane);
        } else if (e.getSource().equals(nextButton)) {
            cl.next(cardsPane);
        }
    };

    @SuppressWarnings("serial")
    class CustomPane extends JPanel {
        private Color color;

        public CustomPane(Color color) {
            this.color = color;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(color);
            g.fillRect(0, 0, getWidth(), getHeight());
        }

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

在此处输入图片说明 在此处输入图片说明

The above code shows a single JPanel that contains 2 more JPanel s, in which each JPanel has its own background color (and might contain their own components such as JLabel or JButton , etc) 上面的代码示出了单个JPanel包含2个JPanel S,其中每个JPanel具有其自己的背景颜色(和可能含有自己的组件,如JLabelJButton ,等等)

I hope this gives you an idea for what you're trying to do. 我希望这可以使您对要尝试的操作有所了解。

Note: 注意:

  • Imagine your JFrame as a notebook. 想象一下您的JFrame作为笔记本。
  • Imagine the JPanel s as the sheets. 想象一下JPanel作为表。
  • Imagine CardLayout as your finger passing pages (back and forward) 想象一下CardLayout作为您的手指传递页面(前进和后退)

In every sheet ( JPanel ) you can have whatever you want (even more than 1 sheet (glued to it)), it's the same principle here 在每个工作表( JPanel )中,您都可以拥有想要的任何东西(甚至可以粘贴到1张以上),这是相同的原理

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

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