如何在NetBeans中查看来自同一JFrame的包含许多子jPanel的另一个jPanel(Java Swing) [英] How to view another jPanel containing many sub-jPanels from same JFrame in netbeans (Java Swing)

查看:118
本文介绍了如何在NetBeans中查看来自同一JFrame的包含许多子jPanel的另一个jPanel(Java Swing)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从按钮事件动作中显示另一个jPanel. 例如

I want to show another jPanel from a button event action. e.g.

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

推荐答案

当我使用CardLayout时,一次只能使用一个面板,没有办法在一个框架中添加多个面板,然后在事件切换到同一框架中的另一组多个面板之后?

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只能显示一个JPanel,但这并不能阻止您在使用它时显示多个JPanel ...

Exactly, you can show only one JPanel everytime with CardLayout but that doesn't prevent you to show multiple JPanels when using it...

您需要制作card(当前视图中显示的JPanel)来显示多个JPanel.

You need to make the card (the JPanel that is shown in the current view) to show multiple JPanels.

例如:

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,其中每个JPanel都有其自己的背景色(并且可能包含其自己的组件,例如JLabelJButton等) )

The above code shows a single JPanel that contains 2 more JPanels, in which each JPanel has its own background color (and might contain their own components such as JLabel or JButton, etc)

我希望这可以让您对您要尝试做的事情有所了解.

I hope this gives you an idea for what you're trying to do.

  • 想象一下您的JFrame作为笔记本.
  • 想象一下JPanel作为工作表.
  • CardLayout想象成手指通过页面(前后)
  • Imagine your JFrame as a notebook.
  • Imagine the JPanels as the sheets.
  • Imagine CardLayout as your finger passing pages (back and forward)

在每张纸(JPanel)中,您可以拥有任何想要的东西(甚至超过1张纸(粘在上面)),这是相同的原理

In every sheet (JPanel) you can have whatever you want (even more than 1 sheet (glued to it)), it's the same principle here

这篇关于如何在NetBeans中查看来自同一JFrame的包含许多子jPanel的另一个jPanel(Java Swing)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆