【问题标题】:How to view another jPanel containing many sub-jPanels from same JFrame in netbeans (Java Swing)如何在netbeans(Java Swing)中查看包含来自同一JFrame的许多子jPanel的另一个jPanel
【发布时间】:2018-03-27 15:52:52
【问题描述】:

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

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

【问题讨论】:

  • 使用CardLayout,看到这个example给你一个想法
  • 我可以在AbsoluteLayout 中做些什么? @Frakcool
  • 不要使用它。 AbsoluteLayout = null layout = Null layout is evilfrowned upon to use them... 这是 example 使用它们时会发生什么...
  • 好的,感谢您的帮助!
  • 当我使用CardLayout时,我一次只能使用一个面板,有没有办法在一帧中添加多个面板,然后在事件切换到另一组同一框架内的多个面板? @Frakcool

标签: java swing


【解决方案1】:

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

没错,您每次使用CardLayout 只能显示一个JPanel,但这并不妨碍您在使用它时显示多个JPanels...

您需要使card(当前视图中显示的JPanel)显示多个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,其中包含另外两个JPanels,其中每个JPanel都有自己的背景颜色(并且可能包含自己的组件,例如JLabelJButton等)

我希望这能让您了解自己想要做什么。

注意:

  • 将您的JFrame 想象成一个笔记本。
  • JPanels 想象成床单。
  • CardLayout 想象成您的手指在翻页(前后)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    • 2011-05-25
    • 2021-05-03
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多