簡體   English   中英

如何在NetBeans中查看來自同一JFrame的包含許多子jPanel的另一個jPanel(Java Swing)

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

我想從按鈕事件動作中顯示另一個jPanel。 例如

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

當我使用CardLayout ,一次只能使用一個面板,難道沒有一種方法可以在一個框架中添加多個面板,然后在事件切換到同一框架中的另一組多個面板之后,又如何呢?

確實,每次使用CardLayout只能顯示一個JPanel ,但這並不能阻止您在使用它時顯示多個JPanel

你需要做的card (在JPanel顯示多是在當前視圖中顯示) JPanel秒。

例如:

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

在此處輸入圖片說明 在此處輸入圖片說明

上面的代碼示出了單個JPanel包含2個JPanel S,其中每個JPanel具有其自己的背景顏色(和可能含有自己的組件,如JLabelJButton ,等等)

我希望這可以使您對要嘗試的操作有所了解。

注意:

  • 想象一下您的JFrame作為筆記本。
  • 想象一下JPanel作為表。
  • 想象一下CardLayout作為您的手指傳遞頁面(前進和后退)

在每個工作表( JPanel )中,您都可以擁有想要的任何東西(甚至可以粘貼到1張以上),這是相同的原理

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM