gpt4 book ai didi

java - 如何在netbeans(Java Swing)中查看同一JFrame中包含许多子jPanel的另一个jPanel

转载 作者:行者123 更新时间:2023-12-02 11:27:21 31 4
gpt4 key购买 nike

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

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

您需要使card(当前 View 中显示的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);
}
}
}

enter image description here enter image description here

上面的代码显示了一个 JPanel,其中包含另外 2 个 JPanel,其中每个 JPanel 都有自己的背景颜色(并且可能包含自己的组件,例如 JLabelJButton 等)

我希望这能让您了解您正在尝试做什么。

注意:

  • 将您的 JFrame 想象成一个笔记本。
  • JPanel 想象为工作表。
  • CardLayout 想象为您的手指经过页面(向后和向前)

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

关于java - 如何在netbeans(Java Swing)中查看同一JFrame中包含许多子jPanel的另一个jPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49517392/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com